Problem: 2063B - Subsequence Update
Language: C++17 (GCC 7-32)

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

using namespace std;

#define ll long long

#define ll long long

#define uin unsigned int

#define ull unsigned long long

#define ld long double

#define fl(i, w, n) for (ll i = w; i < n; i++)

#define rfl(i, w, n) for (ll i = w; i >= n; i--)

#define F first

#define S second

#define B begin()

#define E end()

#define EB emplace_back

#define PB push_back

#define PRES(c, x) ((c).find(x) != (c).end())

#define dbg(...) ;

#define debug(...) ;

typedef vector<string> vs;

typedef pair<ll, ll> pii;

typedef pair<ll, ll> pll;

typedef vector<ll> vi;

typedef vector<ll> vl;

typedef vector<vi> vvi;

typedef vector<vl> vvl;

typedef vector<pii> vpii;

typedef vector<pll> vpll;

#define um(x, y) unordered_map<x, y>

#define us(x) unordered_set<x>

#define mpl map lii

#define mem1(a) memset(a, -1, sizeof(a))

#define mem0(a) memset(a, 0, sizeof(a))

#define sum(a) (accumulate((a).begin(), (a).end(), 0ll))

#define mine(a) (*min_element((a).begin(), (a).end()))

#define maxe(a) (*max_element((a).begin(), (a).end()))

#define mini(a) (min_element((a).begin(), (a).end()) - (a).begin())

#define maxi(a) (max_element((a).begin(), (a).end()) - (a).begin())

#define lowb(a, x) (lower_bound((a).begin(), (a).end(), (x)) - (a).begin())

#define uppb(a, x) (upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

#define nl << "\n";

#define nll cout << "\n";

#define Yes cout << "Yes" << "\n";

#define No cout << "No" << "\n";

#define PI 3.1415926535897932384626

#define SET(n) cout << fixed << setprecision(n)

#define get(s) getline(cin, s)

#define ppc __builtin_popcount

#define ppcll __builtin_popcountll

#define clz __builtin_clz

#define clzll __builtin_clzll

#define ctz __builtin_ctz

#define fast                                                                   \

  ios_base::sync_with_stdio(0);                                                \

  cin.tie(0);                                                                  \

  cin.tie(nullptr);                                                            \

  cout.tie(nullptr);



#define INF 1e18

#define vi vector<ll>

#define vb vector<bool>

#define vc vector<char>

#define vvi vector<vector<ll>>

#define vvc vector<vector<char>>

#define vvb vector<vector<bool>>

#define vpi vector<pair<ll, ll>>

#define mi map<ll, ll>

#define mpi map<pair<ll, ll>, ll>

#define pr pair<ll, ll>

#define mp make_pair

#define triplet pair<ll, pair<ll, ll>>

#define ff first

#define ss second

#define pb push_back

#define MAX ll_MAX

#define MIN ll_MIN

#define fr(i, n) for (ll i = 0; i < n; i++)

#define rep(i, a, b) for (ll i = a; i < b; i++)

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

#define read(x)                                                                \

  ll x;                                                                        \

  cin >> x

#define pD(x, y) cout << fixed << setprecision(y) << x

#define google(tno) cout << "Case #" << tno << ": "

#define pqb priority_queue<ll>

#define pqs priority_queue<ll, vi, greater<ll>>

#define setbits(x) __builtin_popcountll(x)

#define zerobits(x) __builtin_ctzll(x)

#define mod 998244353

#define LMAX 9223372036854775807

#define upperToLower(x) char(x | ' ')

#define lowerToUpper(x) char(x & '_')

const ll MOD = 1e9 + 7;

const ll GN = 1e5 + 10;



void _prll(ll t) { cerr << t; }

void _prll(string t) { cerr << t; }

void _prll(char t) { cerr << t; }

void _prll(ld t) { cerr << t; }

void _prll(double t) { cerr << t; }



template <class T, class V> void _prll(pair<T, V> p);

template <class T> void _prll(vector<T> v);

template <class T> void _prll(set<T> v);

template <class T, class V> void _prll(map<T, V> v);

template <class T> void _prll(multiset<T> v);

template <class T, class V> void _prll(pair<T, V> p) {

  cerr << "{";

  _prll(p.ff);

  cerr << ",";

  _prll(p.ss);

  cerr << "}";

}

template <class T> void _prll(vector<T> v) {

  cerr << "[ ";

  for (T i : v) {

    _prll(i);

    cerr << " ";

  }

  cerr << "]";

}

template <class T> void _prll(set<T> v) {

  cerr << "[ ";

  for (T i : v) {

    _prll(i);

    cerr << " ";

  }

  cerr << "]";

}

template <class T> void _prll(multiset<T> v) {

  cerr << "[ ";

  for (T i : v) {

    _prll(i);

    cerr << " ";

  }

  cerr << "]";

}

template <class T, class V> void _prll(map<T, V> v) {

  cerr << "[ ";

  for (auto i : v) {

    _prll(i);

    cerr << " ";

  }

  cerr << "]";

}



vi fact(GN, 1);

vb sieve(GN, 1);

vi dsieve(GN, 0);



bool isPrime(ll n) {

  if (n == 1)

    return 0;

  for (ll i = 2; i * i <= n; i++) {

    if (n % i == 0)

      return false;

  }

  return true;

}

void primeSieve() {

  sieve[0] = sieve[1] = false;

  for (ll i = 2; i * i < GN; i++) {

    if (sieve[i]) {

      for (ll j = i * i; j < GN; j += i)

        sieve[j] = false;

    }

  }

}

void divSieve() {

  for (ll i = 1; i < GN; i++) {

    for (ll j = i; j < GN; j = j + i) {

      ++dsieve[j];

    }

  }

}

void factSieve() {

  for (ll i = 1; i < GN; i++)

    fact[i] = (fact[i - 1] % MOD * 1LL * i % MOD) % MOD;

}

mi primeFactorise(ll n) {

  mi mpp;

  for (ll i = 2; i * i <= n; i++) {

    if (n % i == 0) {

      ll cnt = 0;

      while (n % i == 0) {

        ++cnt;

        n = n / i;

      }

      mpp[i] = cnt;

    }

  }

  if (n > 1)

    mpp[n] = 1;

  return mpp;

}

ll gcd(ll a, ll b) {

  if (b == 0)

    return a;

  return gcd(b, a % b);

}

ll binexp(ll x, ll n) {

  ll a = x;

  ll prod = 1;

  while (n) {

    if (n % 2 == 1)

      prod = prod * a;

    a = a * a;

    n = n / 2;

  }

  return prod;

}

ll modexp(ll a, ll b, ll m) {

  a %= m;

  ll res = 1LL;

  while (b > 0) {

    if (b & 1)

      res = (res % m * 1LL * a % m) % m;

    a = (a % m * 1LL * a % m) % m;

    b >>= 1;

  }

  return res % m;

}

ll modadd(ll a, ll b, ll m) { return (a % m + b % m) % m; }

ll modsub(ll a, ll b, ll m) { return (a % m - b % m + m) % m; }

ll modmul(ll a, ll b, ll m) { return (a % m * 1LL * b % m) % m; }

ll modinv(ll a, ll m) { return modexp(a, m - 2, m); }

ll moddiv(ll a, ll b, ll m) { return (a % m * 1LL * modinv(b, m) % m) % m; }

ll C(ll n, ll r) {

  if (n < r || r < 0)

    return 0;

  if (r == 1)

    return n;

  ll nmr = fact[n] % MOD;

  ll dmr = (fact[r] % MOD * 1LL * fact[n - r] % MOD) % MOD;

  return (nmr % MOD * 1LL * modinv(dmr, MOD) % MOD) % MOD;

}



void printVector(vector<ll> &v) {

  for (int i = 0; i < v.size(); i++) {

    cout << v[i] << " ";

  }

  cout << endl;

}



ll calAns(vector<ll> &middleSeg) {

  return accumulate(middleSeg.begin(), middleSeg.end(), 0);

}



void solve() {

  ll n, l, r;

  cin >> n >> l >> r;

  vector<ll> a(n);

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

    cin >> a[i];

  }

  vector<ll> leftSeg(l - 1), middleSeg(r - l + 1), rightSeg(n - r);

  ll lInd = 0, mInd = 0, rInd = 0;

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

    if (i < l - 1) {

      leftSeg[lInd++] = a[i];

    } else if (i < r) {

      middleSeg[mInd++] = a[i];

    } else {

      rightSeg[rInd++] = a[i];

    }

  }

  sort(leftSeg.begin(), leftSeg.end());

  sort(middleSeg.begin(), middleSeg.end());

  sort(rightSeg.begin(), rightSeg.end());

  // cout << "leftSeg:" << endl;

  // printVector(leftSeg);

  // cout << "middleSEg" << endl;

  // printVector(middleSeg);

  // cout << "rightSeg" << endl;

  // printVector(rightSeg);

  vector<ll> newMiddleSeg = middleSeg;

  lInd = 0;

  ll ans = calAns(newMiddleSeg);

  if (leftSeg.size() > 0) {

    for (int i = middleSeg.size() - 1; i >= 0; i--) {

      if (lInd < l - 1 && middleSeg[i] > leftSeg[lInd]) {

        newMiddleSeg[i] = leftSeg[lInd];

        lInd++;

      } else {

        break;

      }

    }

    ans = calAns(newMiddleSeg);

  }

  // printVector(newMiddleSeg);

  rInd = 0;

  if (rightSeg.size() > 0) {

    for (int i = middleSeg.size() - 1; i >= 0; i--) {

      if (rInd < n-r && middleSeg[i] > rightSeg[rInd]) {

        newMiddleSeg[i] = rightSeg[rInd];

        rInd++;

      } else {

        newMiddleSeg[i] = middleSeg[i];

      }

    }

    // printVector(newMiddleSeg);

    ans = min(ans, calAns(newMiddleSeg));

  }

  cout << ans << endl;

}

signed main() {

  fast;

  ll t;

  cin >> t;

  while (t--) {

    solve();

  }

}



Submission (verdict: Accepted):
#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define ll long long

#define uin unsigned int

#define ull unsigned long long

#define ld long double

#define fl(i, w, n) for (ll i = w; i < n; i++)

#define rfl(i, w, n) for (ll i = w; i >= n; i--)

#define F first

#define S second

#define B begin()

#define E end()

#define EB emplace_back

#define PB push_back

#define PRES(c, x) ((c).find(x) != (c).end())

#define dbg(...) ;

#define debug(...) ;

typedef vector<string> vs;

typedef pair<ll, ll> pii;

typedef pair<ll, ll> pll;

typedef vector<ll> vi;

typedef vector<ll> vl;

typedef vector<vi> vvi;

typedef vector<vl> vvl;

typedef vector<pii> vpii;

typedef vector<pll> vpll;

#define um(x, y) unordered_map<x, y>

#define us(x) unordered_set<x>

#define mpl map lii

#define mem1(a) memset(a, -1, sizeof(a))

#define mem0(a) memset(a, 0, sizeof(a))

#define sum(a) (accumulate((a).begin(), (a).end(), 0ll))

#define mine(a) (*min_element((a).begin(), (a).end()))

#define maxe(a) (*max_element((a).begin(), (a).end()))

#define mini(a) (min_element((a).begin(), (a).end()) - (a).begin())

#define maxi(a) (max_element((a).begin(), (a).end()) - (a).begin())

#define lowb(a, x) (lower_bound((a).begin(), (a).end(), (x)) - (a).begin())

#define uppb(a, x) (upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

#define nl << "\n";

#define nll cout << "\n";

#define Yes cout << "Yes" << "\n";

#define No cout << "No" << "\n";

#define PI 3.1415926535897932384626

#define SET(n) cout << fixed << setprecision(n)

#define get(s) getline(cin, s)

#define ppc __builtin_popcount

#define ppcll __builtin_popcountll

#define clz __builtin_clz

#define clzll __builtin_clzll

#define ctz __builtin_ctz

#define fast                                                                   \

  ios_base::sync_with_stdio(0);                                                \

  cin.tie(0);                                                                  \

  cin.tie(nullptr);                                                            \

  cout.tie(nullptr);



#define INF 1e18

#define vi vector<ll>

#define vb vector<bool>

#define vc vector<char>

#define vvi vector<vector<ll>>

#define vvc vector<vector<char>>

#define vvb vector<vector<bool>>

#define vpi vector<pair<ll, ll>>

#define mi map<ll, ll>

#define mpi map<pair<ll, ll>, ll>

#define pr pair<ll, ll>

#define mp make_pair

#define triplet pair<ll, pair<ll, ll>>

#define ff first

#define ss second

#define pb push_back

#define MAX ll_MAX

#define MIN ll_MIN

#define fr(i, n) for (ll i = 0; i < n; i++)

#define rep(i, a, b) for (ll i = a; i < b; i++)

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

#define read(x)                                                                \

  ll x;                                                                        \

  cin >> x

#define pD(x, y) cout << fixed << setprecision(y) << x

#define google(tno) cout << "Case #" << tno << ": "

#define pqb priority_queue<ll>

#define pqs priority_queue<ll, vi, greater<ll>>

#define setbits(x) __builtin_popcountll(x)

#define zerobits(x) __builtin_ctzll(x)

#define mod 998244353

#define LMAX 9223372036854775807

#define upperToLower(x) char(x | ' ')

#define lowerToUpper(x) char(x & '_')

const ll MOD = 1e9 + 7;

const ll GN = 1e5 + 10;



void _prll(ll t) { cerr << t; }

void _prll(string t) { cerr << t; }

void _prll(char t) { cerr << t; }

void _prll(ld t) { cerr << t; }

void _prll(double t) { cerr << t; }



template <class T, class V> void _prll(pair<T, V> p);

template <class T> void _prll(vector<T> v);

template <class T> void _prll(set<T> v);

template <class T, class V> void _prll(map<T, V> v);

template <class T> void _prll(multiset<T> v);

template <class T, class V> void _prll(pair<T, V> p) {

  cerr << "{";

  _prll(p.ff);

  cerr << ",";

  _prll(p.ss);

  cerr << "}";

}

template <class T> void _prll(vector<T> v) {

  cerr << "[ ";

  for (T i : v) {

    _prll(i);

    cerr << " ";

  }

  cerr << "]";

}

template <class T> void _prll(set<T> v) {

  cerr << "[ ";

  for (T i : v) {

    _prll(i);

    cerr << " ";

  }

  cerr << "]";

}

template <class T> void _prll(multiset<T> v) {

  cerr << "[ ";

  for (T i : v) {

    _prll(i);

    cerr << " ";

  }

  cerr << "]";

}

template <class T, class V> void _prll(map<T, V> v) {

  cerr << "[ ";

  for (auto i : v) {

    _prll(i);

    cerr << " ";

  }

  cerr << "]";

}



vi fact(GN, 1);

vb sieve(GN, 1);

vi dsieve(GN, 0);



bool isPrime(ll n) {

  if (n == 1)

    return 0;

  for (ll i = 2; i * i <= n; i++) {

    if (n % i == 0)

      return false;

  }

  return true;

}

void primeSieve() {

  sieve[0] = sieve[1] = false;

  for (ll i = 2; i * i < GN; i++) {

    if (sieve[i]) {

      for (ll j = i * i; j < GN; j += i)

        sieve[j] = false;

    }

  }

}

void divSieve() {

  for (ll i = 1; i < GN; i++) {

    for (ll j = i; j < GN; j = j + i) {

      ++dsieve[j];

    }

  }

}

void factSieve() {

  for (ll i = 1; i < GN; i++)

    fact[i] = (fact[i - 1] % MOD * 1LL * i % MOD) % MOD;

}

mi primeFactorise(ll n) {

  mi mpp;

  for (ll i = 2; i * i <= n; i++) {

    if (n % i == 0) {

      ll cnt = 0;

      while (n % i == 0) {

        ++cnt;

        n = n / i;

      }

      mpp[i] = cnt;

    }

  }

  if (n > 1)

    mpp[n] = 1;

  return mpp;

}

ll gcd(ll a, ll b) {

  if (b == 0)

    return a;

  return gcd(b, a % b);

}

ll binexp(ll x, ll n) {

  ll a = x;

  ll prod = 1;

  while (n) {

    if (n % 2 == 1)

      prod = prod * a;

    a = a * a;

    n = n / 2;

  }

  return prod;

}

ll modexp(ll a, ll b, ll m) {

  a %= m;

  ll res = 1LL;

  while (b > 0) {

    if (b & 1)

      res = (res % m * 1LL * a % m) % m;

    a = (a % m * 1LL * a % m) % m;

    b >>= 1;

  }

  return res % m;

}

ll modadd(ll a, ll b, ll m) { return (a % m + b % m) % m; }

ll modsub(ll a, ll b, ll m) { return (a % m - b % m + m) % m; }

ll modmul(ll a, ll b, ll m) { return (a % m * 1LL * b % m) % m; }

ll modinv(ll a, ll m) { return modexp(a, m - 2, m); }

ll moddiv(ll a, ll b, ll m) { return (a % m * 1LL * modinv(b, m) % m) % m; }

ll C(ll n, ll r) {

  if (n < r || r < 0)

    return 0;

  if (r == 1)

    return n;

  ll nmr = fact[n] % MOD;

  ll dmr = (fact[r] % MOD * 1LL * fact[n - r] % MOD) % MOD;

  return (nmr % MOD * 1LL * modinv(dmr, MOD) % MOD) % MOD;

}



void printVector(vector<ll> &v) {

  for (int i = 0; i < v.size(); i++) {

    cout << v[i] << " ";

  }

  cout << endl;

}



ll calAns(vector<ll> &middleSeg) {

  return accumulate(middleSeg.begin(), middleSeg.end(), 0ll);

}



void solve() {

  ll n, l, r;

  cin >> n >> l >> r;

  vector<ll> a(n);

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

    cin >> a[i];

  }

  vector<ll> leftSeg(l - 1), middleSeg(r - l + 1), rightSeg(n - r);

  ll lInd = 0, mInd = 0, rInd = 0;

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

    if (i < l - 1) {

      leftSeg[lInd++] = a[i];

    } else if (i < r) {

      middleSeg[mInd++] = a[i];

    } else {

      rightSeg[rInd++] = a[i];

    }

  }

  sort(leftSeg.begin(), leftSeg.end());

  sort(middleSeg.begin(), middleSeg.end());

  sort(rightSeg.begin(), rightSeg.end());

  // cout << "leftSeg:" << endl;

  // printVector(leftSeg);

  // cout << "middleSEg" << endl;

  // printVector(middleSeg);

  // cout << "rightSeg" << endl;

  // printVector(rightSeg);

  vector<ll> newMiddleSeg = middleSeg;

  lInd = 0;

  ll ans = calAns(newMiddleSeg);

  if (leftSeg.size() > 0) {

    for (int i = middleSeg.size() - 1; i >= 0; i--) {

      if (lInd < l - 1 && middleSeg[i] > leftSeg[lInd]) {

        newMiddleSeg[i] = leftSeg[lInd];

        lInd++;

      } else {

        break;

      }

    }

    ans = min(ans, calAns(newMiddleSeg));

  }

  // printVector(newMiddleSeg);

  newMiddleSeg = middleSeg;

  rInd = 0;

  if (rightSeg.size() > 0) {

    for (int i = middleSeg.size() - 1; i >= 0; i--) {

      if (rInd < n - r && middleSeg[i] > rightSeg[rInd]) {

        newMiddleSeg[i] = rightSeg[rInd];

        rInd++;

      } else {

        break;

      }

    }

    // printVector(newMiddleSeg);

    ans = min(ans, calAns(newMiddleSeg));

  }

  cout << ans << endl;

}

signed main() {

  fast;

  ll t;

  cin >> t;

  while (t--) {

    solve();

  }

}