2551.cpp (414B)
1 class Solution { 2 public: 3 long long putMarbles(vector<int> &weights, int k) { 4 long long res = 0, n = weights.size(); 5 6 for (int i = 0; i < n - 1; i++) 7 weights[i] += weights[i + 1]; 8 9 weights.resize(n - 1); 10 sort(weights.begin(), weights.end()); 11 12 for (int i = 0; i < k - 1; i++) 13 res += weights[n - 2 - i] - weights[i]; 14 15 return res; 16 } 17 };