Source Code
#include <bits/stdc++.h>

using namespace std;

const int N=1e5+5;

int n,k;
int arr[N];

int main()
{
	scanf("%d %d",&n,&k);
	for(int i=0;i<n;i++) scanf("%d",&arr[i]);
	sort(arr,arr+n);
	int ans=0;
	for(int i=0;i<k;i++)
		ans=max(ans,arr[i]+(k-i-1));
	printf("%d\n",ans);
}
Copy
Cutting Trees mahmoudbadawy
GNU G++17
30 ms
636 KB
Accepted