Source Code
#include "bits/stdc++.h"
 
using namespace std;
typedef long long ll;
 
void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> v(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i];
    }

    sort(v.begin(), v.end());

    int ans = 0;
    for (int i = 0; i < k; i++) {
        ans = max(ans, v[i] + (k - i - 1));
    }

    cout <<ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    while (t--) {
        solve();
    }
}
Copy
Cutting Trees OsamaX01
GNU G++17
23 ms
636 KB
Accepted