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

Previous submission (verdict: Wrong answer on test 8):
//Burn like amaterasu
//compete 
#include <bits/stdc++.h>
#define ll long long int
#define ld long double 
#define f(i,a,b) for(i=a;i<b;i++)
#define fr(i,a,b) for(i=a;i>=b;i--)
#define sz(x) (ll)x.size()
#define Yes cout<<"YES\n"
#define yes cout<<"Yes\n"
#define No cout<<"NO\n"
#define no cout<<"No\n"
#define vll vector<ll>
#define vi vector<int>
#define pl pair<ll,ll>
#define pi pair<int,int>
#define vpl vector<pair<ll,ll>> 
#define vpi vector<pair<int,int>> 
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define srt(v) sort(v.begin(),v.end())
#define rst(v) sort(v.rbegin(),v.rend())
#define rev(v) reverse(v.begin(),v.end())
#define read(v) for(ll xyzd=0;xyzd<v.size();xyzd++)cin>>v[xyzd];
#define print(a)  {for(auto x:a)cout<<x<<" "; cout<<'\n';}
#define printn(a,n) {for(ll i=0;i<n;i++)cout<<a[i]<<" "; cout<<'\n';}
const ll mod=1e9+7;
const ll Mod=998244353;
using namespace std;

//*******************************************************************ordered_set********************************************************************************
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
// operations-> o_set.insert(5); 
// o_set.find_by_order(1)
// o_set.find(2) != o_set.end()
// o_set.erase(o_set.find(2)); 
// *(o_set.find_by_order(1))
//**************************************************************************************************************************************************************
//**********************************************************************G.C.D***********************************************************************************
ll gcd(ll high,ll low)
{
 ll h=high>low?high:low;
 ll l=high>low?low:high;
 ll temp;
while(l!=0)
{
   temp=l;
   l=h%l;
   h=temp;
}
return h;
}
//**************************************************************************************************************************************************************
//**********************************************************************x to the power n (Binpow)***************************************************************
ll binpow(ll x,ll n)
{                   
    ll r=1;
    while(n)
    {
        if(n&1)
        r=r*x;
        x=x*x;
        n>>=1;
    }
    return r; 
}
//**************************************************************************************************************************************************************
//**********************************************************************Fenwick Tree/Binary Indexed Tree********************************************************
template<class T>
class fenwick_tree
{ 
 vector<T> tre;
 public:
 fenwick_tree(T n)
 {
   tre.resize(n+1);
 }
 void add(T index,T value)
 {
    for(;index<=tre.size()-1;index+=(index&(-index)))
    tre[index]+=value;
 }
 T sum_i(T index)
 {
     T ans=0;
     for(;index>0;index-=(index&(-index)))
     ans+=tre[index];
     return ans;
 } 
}; 
//**************************************************************************************************************************************************************
//**********************************************************************SEGEMENT TREE***************************************************************************
class segment_tree{
  public:
    vector<int> seg,val;
    segment_tree(int n){
       val.resize(n);
       seg.resize(4*n+1);
    }
    void build(int ind,int low,int high){
       if(low==high){
         seg[ind]=val[low];
         return;
    }
    int mid =(low+high)/2;
    build(2*ind+1,low,mid);
    build(2*ind+2,mid+1,high);

    seg[ind]=min(seg[2*ind+1],seg[2*ind+2]);
    }
    int query(int ind,int low,int high,int l,int r){
       if(r<low||high<l)return INT_MAX;
       else if(low>=l&&high<=r) return seg[ind];
       
       int mid=(low+high)/2;
       int left=query(2*ind+1,low,mid,l,r);
       int right=query(2*ind+2,mid+1,high,l,r);

       return  min(left,right);
    }
    void update(int ind,int low,int high,int i ,int value){
       if(low==high){
          seg[ind]=value;
          return;
       }
    int mid=(low+high)/2;
    if(i<=mid)update(2*ind+1,low,mid,i,value);
    else update(2*ind+2,mid+1,high,i,value);

    seg[ind]=min(seg[2*ind+1],seg[2*ind+2]);
    }
};
//**************************************************************************************************************************************************************
//**********************************************************************DSU*************************************************************************************
class dsu{
   vector<long long> parent, size;
   public :
   dsu(long long n){ 
         parent.resize(n+1);
         size.resize(n+1);
         long long i;
         for(i=1;i<n+1;i++) parent[i]=i,size[i]=1;
    }
    long long find(long long a){
        if(a==parent[a]) return a;
        return parent[a]=find(parent[a]);
    }
    void union_(long long a, long long b){
         a=find(a), b=find(b);
         if(a==b)return;
         if(size[a]<size[b]) swap(a,b);
         parent[b]=a;
         size[a]+=size[b];
    }
    long long size_(long long a){return size[a];}
  };
//**********************************************************************Solution Begins***************************************************************************
//first check the constraints to determine what complexity is acceptable.
//you always forget to use approach of pivots to calculate anwsers. 
//dont forget the edge cases like when n==1
//always try to prove your solution will work on all cases.
//brute force->implementation->sliding window->
//binary search->math->gready->dynamic programming;

void solve_Func()
{
    ll i, j, k, l, r, q, n, m;
    cin>>n>>l>>r;
    l--,r--;
    vll a(n); read(a);

    vll left, mid, right;

    f(i,0,l) left.pb(a[i]);
    f(i,l,r+1) mid.pb(a[i]);
    f(i,r+1,n) right.pb(a[i]);


    srt(left); srt(right); rst(mid);

    ll ans,sum = accumulate(mid.begin(),mid.end(),0);
    
    ans = sum;

    k = min(left.size(),mid.size());

    ll cur = sum;
    
    f(i,0,k){
        if(left[i] <= mid[i]){
            cur -= mid[i];
            cur += left[i];
        }
        else break;
    }
    ans = min(ans, cur);
    cur = sum;

    k = min(right.size(), mid.size());

    f(i,0,k){
        if(right[i] <= mid[i]){
            cur -= mid[i];
            cur += right[i];
        }
        else break;
    }

    cout<<min(ans ,cur)<<"\n";
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // cout<<fixed<<setprecision(9);
    ll t=1;
  
    cin>>t;
    while(t--)
        solve_Func();

}

//*********end*********//

Submission (verdict: Accepted):
//Burn like amaterasu
//compete 
#include <bits/stdc++.h>
#define ll long long int
#define ld long double 
#define f(i,a,b) for(i=a;i<b;i++)
#define fr(i,a,b) for(i=a;i>=b;i--)
#define sz(x) (ll)x.size()
#define Yes cout<<"YES\n"
#define yes cout<<"Yes\n"
#define No cout<<"NO\n"
#define no cout<<"No\n"
#define vll vector<ll>
#define vi vector<int>
#define pl pair<ll,ll>
#define pi pair<int,int>
#define vpl vector<pair<ll,ll>> 
#define vpi vector<pair<int,int>> 
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define srt(v) sort(v.begin(),v.end())
#define rst(v) sort(v.rbegin(),v.rend())
#define rev(v) reverse(v.begin(),v.end())
#define read(v) for(ll xyzd=0;xyzd<v.size();xyzd++)cin>>v[xyzd];
#define print(a)  {for(auto x:a)cout<<x<<" "; cout<<'\n';}
#define printn(a,n) {for(ll i=0;i<n;i++)cout<<a[i]<<" "; cout<<'\n';}
const ll mod=1e9+7;
const ll Mod=998244353;
using namespace std;

//*******************************************************************ordered_set********************************************************************************
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
// operations-> o_set.insert(5); 
// o_set.find_by_order(1)
// o_set.find(2) != o_set.end()
// o_set.erase(o_set.find(2)); 
// *(o_set.find_by_order(1))
//**************************************************************************************************************************************************************
//**********************************************************************G.C.D***********************************************************************************
ll gcd(ll high,ll low)
{
 ll h=high>low?high:low;
 ll l=high>low?low:high;
 ll temp;
while(l!=0)
{
   temp=l;
   l=h%l;
   h=temp;
}
return h;
}
//**************************************************************************************************************************************************************
//**********************************************************************x to the power n (Binpow)***************************************************************
ll binpow(ll x,ll n)
{                   
    ll r=1;
    while(n)
    {
        if(n&1)
        r=r*x;
        x=x*x;
        n>>=1;
    }
    return r; 
}
//**************************************************************************************************************************************************************
//**********************************************************************Fenwick Tree/Binary Indexed Tree********************************************************
template<class T>
class fenwick_tree
{ 
 vector<T> tre;
 public:
 fenwick_tree(T n)
 {
   tre.resize(n+1);
 }
 void add(T index,T value)
 {
    for(;index<=tre.size()-1;index+=(index&(-index)))
    tre[index]+=value;
 }
 T sum_i(T index)
 {
     T ans=0;
     for(;index>0;index-=(index&(-index)))
     ans+=tre[index];
     return ans;
 } 
}; 
//**************************************************************************************************************************************************************
//**********************************************************************SEGEMENT TREE***************************************************************************
class segment_tree{
  public:
    vector<int> seg,val;
    segment_tree(int n){
       val.resize(n);
       seg.resize(4*n+1);
    }
    void build(int ind,int low,int high){
       if(low==high){
         seg[ind]=val[low];
         return;
    }
    int mid =(low+high)/2;
    build(2*ind+1,low,mid);
    build(2*ind+2,mid+1,high);

    seg[ind]=min(seg[2*ind+1],seg[2*ind+2]);
    }
    int query(int ind,int low,int high,int l,int r){
       if(r<low||high<l)return INT_MAX;
       else if(low>=l&&high<=r) return seg[ind];
       
       int mid=(low+high)/2;
       int left=query(2*ind+1,low,mid,l,r);
       int right=query(2*ind+2,mid+1,high,l,r);

       return  min(left,right);
    }
    void update(int ind,int low,int high,int i ,int value){
       if(low==high){
          seg[ind]=value;
          return;
       }
    int mid=(low+high)/2;
    if(i<=mid)update(2*ind+1,low,mid,i,value);
    else update(2*ind+2,mid+1,high,i,value);

    seg[ind]=min(seg[2*ind+1],seg[2*ind+2]);
    }
};
//**************************************************************************************************************************************************************
//**********************************************************************DSU*************************************************************************************
class dsu{
   vector<long long> parent, size;
   public :
   dsu(long long n){ 
         parent.resize(n+1);
         size.resize(n+1);
         long long i;
         for(i=1;i<n+1;i++) parent[i]=i,size[i]=1;
    }
    long long find(long long a){
        if(a==parent[a]) return a;
        return parent[a]=find(parent[a]);
    }
    void union_(long long a, long long b){
         a=find(a), b=find(b);
         if(a==b)return;
         if(size[a]<size[b]) swap(a,b);
         parent[b]=a;
         size[a]+=size[b];
    }
    long long size_(long long a){return size[a];}
  };
//**********************************************************************Solution Begins***************************************************************************
//first check the constraints to determine what complexity is acceptable.
//you always forget to use approach of pivots to calculate anwsers. 
//dont forget the edge cases like when n==1
//always try to prove your solution will work on all cases.
//brute force->implementation->sliding window->
//binary search->math->gready->dynamic programming;

void solve_Func()
{
    ll i, j, k, l, r, q, n, m;
    cin>>n>>l>>r;
    l--,r--;
    vll a(n); read(a);

    vll left, mid, right;

    f(i,0,l) left.pb(a[i]);
    f(i,l,r+1) mid.pb(a[i]);
    f(i,r+1,n) right.pb(a[i]);


    srt(left); srt(right); rst(mid);

    ll ans,sum = accumulate(mid.begin(),mid.end(),0LL);
    
    ans = sum;

    k = min(left.size(),mid.size());

    ll cur = sum;
    
    f(i,0,k){
        if(left[i] <= mid[i]){
            cur -= mid[i];
            cur += left[i];
        }
        else break;
    }
    ans = min(ans, cur);
    cur = sum;

    k = min(right.size(), mid.size());

    f(i,0,k){
        if(right[i] <= mid[i]){
            cur -= mid[i];
            cur += right[i];
        }
        else break;
    }

    cout<<min(ans ,cur)<<"\n";
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // cout<<fixed<<setprecision(9);
    ll t=1;
  
    cin>>t;
    while(t--)
        solve_Func();

}

//*********end*********//