Source Code
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n, k;
	cin >> n >> k;
	vector<int> a(n);
	for (int i = 0; i < n; i++)
		cin >> a[i];
	sort(a.begin(), a.end());
	ll st = 1, en = 1e10, res;
	while (st <= en)
	{
		ll mid = (st + en) / 2;
		ll g = mid;
		bool ch = 1;
		for (int i = k - 1; i >= 0; i--)
		{
			if (a[i] > g)
				ch = 0;
			g--;
		}

		if (ch)
		{
			res = mid;
			en = mid - 1;
		}
		else
			st = mid + 1;
	}
	cout << res;

	return 0;
}
Copy
Cutting Trees Yamanabdullah1
GNU G++17
26 ms
696 KB
Accepted