-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.cpp
54 lines (43 loc) · 1.04 KB
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <cmath>
#include <vector>
#include <iomanip>
#include <iostream>
#include <algorithm>
void solve(int N)
{
std::vector<int> students(N, 0);
int sum = 0;
for (int i = 0; i < N; ++i)
{
int dollar; char dot; int cent;
std::cin >> dollar >> dot >> cent;
students[i] = dollar * 100 + cent;
sum += students[i];
}
int lowAverage = sum / N;
int highAverage = lowAverage + ((sum % N) ? 1 : 0);
int sumAbove = 0;
for (int i = 0; i < N; ++i)
{
if (students[i] > highAverage)
sumAbove += students[i] - highAverage;
}
int sumBelow = 0;
for (int i = 0; i < N; ++i)
{
if (students[i] < lowAverage)
sumBelow += lowAverage - students[i];
}
int ansSum = std::max(sumAbove, sumBelow);
std::cout << '$' << (ansSum / 100) << '.' << std::setw(2) << std::setfill('0') << (ansSum % 100) << '\n';
}
int main()
{
std::ios::sync_with_stdio(false);
int N;
while (std::cin >> N, N)
{
solve(N);
}
return 0;
}