Educational Codeforces Round 148 (Rated for Div. 2) 1832B - Maximum Sum Solution


 

Problem Link :  https://codeforces.com/contest/1832/problem/B

Solution in C++:

/// Author : AH_Tonmoy
#include <bits/stdc++.h>
using namespace std;
const int mx = 2e5+3 ;
using ll = long long ;
ll a[mx] ;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll  t;
  cin >> t;
  while (t--) {
    int n , k ;
    cin >> n >> k ;
    for ( int i = 1 ; i <= n ; i++) {
      cin >> a[i] ;
    }
    sort(a+1,a+n+1) ;
    for ( int i = 1 ; i <= n ; i++) {
      a[i] +=a[i-1] ;
    }
    ll ans = 0 ,l , r ;
    for ( int i = 0 ; i <= k ; i++) {
      l = 2 * i ;
      r = n - (k - i) ;
      ans = max ( ans , a[r] - a[l]) ;
    }
    cout << ans << endl; 
  }
  ret

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.