-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2551.cpp
More file actions
22 lines (22 loc) · 684 Bytes
/
Copy path2551.cpp
File metadata and controls
22 lines (22 loc) · 684 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
long long putMarbles(vector<int>& weights, int k) {
if (k == 1) return 0;
int n = weights.size();
vector<long long> cuts(n - 1, 0);
for (int i = 0; i < n - 1; ++i) {
cuts[i] = (long long)weights[i] + (long long)weights[i + 1];
}
sort(cuts.begin(), cuts.end());
long long maxValue = 0;
long long minValue = 0;
int selections = k - 1;
for (int i = 0; i < selections; ++i) {
minValue += cuts[i];
}
for (int i = n - 2; i >= n - 1 - selections; --i) {
maxValue += cuts[i];
}
return maxValue - minValue;
}
};