Source Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{

    ios_base::sync_with_stdio(0);
    cout.tie(0); cout.tie(0);

    int n , k;
    cin >> n >> k;
    vector<int> a(n);
    for(int &x : a)
        cin >> x;

    sort(a.begin() , a.end());
    auto check = [&](ll mid){
        int i = k - 1;
        for(i; ~i && mid >= a[i] ; i-- ,mid--);
        return i == -1 && mid >= 0;
    };
    ll l = 0 , r = 2e18 , ans = r;
    while(l <= r){
        ll mid = (l + r) >> 1;
        if(check(mid)){
            ans = mid;
            r = mid - 1;
        }
        else l = mid + 1;
    }
    cout << ans;
    return 0;
}
Copy
Cutting Trees Greedious
GNU G++17
25 ms
760 KB
Accepted