Codeforces Round 919 (Div. 2) 1920B. Summation Game Solution
Problem Link : https://codeforces.com/contest/1920/problem/C
Solution in C++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- int n , k , x ;
- cin >> n >> k >> x ;
- vector<int>a(n+1,0) ;
- for (int i = 1 ; i <= n ; i++){
- cin >> a[i] ;
- }
- sort(a.begin(),a.end()) ;
- int pre_sum[n+2] ;
- pre_sum[0] = a[0] ;
- for ( int i = 1 ; i <= n ; i++){
- pre_sum[i] = a[i] + pre_sum[i-1] ;
- }
- int ans = INT_MIN ;
- for ( int i = n ; i >= 0 ; i--){
- int check = n - i ;
- if ( check > k) break ;
- int r = min(x,i) ;
- int sum = (pre_sum[i-r] - (pre_sum[i] - pre_sum[i-r]));
- ans = max(sum,ans) ;
- }
- cout << ans <<endl;
- }
- return 0 ;
- }
No comments