Problem: 2063B - Subsequence Update
Language: c++17 

Solution 1 (runtime: 140 ms):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <random>
#include <ctime>
#include <list>
#include <stack>
#include <bitset>
#include <chrono>
#define N 200005
#define M 1002
#define eps 1e-7
using namespace std;
typedef long long ll;
const ll mod = 1000000007LL;
const int mod2 = 998244353;
const int inf = 1000000010;
//const __int128 hmod = 212370440130137957LL;
//srand((unsigned)time(0)); 
const int base=337;
int n, m, q;
int k;
int t;
int a[N];
ll check(int l1, int r1, int l2, int r2) {
  ll tot=0LL;
  set<pair<int,int>> ss;
  ss.clear();
  for (int i = l2; i <= r2; ++i) {
    tot += a[i];
    ss.insert(make_pair(a[i], i));
  }
  vector<int> v;
  v.clear();
  for (int i = l1; i <= r1; ++i) {
    v.push_back(a[i]);
  }
  sort(v.begin(), v.end());
  for (auto x : v) {
    if (!ss.empty() && x < (*ss.rbegin()).first) {
      tot -= (*ss.rbegin()).first;
      ss.erase(*ss.rbegin());
      tot += x;
    } else {
      break;
    }
  }
  return tot;
}
int main() {
  cin>>t;
  for (int cas=1; cas<=t; ++cas) {
    int l, r;
    scanf("%d%d%d", &n, &l, &r);
    ll ans=0LL;
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i]);
      if (i>=l && i<=r) ans += a[i];
    }
    ll res1=check(1, l-1, l, r);
    ll res2=check(r+1, n, l, r);
    ans = min(ans, min(res1, res2));
    printf("%lld\n", ans);
  }

  // cin>>n;
  // srand((unsigned)time(0)); 
  // for (int i = 1; i <= n; ++i) {
  //   printf("%d ", rand()%n+1);
  // }
  // cout<<endl;
  return 0;
}


Solution 2 (runtime: 78 ms):
// ---------------------------------

// READ { WHOLE } QUESTION PROPERLY. |

// ---------------------------------

// --------------------------------------------

// READ QUESTION CAREFULLY IN EDUCATIONAL ROUND

// --------------------------------------------

// understand question properly and have patience if not able to understand question in first time.

// -----------------------------------------------

// | VERY IMPORTANT : OBSERVE TESTCASES PROPERLY. |

// -----------------------------------------------

// understand question properly and fast.

// don't do faasam faas if you know its easy question or you have to face a lot of repurcussion.

// don't do stupidness in div 4 be calm and do it.

// if not able to do find out solution of any question jump to another question.

// Keep Precomputation in mind if it can be used.

// If questions are done early don't be happy try next question very seriously



#include <bits/stdc++.h>



using namespace std;



typedef long long ll;

typedef long double ld;

typedef double dd;

typedef float fl;



const ll N = 1e6+7;



vector<ll> binary(ll n){

    vector<ll> v;

    while(n!=0){

        v.push_back(n%2);

        n=n/2;

    }

       // reverse(v.begin(),v.end());

    return v;

}

const int MOD = 1e9 + 7;

const int MAX_N = 2.1e5 + 1;

vector<long long> fact(MAX_N);

vector<long long> invFact(MAX_N);

void precomputeFactorials() {

    fact[0] = 1;

    for (int i = 1; i < MAX_N; ++i) {

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

    }

}

long long modInverse(long long x, long long mod) {

    long long result = 1, power = mod - 2;

    while (power) {

        if (power & 1) result = (result * x) % mod;

        x = (x * x) % mod;

        power >>= 1;

    }

    return result;

}

void precomputeInvFactorials() {

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

        invFact[i] = modInverse(fact[i], MOD);

    }

}

long long comb(int n, int k) {

    if (k > n || k < 0) return 0;

    return fact[n] * invFact[k] % MOD * invFact[n - k] % MOD;

}

ll power(ll a,ll b){

    ll ans=1;

    while(b){

        if(b&1) ans=ans*a;

        a=a*a;

        b>>=1;

    }

    return ans;

}



vector<bool> isPrime(N,1);



bool sortbysec(const pair<int,int> &a,const pair<int,int> &b){

        return a.second<b.second;

}



int main(){

//   isPrime[0]=isPrime[1]=false;

//   for(int i=2;i*i < N;i++){

//       if(isPrime[i]==true){

//           for(int j=2*i;j<N;j+=i){

//               isPrime[j]=false;

//           }

//       }

//   }



    ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0);



    int t=1;

    cin >> t;

    while(t--){

        ll n,l,r;

        cin>>n>>l>>r;

        vector<ll> v(n);

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

            cin>>v[i];

        }

        vector<ll> w,x;

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

            w.push_back(v[i]);

        }

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

            x.push_back(v[i]);

        }

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

        //     x.push_back(v[i]);

        // }

        // cout<<w.size()<<x.size()<<"\n";

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

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

        ll left=0,right=w.size()-1;

        while(right>=0 && left<x.size()){

           if(w[right]>=x[left]) swap(x[left],w[right]);

           else break;

           left++;

           right--;

        }

        ll sum=0;

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

            sum+=w[i];

        }

        w.clear();

        x.clear();

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

            w.push_back(v[i]);

        }

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

            x.push_back(v[i]);

        }

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

        //     x.push_back(v[i]);

        // }

        // cout<<w.size()<<x.size()<<"\n";

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

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

        left=0,right=w.size()-1;

        while(right>=0 && left<x.size()){

           if(w[right]>=x[left]) swap(x[left],w[right]);

           else break;

           left++;

           right--;

        }

        ll sum1=0;

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

            sum1+=w[i];

        }

        cout<<min(sum,sum1)<<"\n";

    }

    return 0;

}