Source Code
#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <math.h>
#include <iomanip>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <array>
#include <set>
using namespace std;

int main()
{
    /*int _;
    cin>>_;
    while (_--)
    {
    }*/
    long long n,k,ans=0;
    cin>>n>>k;
    vector<long long> ary;
    for(int i=0; i<n; i++)
    {
        int x;
        cin>>x;
        ary.push_back(x);
    }
    sort(ary.begin(),ary.end());
    int x=ary[k-1];
    ans=ary[k-1];
    for(int i=k-1; i>=0; i--)
    {
        if(x<ary[i])
            ans++;
        else
            x--;
    }
    cout<<ans<<endl;
}
Copy
Cutting Trees Abbro
GNU G++17
61 ms
1.5 MB
Accepted