2024-7-1 LCP 40. 心算挑战
代码实现
class Solution {
public:
int maxmiumScore(vector<int>& cards, int cnt) {
int n = size(cards);
std::sort(cards.rbegin(), cards.rend());
int sum = std::accumulate(cards.begin(), cards.begin() + cnt, 0);
int odd = -1, even = -1;
for (int i = 0; i < cnt; ++i) {
if (cards[i] % 2) {
odd = i;
} else {
even = i;
}
}
int ans = 0;
if (cnt == n || sum % 2 == 0) {
ans = sum;
} else {
{
int now = -1;
for (int i = cnt; i < n; ++i) {
if (cards[i] % 2 == 0) {
now = i;
break;
}
}
if (now == -1) {
ans = std::max(ans, sum - cards[odd]);
} else {
ans = std::max(ans, sum - cards[odd] + cards[now]);
}
}
{
int now = -1;
for (int i = cnt; i < n; ++i) {
if (cards[i] % 2) {
now = i;
break;
}
}
if (now == -1) {
ans = std::max(ans, sum - cards[odd]);
} else if (even != -1) {
ans = std::max(ans, sum - cards[even] + cards[now]);
}
}
}
return ans % 2 == 0 ? ans : 0;
}
};
标签:cnt,int,sum,2024,ans,cards,now,合集,LeetCode
From: https://www.cnblogs.com/sleeeeeping/p/18336020