-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1426D.cpp
More file actions
47 lines (37 loc) · 830 Bytes
/
Copy path1426D.cpp
File metadata and controls
47 lines (37 loc) · 830 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
void solve() {
int t;
cin >> t;
while (t--) {
ll n, m;
cin >> n >> m;
ll a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
ll result = 0;
for (int i = 0; i < n - 1; i++) {
result += (a[n - 1] - a[i]);
}
ll maxElement = a[n - 1];
ll target = a[n - 1];
for (int i = n - 2; i >= 0; i--) {
if (a[i] < target) {
target = maxElement;
result -= (maxElement - a[i]);
}
if (i % m == 0) {
target--;
}
}
cout << result << endl;
}
}
int main() {
solve();
return 0;
}