Advertisement
SaberToaster

DOVUIVE

Oct 6th, 2022
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6.  
  7. int main()
  8. {
  9.     map<ll, ll> myArr;
  10.     ll n, temp, k, totalSum = 0;
  11.     cin >> n >> k;
  12.     for (ll i = 0; i < n; i++)
  13.     {
  14.         cin >> temp;
  15.         myArr[temp]++;
  16.     }
  17.     auto iter = myArr.rbegin();
  18.     for (iter; iter != myArr.rend(); iter++)
  19.     {
  20.         if ((iter)->first == 0)
  21.         {
  22.             break;
  23.         }
  24.         if (k > (iter)->second)
  25.         {
  26.             totalSum += (iter)->second * (iter)->first;
  27.             k -= (iter)->second;
  28.         }
  29.         else if (k <= (iter)->second)
  30.         {
  31.             totalSum += k * (iter)->first;
  32.             k = (iter)->second - k;
  33.             break;
  34.         }
  35.  
  36.         myArr[(iter)->first - 1] += (iter)->second;
  37.         // cout << (iter)->first << " " << (iter)->second << "\n";
  38.     }
  39.     cout << totalSum;
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement