Source Code
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 9;
const int mod = 1e9 + 7;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int t;
    t = 1;
    while(t--){
        int n , k;
        cin >> n >> k;
        k--;
        int arr[n];
        for(int i = 0; i < n; i++) cin >> arr[i];
        sort(arr, arr + n);
        int ans = arr[k];
        int cnt = ans;
        for(int i = k; i >= 0; i--){
            if(arr[i] <= cnt){
                cnt--;
            }else ans++;
        }
        cout << ans << "\n";
    }
    return 0;
}  
Copy
Cutting Trees AbduSaber
GNU G++17
35 ms
632 KB
Accepted