ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 108 ms):
#include <iostream>
#include <vector>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while (t--) {
        int n;
        cin >> n;

        vector<int> a(n);
        for (int i = 0; i < n; ++i) {
            cin >> a[i];
        }

        bool possible = true;
        for (int i = n - 2; i >= 0; --i) {
            if (a[i] > a[i + 1]) {
                if (i == 0) {
                    possible = false;
                    break;
                }
                int diff = a[i] - a[i + 1];
                a[i] -= diff;
                a[i - 1] -= diff;
                if (a[i - 1] < 0) {
                    possible = false;
                    break;
                }
            }
        }

        cout << (possible ? "YES" : "NO") << '\n';
    }

    return 0;
}

Solution 2 (runtime: 78 ms):
#include <bits/stdc++.h> 

#define fr(i, x, n)for(int i = x; i<(int)n; ++i) 

#define fi(i, x, n)for(int i = (int)x - 1; i >= n; --i) 

#define in >> 

#define p << 

#define fp(a, b, t, x) for(pair<a, b>t : x) 

#define vp(x) vector<pair<int, int>>x; 

#define v(x, d) vector<d>x; 

#define fast_io ios::sync_with_stdio(0);cin.tie(0); 

#define cAll(a, b) count(a.begin(), a.end(), b) 

#define ff first 

#define ss second 

#define lsb(x) ((x) & -(x))

typedef long long ll; 

using namespace std; 



void solve(){

    int t, n; cin in t;

    while(t--){

        cin in n;

        vector<int>a(n);

        fr(i, 0, n) cin in a[i];

        if(is_sorted(a.begin(), a.end())) cout p "YES\n";

        else{

            fr(i, 0, n - 1){

                //make operation

                int x = min(a[i], a[i + 1]);

                a[i] -= x;

                a[i + 1] -= x;

            }

            if(is_sorted(a.begin(), a.end())) cout p "YES\n";

            else cout p "NO\n";

        }

    }

}



int main(){

    fast_io

    solve();

    return 0;

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

Solution 1 (runtime: 202 ms):
#include <bits/stdc++.h>
 
using namespace std;


int main() {
	long long q, x , y, z;
	cin >> q;
	while(q--){
		cin >> x >> y >> z;
		long long a[x], b[x];
		for(int i = 0; i < x;i++)
			cin >> a[i];
		long long ans = 0, k = 0;
		for(int i = y - 1; i < z; i++) 
			ans += a[i];
		for(int i = 0; i < z;i++){
			b[k] = a[i];
			k++;	
		}
		sort(b ,b + k);
		long long o = 0;
		for(int i = 0;i < z - y + 1;i++) 
			o += b[i];
		ans = min(ans, o);
		o = 0, k = 0;
		for(int i = y - 1; i < x;i++) 
			b[k++] = a[i];
		sort(b,b + k);
		for(int i = 0;i < z - y + 1;i++)
			o += b[i];
		ans = min(ans, o);
		cout << ans << "\n";
	}
}

Solution 2 (runtime: 108 ms):
#include <bits/stdc++.h>
using namespace std;
#define ar array

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t; 
    cin >> t;
    while(t--){
        int n, l, r;
        cin >> n >> l >> r;
        vector<long long> a(n);
        for (int i = 0; i < n; i++){
            cin >> a[i];
        }
        
        long long origSum = 0;
        for (int i = l - 1; i < r; i++){
            origSum += a[i];
        }
        
        vector<long long> leftPart, insidePart, rightPart;
        for (int i = 0; i < l - 1; i++){
            leftPart.push_back(a[i]);
        }
        for (int i = l - 1; i < r; i++){
            insidePart.push_back(a[i]);
        }
        for (int i = r; i < n; i++){
            rightPart.push_back(a[i]);
        }
        
        sort(leftPart.begin(), leftPart.end());
        sort(rightPart.begin(), rightPart.end());
        
        vector<long long> insideDesc = insidePart;
        sort(insideDesc.begin(), insideDesc.end(), greater<long long>());
        
        int szLeft = leftPart.size();
        int szInside = insideDesc.size();
        int szRight = rightPart.size();
        
        vector<long long> prefLeft(szLeft + 1, 0), prefInside(szInside + 1, 0), prefRight(szRight + 1, 0);
        for (int i = 0; i < szLeft; i++){
            prefLeft[i + 1] = prefLeft[i] + leftPart[i];
        }
        for (int i = 0; i < szInside; i++){
            prefInside[i + 1] = prefInside[i] + insideDesc[i];
        }
        for (int i = 0; i < szRight; i++){
            prefRight[i + 1] = prefRight[i] + rightPart[i];
        }
        
        long long bestBenefitLeft = 0;
        int maxPairsLeft = min(szLeft, szInside);
        for (int p = 1; p <= maxPairsLeft; p++){
            long long benefit = prefInside[p] - prefLeft[p];
            bestBenefitLeft = max(bestBenefitLeft, benefit);
        }
        
        long long bestBenefitRight = 0;
        int maxPairsRight = min(szRight, szInside);
        for (int p = 1; p <= maxPairsRight; p++){
            long long benefit = prefInside[p] - prefRight[p];
            bestBenefitRight = max(bestBenefitRight, benefit);
        }
        
        long long bestBenefit = max(bestBenefitLeft, bestBenefitRight);
        long long ans = origSum - bestBenefit;
        cout << ans << "\n";
    }
    return 0;
}
Problem: 2052A - Adrenaline Rush
Language: c++17 

Solution 1 (runtime: 187 ms):
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1010;
int a[N];
int b[N];
int st[N];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		cin>>a[i];
		b[i]=i;
	}
	int cut=0;
	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){
			if(a[i]>a[j]){
				cut++;
			}
		}
	}
	int ans=n*(n-1)-cut;
	printf("%d\n",ans);
	reverse(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		int p=a[i];
		for(int j=p-1;j>0;j--){
			if(st[j]) continue;
			printf("%d %d\n",p,j);
		}
		st[p]=1;
		for(int j=1;j<=n;j++){
			if(st[j]) continue;
			printf("%d %d\n",j,p);
		}
		
	}
	return 0;
}

Solution 2 (runtime: 358 ms):
#include<bits/stdc++.h>
using namespace std;

// JAY SHREE KRISHNA
// AUTHOR:- Solanki Nirmal Bhikhabhai

// ------------------------------------------------------------------------------------------------------------
#define hello ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define ld long double
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define pb push_back
#define pp pop_back
#define endl "\n"
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
// ------------------------------------------------------------------------------------------------------------

vector<ll> llToBin(ll n){
    vector<ll> ans;
    while(n!=0){
        ans.pb(n%2);
        n/=2;
    }
    reverse(all(ans));
    return ans;
}

vector<ll> isprime(1e6+1,1);
void sieve(int n){
    isprime[1]=0;
    for(ll i=2;i*i<1000000;i++){
        if(isprime[i]!=0){
            for(ll j=i*i;j<1000000;j+=i) isprime[j]=0;
        }
    }
}

void printvec(vector<ll> temp){
    for(ll i=0;i<temp.size();i++) cout<<temp[i]<<" ";
    cout<<endl;
}

void printmap(map<ll,ll> temp){
    for(auto it=temp.begin();it!=temp.end();it++) cout<<it->fi<<" "<<it->se<<endl;
}

ll ncr(ll n,ll r){
    ld sum=1;
    for(ll i=0;i<=r;i++) sum=sum*(n-r+i)/i;
    return (ll)sum;
}


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

void solve(){
    ll n;		cin>>n;
    vector<ll> a(n);
    for(ll i=0;i<n;i++) cin>>a[i];
    map<ll,ll>mp;
    map<ll,ll>mp2;
    for(int i=0;i<n;i++)
    {
        mp2[a[i]]++;
    }
    vector<pair<ll,ll>>vp;
    for(ll i=n-1;i>=0;i--)
    {
        mp[a[i]]++;
        mp2.erase(a[i]);
        for(ll j=a[i]-1;j>=1;j--)
        {
            if(mp[j]==0)
            {
                vp.pb({a[i],j});
            }
        }
        for(auto it:mp2)
        {
            vp.pb({it.first,a[i]});
        }
    }
    cout<<vp.size()<<endl;
    for(int i=0;i<vp.size();i++)
    {
        cout<<vp[i].first<<" "<<vp[i].second<<endl;
    }
}

int32_t main(){
    hello;
    
        solve();
    
    return 0;
}
Problem: 2050E - Three Strings
Language: c++17 

Solution 1 (runtime: 77 ms):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const bool multicase = 1;
int s(string a, string b, string c, int i, int j, int ind, vector<vector<int>> &dp)
{
    int p = c.size();
    if (ind >= p)
    {
        return 0;
    }
    if (dp[i][j] != -1)
    {
        return dp[i][j];
    }
    int n = a.size();
    int m = b.size();
    int ans = INT_MAX;
    if (i < n && a[i] == c[ind])
    {
        ans = min(ans, s(a, b, c, i + 1, j, ind + 1, dp));
    }
    if (j < m && b[j] == c[ind])
    {
        ans = min(ans, s(a, b, c, i, j + 1, ind + 1, dp));
    }
    if (i < n)
        ans = min(ans, 1 + s(a, b, c, i + 1, j, ind + 1, dp));
    if (j < m)
        ans = min(ans, 1 + s(a, b, c, i, j + 1, ind + 1, dp));
    return dp[i][j] = ans;
}
void solve()
{
    string a, b, c;
    cin >> a >> b >> c;
    vector<vector<int>> dp(a.size() + 1, vector<int>(b.size() + 1, INT_MAX));
    //  int ans = s(a, b, c, 0, 0, 0, dp);
    // cout << ans << endl;
    dp[0][0] = 0;
    int n = a.size();
    int m = b.size();
    for (int i = 0; i <= a.size(); i++)
    {
        for (int j = 0; j <= b.size(); j++)
        {
            int p = i + j;
            if (i > 0 && c[p - 1] == a[i - 1])
            {
                dp[i][j] = min(dp[i][j], dp[i - 1][j]);
            }
            if (j > 0 && c[p - 1] == b[j - 1])
            {
                dp[i][j] = min(dp[i][j], dp[i][j - 1]);
            }
            if (i > 0)
            {
                dp[i][j] = min(dp[i][j], 1 + dp[i - 1][j]);
            }
            if (j > 0)
            {
                dp[i][j] = min(dp[i][j], 1 + dp[i][j - 1]);
            }
        }
    }
    cout << dp[n][m] << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    if (multicase)
    {
        cin >> t;
    }
    else
    {
        t = 1;
    }
    while (t--)
    {
        solve();
    }
    return 0;
}

Solution 2 (runtime: 62 ms):
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  int t; cin >> t;
  while(t--) {
    string a, b, c; cin >> a >> b >> c;
    int n = a.size(), m = b.size();
    vector<vector<int>> dp(n+1, vector<int>(m+1, 1e9));
    dp[0][0] = 0;
    for(int i = 1; i<=n; i++) dp[i][0] = dp[i-1][0]+(c[i-1]!=a[i-1]);
    for(int i = 1; i<=m; i++) dp[0][i] = dp[0][i-1]+(c[i-1]!=b[i-1]);
    for(int i = 1; i<=n; i++) {
      for(int j = 1; j<=m; j++) {
        char cc = c[i+j-1];
        dp[i][j] = min(dp[i][j], dp[i-1][j]+(cc!=a[i-1]));
        dp[i][j] = min(dp[i][j], dp[i][j-1]+(cc!=b[j-1]));
      }
    }
    /*for(int i = 0; i<=m; i++) {
      for(int j = 0; j<=n; j++) {
        printf("%d %d %d\n", i, j, dp[i][j]);
      }
    }*/
    cout << dp[n][m]<< endl;
  }
  return 0;
}
Problem: 2052A - Adrenaline Rush
Language: c++17 

Solution 1 (runtime: 327 ms):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

// https://codeforces.com/blog/entry/62393
struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		// http://xorshift.di.unimi.it/splitmix64.c
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}

	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};

void solve() {
	int n;
	cin >> n;
	vector<int> c(n), p(n);
	for (int i = 0; i < n; i++) {
		cin >> c[i];
		c[i]--;
		p[c[i]] = i;
	}
	vector<vector<int>> ans;
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			ans.push_back({j, i});
		}
	}
	for (int x = n - 1; x >= 0; x--) {
		for (int i = n - 1; i > p[x]; i--) {
			if (c[i] > x) {
				ans.push_back({x, c[i]});
			}
		}
	}
	cout << ans.size() << "\n";
	for (auto& v : ans) {
		cout << v[0] + 1 << " " << v[1] + 1 << "\n";
	}
	// 2 1 3
	// 2 3 1
	// 3 2 1
	// 2 3 1
	//
}

int main() {
	ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int tt = 1;
	while (tt--) {
		solve();
	}
}



Solution 2 (runtime: 1139 ms):
#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	
	int n;
    cin >> n;
 
    int arr[n];
    for(int i=0;i<n;i++){
        cin >> arr[i];
    }
    
    vector<pair<int,int>> vec;
    map<int,int> mp;
    
    for(int i=(n-1);i>=0;i--){
        int val = arr[i];
        mp[val]=1;
        for(int j=(val-1);j>=1;j--){
            if(mp[j]){
                continue;
            }
            vec.push_back({val,j});
        }
        for(int j=1;j<=n;j++){
            if(mp[j]){
                continue;
            }
            vec.push_back({j,val});
        }
    }
 
    cout << vec.size() << endl;
 
    for(auto it:vec){
        cout << it.first << " " << it.second << endl;
    }
    
    return 0;

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

Solution 1 (runtime: 202 ms):
#include<bits/stdc++.h>

#define int long long

using namespace std;









void Solve()

{



    int n, l , r;

    cin >> n >> l >> r ;

    vector<int>a(n,0);

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

        cin >> a[i];

    }

    l--;

    vector<int>b = a;

    sort(a.begin() + l , a.end());

    sort(b.begin() , b.begin() + r);



    int sum = 0;

    int sum1 = 0;

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

        sum += a[i];

    }



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

        sum1 += b[i];

    }



    cout<<min(sum,sum1)<<endl;

}





int32_t main(){



   int t;

   cin >> t;

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

    Solve();

   }

}



//6

//17

//22

//

//

//00110

//01001

//10110

//

//32 + 16 + 8 + 4 + 2



Solution 2 (runtime: 93 ms):
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
 
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    ll tc ; cin>>tc ;
    while(tc--)
    {
    	ll n, x, y ; cin>>n>>x>>y ; x--; y--;
    	
    	vector < ll > v (n);
    	
    	for(int i = 0 ; i<n ; i++)
    	{
    		cin>>v[i];
    	}
    	
    	vector < ll > a ; vector < ll > b ;
    	
    	for(int i = 0 ; i<=y ; i++)
    	{
    		a.push_back(v[i]);
    	}
    	for(int i = x ; i<n ; i++)
    	{
    		b.push_back(v[i]);
    	}
    	
    	sort(a.begin(), a.end()); sort(b.begin(), b.end());
    	
    	
    	
    	ll ans1 = 0, ans2 = 0 ;
    	
    	for(int i = 0 ; i<(y-x+1) ; i++)
    	{
    		ans1 += a[i];
    		ans2 += b[i];
    	}
    	
    	cout<<min(ans1, ans2)<<'\n';
    }
    
	return 0 ;
}
Problem: 2057C - Trip to the Olympiad
Language: c++17 

Solution 1 (runtime: 62 ms):
#include <bits/stdc++.h>
using namespace std;

int main () {
  ios_base::sync_with_stdio(0); cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int l, r;
    cin >> l >> r;
    int b = 30;
    int cm = 0;
    while ((l & (1 << b)) == (r & (1 << b))) {
      cm |= (l & (1 << b));
      b--;
    }
    int x = cm + (1<<b)-1;
    int y = x+1;
    int z = r;
    if(z==x || z==y)z=l;
    cout << x << ' ' << y << ' ' << z << '\n'; 
  }
}


Solution 2 (runtime: 46 ms):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define popb pop_back
#define vect_arr(n) vector<long long> arr(n)
#define loop(l,r) for(long long i=l;i<r;i++)
#define looprev(r,l) for(long long i=r;i>l;i--)
using namespace std;

void solve(){
    int l,r;
    cin>>l>>r;
    int a=0,b=0,c=0;
    int curr = pow(2,30);
    while((l&curr) == (r&curr)){
        // cout<<"Here"<<"\n";
        
        if((l&curr)>0){
            a+=curr;
            b=a;
            c=a;
        }
        curr=curr>>1;
    }

    a+=curr;
    b+=(curr-1);
    cout<<a<<" "<<b<<" "<<c<<"\n";
}

signed main()
{   int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
Problem: 2050E - Three Strings
Language: c++17 

Solution 1 (runtime: 77 ms):
#include <bits/stdc++.h>
#define int long long
const int m1=1e9+7;
const int m2=998244353;
using namespace std;
void sol(){
string a,b,c;
cin>>a>>b>>c;
int n=a.size();
int m=b.size();
vector<vector<int>>dp(n+1,vector<int>(m+1,1e9));
dp[0][0]=0;
for(int i=0;i<=n;i++){
   for(int j=0;j<=m;j++){
   int k=i+j;
   int cost=0;
   if(i<n){
   if(a[i]!=c[k]) cost=1;
   dp[i+1][j]=min(dp[i+1][j],dp[i][j]+cost);
   }
   if(j<m){
   cost=0;
   if(b[j]!=c[k]) cost=1;
   dp[i][j+1]=min(dp[i][j+1],dp[i][j]+cost);
  }
   }
 }
 cout<<dp[n][m]<<endl;
}
 signed main() {
    #ifndef ONLINE_JUDGE
    freopen("input1.txt", "r", stdin);
    freopen("output1.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   //cout<<fixed<<setprecision(9);
    
    int t=1;
    cin>>t;
    while(t--){
     sol();
    }
return 0;
}

Solution 2 (runtime: 765 ms):
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<climits>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<queue>
#define ll long long int
using namespace std;

int solve(string &a,string &b,string &c,int i,int j,int s,vector<vector<vector<int> > >&dp){
    if(i==a.size() && j==b.size()){
        if(s==1){
            if(a[i-1]!=c[i+j+1-2]){
                return dp[i][j][s]=1;
            }
            return  dp[i][j][s]=0;
        }
        if(b[j-1]!=c[i+j+1-2]){
            return  dp[i][j][s]=1;
        }
        return  dp[i][j][s]=0;
        
    }
    if(dp[i][j][s]!=-1){
        return dp[i][j][s];
    }
    if(s==1){
        int cmp=i+j+1-2;
        int add=0;
        if(a[i-1]!=c[cmp]){
            add=1;
        }
        int c1=1e9,c2=1e9;
        if(i+1<=a.size()-1+1){
            c1=add+solve(a,b,c,i+1,j,1,dp);
        }

        if(j+1<=b.size()-1+1){
            c2=add+solve(a,b,c,i,j+1,0,dp);
        }

        return  dp[i][j][s]=min(c1,c2);

        }
        int cmp=i+j+1-2;
        int add=0;
        if(b[j-1]!=c[cmp]){
            add=1;
        }
        int c1=1e9,c2=1e9;
        if(i+1<=a.size()-1+1){
            c1=add+solve(a,b,c,i+1,j,1,dp);
        }

        if(j+1<=b.size()-1+1){
            c2=add+solve(a,b,c,i,j+1,0,dp);
        }

        return  dp[i][j][s]=min(c1,c2);


    





}

int main(){
    ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     int t=1;
     cin>>t;
     while(t--){
        string a;
        cin>>a;

        string b;
        cin>>b;

        string c;
        cin>>c;
        int n1=a.size();
        int n2=b.size();
        
        vector<vector<vector<int> > >dp1(n1+1,vector<vector<int> >(n2+1,vector<int>(2,-1)));
         vector<vector<vector<int> > >dp2(n1+1,vector<vector<int> >(n2+1,vector<int>(2,-1)));

        int ans1=solve(a,b,c,1,0,1,dp1);
        int ans2=solve(a,b,c,0,1,0,dp2);

        cout<<min(ans1,ans2)<<endl;


      
     
       
       
       
        




        
     }


}
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 77 ms):
#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        int n = s.length();
        for(int i = 0; i < n;i++){
            int ind = i;
            int curr = s[i] -'0';
            for(int j = i+1;j <=(i+9) && j < n;j++){
                if(s[j]-'0' == 0)continue;
                int k = s[j] - '0' - j +i;
                if(k > curr){
                    ind = j;
                    curr = k;
                }
            }
            for(int k = ind; k >=i+1;k--){
                s[k] =  s[k-1];
            }
            s[i] = (char)(curr + '0');
        }
        cout<<s<<endl;
    }
}

Solution 2 (runtime: 77 ms):
#include <bits/stdc++.h>

using namespace std;

void solve() {
    string s; cin >> s;
    for (int i = 0; i < s.size(); i++) {
        int best = s[i] - '0', pos = i;
        for (int j = i; j < min(i + 10, (int) s.size()); j++) {
            if (s[j] - '0' - (j - i) > best) {
                best = s[j] - '0' - (j - i);
                pos = j;
            }
        }
        while (pos > i) {
            swap(s[pos], s[pos - 1]);
            pos--;
        }
        s[i] = char(best + '0');
    }
    cout << s;
}

int main() {
    int TESTS = 1; cin >> TESTS;
    while (TESTS --> 0) {
        solve();
        cout << '\n';
    }
    return 0;
}
Problem: 2063B - Subsequence Update
Language: c++17 

Solution 1 (runtime: 92 ms):
#include<bits/stdc++.h>

#define ll long long

#define vi vector<int>

#define vvi vector<vector<int>>

#define vc vector<char>

#define fi for(int i=0;i<n;i++)

#define fit(a) for(auto it:a)

#define pb push_back

#define pf push_front

#define mi map<int,int>

#define umi unordered_map<int,int>

#define usi unordered_set<int>

#define maxheapint priority_queue<int>

#define maxheappair priority_queue<pair<int,int>>

#define minheapint priority_queue<int,vector<int>,greater<int>>

#define minheappair priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>

using namespace std;

void print(vector<int> &a){

    for(auto i:a) cout<<i<<" ";

    cout<<"\n";

}

int main() {

ios_base::sync_with_stdio(false);

cin.tie(NULL);

    ll t;

    cin>>t;

    while(t--){

        ll n,l,r;

        cin>>n>>l>>r;

        vector<ll> a(n);

        fi cin>>a[i];

        vector<ll> b(a.begin()+l-1,a.begin()+r);

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

        sort(b.begin(),b.end(),greater<ll>());

        vector<ll> left,right;

        ll ans1=ans,ans2=ans;

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

            left.push_back(a[i]);

        }

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

            right.push_back(a[i]);

        }

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

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

        vector<ll> left1,right1;

        ll szl=left.size(),szr=right.size();

        for(ll i=0;i<min(szl,r-l+1);i++) left1.push_back(left[i]);

        for(ll i=0;i<min(szr,r-l+1);i++) right1.push_back(right[i]);

        ll z=0,s=0;

        while(z<left1.size() && s<b.size() && left1[z]<b[s]){

            ans1+=left1[z++];

            ans1-=b[s++];

        }

        z=0,s=0;

        while(z<right1.size() && s<b.size() && right1[z]<b[s]){

            ans2+=right1[z++];

            ans2-=b[s++];

        }

        cout<<min(ans1,ans2)<<"\n";

    }   

}

Solution 2 (runtime: 78 ms):
#include <bits/stdc++.h>
using namespace std;

#define optimize() ios_base::sync_with_stdio(0); cin.tie(0)
#define ll long long
#define ff first
#define ss second
#define sz size()
#define pb push_back
#define vinput for(auto& x: v) cin >> x;
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define range(i,x,y) for(int i=x;i<y;i++)
#define back_range(i,x,y) for(int i=y-1;i>=x;i--)
#define mem(a,b) memset(a, b, sizeof(a) )

typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef vector<pii> vii;

// Debug..
#ifdef LOCAL
#include "debug2.h"
#else
#define dg(x...)
#endif

void Solve() {
    int n, l, r;
    cin >> n >> l >> r;
    vi v(n);
    vinput;
    l--;
    
    vi a = v;
    sort(a.begin(), a.begin()+r, greater<int>());
    ll sum_a = accumulate(a.begin()+l, a.begin()+r, 0ll);
    
    vi b = v;
    sort(b.begin()+l, b.end());
    ll sum_b = accumulate(b.begin()+l, b.begin()+r, 0ll);
    
    cout << min(sum_a, sum_b) << endl;
}

int main() {
    optimize();
    
    int t = 1;
    cin >> t;
    for(int i=1; i<=t; i++){
        //cout << "Case " << i << ": ";
        Solve();
    }
    
    return 0;
}