-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathC1LJUTNJ.cpp
49 lines (42 loc) · 1.25 KB
/
C1LJUTNJ.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
// Ivan Carvalho
// Solution to https://www.spoj.com/problems/C1LJUTNJ/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
int N, M;
vector<ii> guloso;
ll resposta;
int main() {
scanf("%d %d", &M, &N);
guloso.push_back(ii(0, 0));
for (int i = 1; i <= N; i++) {
int x;
scanf("%d", &x);
guloso.push_back(ii(x, 1));
}
sort(guloso.begin(), guloso.end());
while (M > 0) {
int diferenca = guloso.back().first - guloso[guloso.size() - 2].first,
tamanho = guloso.back().second;
// printf("Diferenca %d Tamanho %d\n",diferenca,tamanho);
ll precisa = diferenca * guloso.back().second;
if (precisa <= M) {
M -= precisa;
guloso.pop_back();
guloso.back().second += tamanho;
} else {
ll paratodos = M / tamanho;
ll resta = M % tamanho;
guloso.back().first -= paratodos;
guloso.back().second -= resta;
guloso.push_back(ii(guloso.back().first - 1, resta));
M = 0;
}
}
for (ii pares : guloso) {
resposta += 1LL * pares.first * pares.first * pares.second;
}
printf("%lld\n", resposta);
return 0;
}