Source Code
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <ctype.h>
#include <queue>
#include <cstring>
#include <set>
#include <bitset>
#include <map>
#include <chrono>
#include <random>
#include <unordered_map>
#include <stdio.h>
#include <deque>
#include <iomanip>
#include <stack>
#include <cassert>
using namespace std;
using ll = long long;
#define int long long
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define ain(x,n) for(int i=0;i<n;i++)cin>>x[i];
#define aout(x,n) for(int i=0;i<n;i++)cout<<x[i]<<" ";
//#define endl '\n'
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mod=32768;
void solve(int tccc)
{
    int n,k;
    cin>>n>>k;
    int arr[n+1];
    for(int i=1;i<=n;i++)
    {
        cin>>arr[i];
    }
    sort(arr+1,arr+n+1);
    int ans=1e18;
    int s=1,e=1e14,m;
    while(s<=e)
    {
        m = (s+e)/2;
        int dur=m;
        bool can=1;
        for(int i=k;i>=1;i--)
        {
            if(arr[i]>dur)
            {
                can=0;
                break;
            }
            dur--;
        }
        if(can)
        {
            ans=min(ans,m);
            e=m-1;
        }
        else
        {
            s=m+1;
        }
    }
    cout<<ans<<endl;

}

int32_t main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
   // cin>>t;
    for(int i=1;i<=t;i++)
    {
        solve(i);
    }

}

Copy
Cutting Trees MrPie
GNU G++17
27 ms
1.1 MB
Accepted