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 = v[k - 1];
    map<int, int> mp;

    for (int i = k - 1; i >= 1; i--) {
        ans -= (v[i] - v[i - 1] - 1); 
    }

    cout << max(ans, v[k - 1]) << '\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
632 KB
Wrong Answer