GPT答: |
7i24.cn
|
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> candies(N);
for (int i = 0; i < N; i++) {
cin >> candies[i];
}
sort(candies.begin(), candies.end(), greater<int>());
int sum = 0;
int max_candy = -1;
for (int i = 0; i < N; i++) {
if (i % M == 0) {
if (max_candy != -1) {
sum += max_candy;
}
max_candy = -1;
}
if (candies[i] > max_candy) {
max_candy = candies[i];
}
if ((i + 1) % M == 0 && max_candy != -1) {
sum += max_candy;
max_candy = -1;
}
if ((i + 1) == N && max_candy != -1) {
sum += max_candy;
}
}
cout << sum << endl;
return 0;
}
```
|