3075.cpp (393B)
1 class Solution { 2 public: 3 long long maximumHappinessSum(vector<int> &happiness, int k) const { 4 long long res = 0; 5 6 nth_element(begin(happiness), begin(happiness) + k, end(happiness), greater()); 7 sort(begin(happiness), begin(happiness) + k, greater()); 8 9 for (int i = 0; i < k; i++) 10 res += max(happiness[i] - i, 0); 11 12 return res; 13 } 14 };