Josif_tepe

Untitled

Sep 21st, 2026
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5. typedef long long ll;
  6.  
  7. int main() {
  8.     ios_base::sync_with_stdio(false);
  9.     int n;
  10.     cin >> n;
  11.    
  12.     vector<ll> v(n);
  13.     for(int i = 0; i < n; i++) {
  14.         cin >> v[i];
  15.     }
  16.    
  17.     int K = n / 3;
  18.    
  19.     vector<ll> max_left(n, 0);
  20.     priority_queue<ll, vector<ll>, greater<ll>> pq;
  21.    
  22.     ll current_sum = 0;
  23.     for(int i = 0; i < n; i++) {
  24.         pq.push(v[i]);
  25.         current_sum += v[i];
  26.         if((int) pq.size() > K) {
  27.             current_sum -= pq.top();
  28.             pq.pop();
  29.         }
  30.        
  31.         if((int) pq.size() == K) {
  32.             max_left[i] = current_sum;
  33.         }
  34.     }
  35.    
  36.     vector<ll> min_right(n, 0);
  37.     priority_queue<ll, vector<ll>, less<ll>> pq2;
  38.     ll right_current_sum = 0;
  39.    
  40.     for(int i = n - 1; i >= 0; i--) {
  41.         pq2.push(v[i]);
  42.         right_current_sum += v[i];
  43.         if((int) pq2.size() > K) {
  44.             right_current_sum -= pq2.top();
  45.             pq2.pop();
  46.         }
  47.        
  48.         if((int) pq2.size() == K) {
  49.             min_right[i] = right_current_sum;
  50.         }
  51.     }
  52.    
  53.     ll res = -1e18;
  54.     for(int i = K - 1; i <= 2 * K - 1; i++) {
  55.         ll A = max_left[i];
  56.         ll B = min_right[i + 1];
  57.         res = max(res, A - B);
  58.     }
  59.    
  60.     cout << res << endl;
  61.    
  62.     return 0;
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment