1.

package sem2.am.thetrip;

/**
* Angewandte Mathematik, SS11
* Problem: 10137 - The Trip
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=13&page=show_problem&problem=1078
*
* @author Florian Stein
* @author Franz Sommer
* @version 1.0, 05/08/2011
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 0.456
*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.StringTokenizer;

public class Main {

public static void main(String... args) throws IOException {

double[] bricks;

// input reader
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));

// string der zeile
String inputLine;

int size = 1;

while (size != 0) {

inputLine = reader.readLine();
StringTokenizer token = new StringTokenizer(inputLine);
size = new Integer(token.nextToken());

if (size == 0)
break;

// zuruecksetzen
double avg = 0.00;

if (size == 0) {
break;
}

// array fuer die betraege
bricks = new double[size];

// nimmt alle betraege auf
for (int i = 0; i < size; i++) {
inputLine = reader.readLine();
token = new StringTokenizer(inputLine);
bricks[i] = new Double(token.nextToken());
// parallel ins durchschnittsbetrag addieren
avg = avg + bricks[i];
}

// durchschnittsbetrag berechnen
avg = runden((avg / size), 2);

// hilfsvariablen
double above = 0;
double below = 0;

// berechnet den benoetigten austausch
for (int i = 0; i < bricks.length; i++) {
if (bricks[i] < avg) {
below = below + avg - bricks[i];
}
if (bricks[i] > avg) {
above = above + bricks[i] - avg;
}
}

if (above == 0) {
above = below;
}

if (below == 0) {
below = above;
}

// formatierung
NumberFormat n = NumberFormat.getInstance(Locale.US);

n.setMaximumFractionDigits(2);
n.setMinimumFractionDigits(2);
n.setGroupingUsed(false);

// ausgabe
if (below < above)
System.out.println("$" + n.format(runden(below, 2)));
else {
System.out.println("$" + n.format(runden(above, 2)));
}
}
}

// hilfsmethode zum runden
public static double runden(double x, int precision) {
return Math.floor(x * Math.pow(10.0, (double) precision) + 0.5)
/ Math.pow(10.0, (double) precision);
}
}