Problem: 2063B - Subsequence Update
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

#include <functional>

#include <vector>

using namespace std;

#define pb push_back

#define fi first

#define se second

#define mp make_pair

#define endl '\n';

#define sz(x) ((int)(x).size())

#define all(x) (x).begin(), (x).end()

#define rall(x) (x).rbegin(), (x).rend()

#define char unsigned char



typedef long long ll;

typedef long double ld;



void solve() {

  ll n, l, r;

  cin >> n >> l >> r;

  l--, r--;

  vector<int> a(n);

  for (auto &i : a)

    cin >> i;

  multiset<int, greater<int>> mid;

  multiset<int> right, left;

  ll orig = 0;

  for (int i = l; i <= r; i++) {

    mid.insert(a[i]);

    orig += a[i];

  }

  for (int i = 0; i < l; i++) {

    left.insert(a[i]);

  }

  for (int i = r + 1; i < n; i++) {

    right.insert(a[i]);

  }

  ll diff1 = 0;

  while (!mid.empty() && !left.empty()) {

    auto m = mid.begin();

    auto l = left.begin();

    if (*m >= *l) {

      diff1 = diff1 - *m + *l;

      mid.erase(m);

      left.erase(l);

    } else {

      break;

    }

  }

  ll ans = min(orig, orig + diff1);

  ll diff2 = 0;

  while (!mid.empty() && !right.empty()) {

    auto m = mid.begin();

    auto r = right.begin();

    if (*m >= *r) {

      diff2 = diff2 - *m + *r;

      mid.erase(m);

      right.erase(r);

    } else {

      break;

    }

  }

  ans = min(ans, orig + diff2);

  cout << ans << endl;



  return;

}



int main() {

  ios::sync_with_stdio(0);

  cin.tie(0);



  int TET = 1;

  cin >> TET;



  while (TET--) {

    solve();

  }

}



Submission (verdict: Accepted):
#include <bits/stdc++.h>
#include <functional>
#include <vector>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define endl '\n';
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define char unsigned char

typedef long long ll;
typedef long double ld;

void solve() {
  ll n, l, r;
  cin >> n >> l >> r;
  l--, r--;
  vector<int> a(n);
  for (auto &i : a)
    cin >> i;
  multiset<int, greater<int>> mid1, mid2;
  multiset<int> right, left;
  ll orig = 0;
  for (int i = l; i <= r; i++) {
    mid1.insert(a[i]);
    mid2.insert(a[i]);
    orig += a[i];
  }
  for (int i = 0; i < l; i++) {
    left.insert(a[i]);
  }
  for (int i = r + 1; i < n; i++) {
    right.insert(a[i]);
  }
  ll diff1 = 0;
  while (!mid1.empty() && !left.empty()) {
    auto m = mid1.begin();
    auto l = left.begin();
    if (*m > *l) {
      diff1 = diff1 - *m + *l;
      mid1.erase(m);
      left.erase(l);
    } else {
      break;
    }
  }
  ll ans = min(orig, orig + diff1);
  ll diff2 = 0;
  while (!mid2.empty() && !right.empty()) {
    auto m = mid2.begin();
    auto r = right.begin();
    if (*m > *r) {
      diff2 = diff2 - *m + *r;
      mid2.erase(m);
      right.erase(r);
    } else {
      break;
    }
  }
  ans = min(ans, orig + diff2);
  cout << ans << endl;

  return;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int TET = 1;
  cin >> TET;

  while (TET--) {
    solve();
  }
}