Source Code
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int OO = 1e9+7 ,N=2e5+5;

int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 }; // dir array
int dy[] = { 1, -1, 0, 0, 1, -1, -1, 1 };
void File(){
#ifndef ONLINE_JUDGE
    freopen("Input.txt", "r", stdin);
    freopen("Output.txt", "w", stdout);
#endif
}
int a[N],n,k;
bool can(int md){
    for(int i=k-1;i>=0;i--){
        if(md<a[i])return 0;
        md--;
    }
    return 1;
}
int solve(){
    cin>>n>>k;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    sort(a,a+n);
    int l=1,r=2e9,md,ans=-1;
    while(l<=r){
        md=(l+r)>>1;
        if(can(md))ans=md , r=md-1;
        else l=md+1;
    }
    cout<<ans;

    return 0;
}
int main() {
    File();
    ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
//    int t,i=1;cin>>t;
//    while(t--)
        solve();

}
//Solve on paper first
//Overflooooooow
//Reverse Thinking
//If there's an equation, transform it into an easy one
//READ ALL PROBLEMS
//Verify Your thought before Coding
Copy
Cutting Trees ahmes
GNU G++17
2070 ms
652 KB
Time Limit Exceeded