Code: Select all

```
import java.text.DecimalFormat;
/*
* The Trip
*/
class Main {
/**
* Read a string from the console. The string is terminated by a newline
*
* @return the input string (without the newline)
*/
String readString() {
String delim = "\n";
char c = delim.charAt(0);
StringBuffer s = new StringBuffer();
try {
c = (char) System.in.read();
while (delim.indexOf((int) c) == -1 && c != 65535) {
if (c != '\r') {
s.append((char) c);
}
c = (char) System.in.read();
}
} catch (Exception e) {
return (null);
}
if (s.toString().equals("")) {
return null;
}
return s.toString();
}
/*
* Round the decimal to precision digits.
*/
double round(double x, int precision) {
return Math.floor(x * Math.pow(10.0, (double) precision) + 0.5)
/ Math.pow(10.0, (double) precision);
}
/*
* Calculate the min share.
*/
double calculate(double[] a) {
double avg = 0.0;
for (int i = 0; i < a.length; i++) {
avg += a[i];
}
avg = round(avg/a.length,2);
double a1 = 0, a2 = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] > avg) {
a1 += a[i] - avg;
} else {
a2 += avg - a[i];
}
}
a1 = Math.min(a1, a2);
a1 = round(a1,2);
return a1;
}
void solve() {
String input = readString();
int a;
double[] in;
while (true) {
a = Integer.parseInt(input);
if (a == 0) {
break;
}
in = new double[a];
for (int i = 0; i < a; i++) {
in[i] = new Double(readString()).doubleValue();
}
DecimalFormat df = new DecimalFormat();
df.applyPattern("0.00");
System.out.println(df.format(calculate(in)));
input = readString();
}
}
public static void main(String[] args) {
Main m = new Main();
m.solve();
}
}
```

input

3

10.00

20.00

30.00

4

15.00

15.01

3.00

3.01

5

5000.00

11.11

11.11

11.11

11.11

3

0.01

0.03

0.03

4

25.00

25.00

25.00

28.00

3

10.01

15.25

18.96

4

25.03

25.00

25.00

25.00

0

output

$10.00

$11.99

$3991.11

$0.01

$2.25

$4.73

$0.02