Source Code
#include<bits/stdc++.h>
using namespace std;

int main(){
    int n,k;
    cin>>n>>k;
    int a[n];
    for(int i= 0 ; i<n ; i++)
        cin>>a[i];
    sort(a,a+n);
    int ans = a[0];
    for(int i = 1 ; i<k ; i++){
        if(a[i] == a[i-1])
            ans+=1;
        else    
            ans+=a[i] - a[i-1];
    }
    cout<<ans;
}
Copy
Cutting Trees hashemtb
GNU G++17
33 ms
444 KB
Wrong Answer