-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2491.cpp
More file actions
26 lines (22 loc) · 690 Bytes
/
Copy path2491.cpp
File metadata and controls
26 lines (22 loc) · 690 Bytes
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
class Solution {
public:
long long dividePlayers(const vector<int> &skill) const {
static int count[1001];
memset(count, 0x00, sizeof(count));
const int sum = accumulate(begin(skill), end(skill), 0);
const int n = size(skill), goal = sum / (n / 2);
if (goal * n / 2 != sum) return -1;
long long res = 0, cnt = 0;
for (const int n : skill) {
const int target = goal - n;
if (!count[target])
count[n]++;
else {
count[target]--;
res += target * n;
cnt += 2;
}
}
return cnt == n ? res : -1;
}
};