-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathljutnja.cc
38 lines (30 loc) · 961 Bytes
/
ljutnja.cc
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
#include <cstdio>
#include <algorithm>
#include <vector>
#include <functional>
#include <cmath>
#include <numeric>
#define min(a, b) ((a) < (b)) ? (a) : (b)
int main() {
unsigned long m, n;
scanf("%lu %lu", &m, &n);
unsigned long long demand = 0;
std::vector<unsigned long> wishes(n);
for (unsigned long long i = 0; i < n; ++i) {
unsigned long c;
scanf("%lu", &c);
wishes[i] = c;
demand += c;
}
std::sort(wishes.begin(), wishes.end(), std::less<unsigned long long>());
unsigned long unaccounted_for = demand - m;
// Divide the unaccounted for candies evenly between all children.
unsigned long result = 0;
for (unsigned long i = 0; i < n; ++i) {
unsigned long wish_remainder = min(wishes[i], unaccounted_for / (n - i));
result += wish_remainder * wish_remainder;
unaccounted_for -= wish_remainder;
}
printf("%lu\n", result);
return 0;
}