-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cpp
33 lines (33 loc) · 893 Bytes
/
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
class Solution {
public:
int maxmiumScore(vector<int>& cards, int cnt) {
sort(cards.begin(), cards.end());
int ans = 0;
int n = cards.size();
for (int i = 0; i < cnt; ++i) {
ans += cards[n - i - 1];
}
if (ans % 2 == 0) {
return ans;
}
const int inf = 1 << 29;
int mx1 = -inf, mx2 = -inf;
for (int i = 0; i < n - cnt; ++i) {
if (cards[i] % 2) {
mx1 = cards[i];
} else {
mx2 = cards[i];
}
}
int mi1 = inf, mi2 = inf;
for (int i = n - 1; i >= n - cnt; --i) {
if (cards[i] % 2) {
mi2 = cards[i];
} else {
mi1 = cards[i];
}
}
ans = max(ans - mi1 + mx1, ans - mi2 + mx2);
return ans < 0 ? 0 : ans;
}
};