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

Solution 1 (runtime: 124 ms):
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

// פונקציה שמקטינה את סכום הקטע [l, r] על ידי החלפה עם חלק חיצוני
long long minimizeSegment(vector<int> segment, vector<int> external) {
    if (external.empty()) {
        return accumulate(segment.begin(), segment.end(), 0LL);
    }

    sort(segment.rbegin(), segment.rend());  // מיון יורד (הגדולים ראשונים)
    sort(external.begin(), external.end());  // מיון עולה (הקטנים ראשונים)

    int minSize = min(segment.size(), external.size());
    for (int i = 0; i < minSize; ++i) {
        if (segment[i] > external[i]) {
            swap(segment[i], external[i]);
        } else {
            break;
        }
    }

    return accumulate(segment.begin(), segment.end(), 0LL);
}

void solve() {
    int t;
    cin >> t;

    while (t--) {
        int n, l, r;
        cin >> n >> l >> r;
        vector<int> arr(n);

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

        vector<int> segment(arr.begin() + l - 1, arr.begin() + r);
        vector<int> prefix(arr.begin(), arr.begin() + (l - 1));
        vector<int> suffix(arr.begin() + r, arr.end());

        long long originalSum = accumulate(segment.begin(), segment.end(), 0LL);
        long long sumWithPrefix = prefix.empty() ? originalSum : minimizeSegment(segment, prefix);
        long long sumWithSuffix = suffix.empty() ? originalSum : minimizeSegment(segment, suffix);

        cout << min({originalSum, sumWithPrefix, sumWithSuffix}) << '\n';
    }
}

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

    solve();

    return 0;
}


Solution 2 (runtime: 92 ms):
#include <bits/stdc++.h>
using namespace std;
#define fastread() (ios_base::sync_with_stdio(false),cin.tie(NULL))
#define ll long long
#define sp " "
#define nl "\n"
 

#define ON(n,k)         ((n)| (1<<(k)))
#define OFF(n,k)        ((n)& (~(1<<(k))))
#define GetBit(n,k)     (((n)>> (k)) & 1 )
#define flibBit(n,k)    ((n)^(1<<(k)))
#define IsPowerOfTwo(n) (((n)&(n-1))==0)
#define IsOdd(n)        ((n) & 1 )


#define ONll(n,k)         ((n)| (1LL<<(k)))
#define OFFll(n,k)        ((n)& (~(1LL<<(k))))
#define GetBitll(n,k)     (((n)>> (k)) & 1LL )
#define flibBitll(n,k)    ((n)^(1LL<<(k)))
#define IsPowerOfTwoll(n) (((n)&(n-1))==0)
#define IsOddll(n)        ((n) & 1LL )


ll fast_power(ll base , ll pwr)
{
    if(pwr==0) return 1;
    ll p= fast_power(base, pwr/2);
    
    if(pwr%2 == 0) return p*p;

    return p*p*base;
}
ll sumtoN(ll n)
{
    return n*(n+1)/2;
}



const int N=1e5+5;

int n,m,u,v;
vector<int> adj[N];    
bool vis[N]; 
int depth[N];
int freq[N];



void DFS(int u,int dep)   
{
    vis[u]= true;
    depth[u]=dep;
    freq[dep]++;
    freq[dep]=freq[dep]%2;
    for(int v: adj[u])
    {
        if (!vis[v])
        {
            DFS(v,dep+1);
        }
    }

    
}

void solution()
{
    ll n,l,r;
    cin>>n>>l>>r;
    vector<int>v(n), seg , lp, rp;
    ll sumg=0;
    l--,r--;
    for(int i=0; i<n; i++)
    {
        cin>>v[i];
        if(i>=l && i<=r)
        {
            seg.push_back(v[i]);
            sumg+=v[i];
        }
        if(i<l)
        {
            lp.push_back(v[i]);
        }
        if(i>r) rp.push_back(v[i]);
    }
    sort(seg.begin(),seg.end(),greater<int>());
    sort(lp.begin(),lp.end());
    sort(rp.begin(),rp.end());

    ll lft=0,rght=0,currl=0,currr=0;
    for(int i=0; i<lp.size(); i++)
    {
        if(lp[i]<seg[currl])
        {
            lft+= (seg[currl]-lp[i]);
            currl++;
        }
        if(currl==seg.size()) break;
    }
    
    for(int i=0; i<rp.size(); i++)
    {
        if(rp[i]<seg[currr])
        {
            rght+=(seg[currr]-rp[i]);
            currr++;
        }
        if(currr==seg.size()) break;

    }
    ll ans=sumg-max(lft,rght);
   
    cout<<ans<<nl;

}


int main() {
    fastread();
 
    int t=1;
    cin>>t;
    while(t--) {
        solution();
    }
    
    return 0;
}