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

    ll n,k;cin>>n>>k;
    ll a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    ll d=a[k-1],p=0;
    for(int i=k-1;i>=0;i--){
        if(a[i]>d)
            p+=abs(a[i]-d);
        p++;
        d--;
    }
    cout<<d+p;
       
}
Copy
Cutting Trees call911kid
GNU G++17
31 ms
1.0 MB
Wrong Answer