Source Code
#include <bits/stdc++.h>
using namespace std;

#define endl "\n"
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

typedef long long int ll;

ll mod=1e9+7;
ll a[300010];

ll tc,i,j,k,n,m;
string s,s1,s2;

map<ll,ll> mp;
vector<ll> v;
int main(){
    IOS
            cin>>n>>k;

            for(i=0;i<n;i++)
                cin>>a[i];

            sort(a,a+n);

            if(k==1){
                cout<<a[0]<<endl;
                return 0;
            }

            ll ans=a[0];
            ll num=1;

            for(i=1;i<n;i++){

                if( (a[i]-ans)>1 )
                ans+=abs(a[i]-ans);
                else
                ans++;

                num++;

                if(num==k)
                break;
            }

            cout<<ans<<endl;


}
Copy
Cutting Trees AshcoOoOdel
GNU G++14
36 ms
1.0 MB
Accepted