Codeforces Round 919 (Div. 2) 1920B. Summation Game Solution


 

Problem Link : https://codeforces.com/contest/1920/problem/C

Solution in C++:

  1. /// Author : AH_Tonmoy
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int32_t main() {
  5. ios_base::sync_with_stdio(0);
  6. cin.tie(0);
  7. int t;
  8. cin >> t;
  9. while (t--) {
  10. int n , k , x ;
  11. cin >> n >> k >> x ;
  12. vector<int>a(n+1,0) ;
  13. for (int i = 1 ; i <= n ; i++){
  14. cin >> a[i] ;
  15. }
  16. sort(a.begin(),a.end()) ;
  17. int pre_sum[n+2] ;
  18. pre_sum[0] = a[0] ;
  19. for ( int i = 1 ; i <= n ; i++){
  20. pre_sum[i] = a[i] + pre_sum[i-1] ;
  21. }
  22. int ans = INT_MIN ;
  23. for ( int i = n ; i >= 0 ; i--){
  24. int check = n - i ;
  25. if ( check > k) break ;
  26. int r = min(x,i) ;
  27. int sum = (pre_sum[i-r] - (pre_sum[i] - pre_sum[i-r]));
  28. ans = max(sum,ans) ;
  29. }
  30. cout << ans <<endl;
  31. }
  32. return 0 ;
  33. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.