-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbee1225.cpp
More file actions
49 lines (40 loc) · 1.04 KB
/
Copy pathbee1225.cpp
File metadata and controls
49 lines (40 loc) · 1.04 KB
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
/**
* Contest : Beecrowd
* Problem : 1225 - Coral Perfeito
* Link : https://judge.beecrowd.com/pt/problems/view/1225
* Time : O(N)
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fastio ios::sync_with_stdio(0); cin.tie(0);
int main() {
fastio
int n;
while (cin >> n) {
vector<ll> v(n);
ll sum = 0;
// se a cada compasso, um v [i] aumemta +1
// e um v[i] diminui -1, soma total sem mantém
for (auto& i : v) {
cin >> i;
sum += i;
}
// se a soma se mantém e no final todos v[i]
// devem ser iguais, então temos que o
// n*v[i] = sum, logo sum deve ser multiplo de n
if (sum % n != 0)
cout << -1 << '\n';
else {
// para n*v[i] = sum, logo v[i] = sum/n
ll cnt = 0, media = sum/n;
// contar quantos compassos para v[i] ser igual a média
// soh precisar contar uma das operações (+1 ou -1)
for (auto& i : v)
if (i < media)
cnt += media - i;
cout << cnt+1 << '\n';
}
}
return 0;
}