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

#define ll                 long long
#define l(i,k,n)           for (int i=k ; i<n ; i++)

bool ch(int d , int a[] , int k){
    int i=k-1;
    while(i>=0){
        if(a[i] > d) return 0;
        d--; i--;
    }
    return 1;
}
int main ()
{
    cin.tie(0); cin.sync_with_stdio(0);                     
                            int n,k;
                            cin>>n>>k;
                            int a[n];
                            l(i , 0 , n) cin>>a[i];
                            sort(a,a+n);

                            int l=1 , r=2e9 , d , ans=0;
                            while(l<=r){
                                d = (l+r)/2;
                                if(ch(d,a,k)) ans=d , r=d-1;
                                else l=d+1;
                            }
                            cout<<ans;
                   return 0;
                }
Copy
Cutting Trees The Legendary_Scar
GNU G++17
2075 ms
660 KB
Time Limit Exceeded