fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N=1e6+4;
  4. long long a[N],n,k;
  5. int main()
  6. {
  7. cin>>n>>k;
  8. for(int i=0;i<n;i++) cin>>a[i];
  9. sort(a+1,a+n+1,greater<long long>());
  10. long long s=0;
  11. for(int i=0;i<=k;i++) s=s+a[i];
  12. for(int i=k+1;i<n;i++) s=s-a[i];
  13. cout<<s;
  14. }
  15.  
Success #stdin #stdout 0.01s 5520KB
stdin
Standard input is empty
stdout
Standard output is empty