Source Code
#include <bits/stdc++.h>

using namespace std;
#define pb push_back
typedef long long ll;



int main(){

	int T,n,k, t,a,b;

        cin>>n>>k;
        vector<int>v,v2;
       for(t=0;t<n;t++){
        cin>>a;
        v.pb(a);
       }

       sort(v.begin(),v.end());

        ll ans=v[k-1],c=ans;
       for(t=k-1;t>=0;t--){
         if(c<v[t]){
            ans+=(v[t]-c);
            c+=(v[t]-c);
         }

         c--;
       }

       cout<<ans<<"\n";



}
Copy
Cutting Trees mohammed_mraish
GNU G++17
58 ms
988 KB
Accepted