#include <iostream>

#include <numeric>

#include <algorithm>

#include <vector>

#include <ranges>

int main()

{

    std::size_t tc, n, l, r;

    std::cin >> tc;

    for(std::size_t i{0}; i<tc; ++i)

    {

        std::cin >> n >> l >> r;

        std::vector<unsigned long long> seq(n);

        for(auto& el : seq)

            std::cin >> el;

        std::vector<unsigned long long> left(seq.begin(), seq.begin()+r),

        right(seq.begin()+l-1, seq.end());

        std::ranges::sort(left);

        std::ranges::sort(right);

        std::cout << std::min(std::accumulate(left.begin(), left.begin()+r-l+1, 0ULL),

                     std::accumulate(right.begin(), right.begin()+r-l+1, 0ULL)) << "\n";

    }

}

#include <iostream>

#include <numeric>

#include <algorithm>

#include <vector>

#include <ranges>

int main()

{

    std::size_t tc, n, l, r;

    std::cin >> tc;

    for(std::size_t i{0}; i<tc; ++i)

    {

        std::cin >> n >> l >> r;

        std::vector<unsigned long long> seq(n);

        for(auto& el : seq)

            std::cin >> el;

        std::vector<unsigned long long> left(seq.begin(), seq.begin()+r),

        right(seq.begin()+l-1, seq.end());

        std::ranges::sort(left);

        std::ranges::sort(right);

        std::cout << std::min(std::accumulate(left.begin(), left.begin()+r-l+1, 0),

                     std::accumulate(right.begin(), right.begin()+r-l+1, 0)) << "\n";

    }

}

Answer: 1


#include<bits/stdc++.h>

using namespace std;

#define ll long long

#define ull unsigned long long

#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)

#define PII pair<int, int>

#define PLL pair<ll, ll>

#define lowbit(x) (x & -(x))

#define INF 0x3f3f3f3f

#define ls(p) p << 1

#define rs(p) p << 1 | 1

const double eps = 1e-6;

const double PI = acos(double(-1));

const ull base = 131;

const double C = 0.5772156649;

const int mod = 1000007;

const int N = 1e5 + 7;

ll a[N],b[N];

void solve(){

    int n, l, r;

    cin >> n >> l >> r;

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

        cin >> a[i];

        b[i] = a[i];

    }

    sort(b + 1, b + r + 1);

    ll ans = 0;

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

        ans += b[i - l + 1];

    }

    sort(a + l, a + n + 1);

    ll res = 0;

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

        res += a[i];

    }

    ans = min(ans, res);

    cout << ans << endl;

}

int main()

{

    IOS;

    int t=1;

    cin>>t;

    while(t--){

        solve();

    }

    return 0;

}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define ull unsigned long long
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define lowbit(x) (x & -(x))
#define INF 0x3f3f3f3f
#define ls(p) p << 1
#define rs(p) p << 1 | 1
const double eps = 1e-6;
const double PI = acos(double(-1));
const ull base = 131;
const double C = 0.5772156649;
const int mod = 1000007;
const int N = 2e5 + 7;
int w[N];
void solve()
{
    int n, q;
    cin >> n >> q;
    for(int i = 1; i <= n; i++){
        cin >> w[i];
    }

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

Answer: 1


#include <bits/stdc++.h>

using namespace std;

#define int long long
#define pb push_back
#define FORI(sti, n) for(int i=(sti); i<(n); i++)
#define FORJ(stj, n) for(int j=(stj); j<(n); j++)
#define FORK(stk, n) for(int k=(stk); k<(n); k++)
#define tvi(v) FORI(0, (v).size()) cerr << (v)[i] << " "; cerr << "\n";
#define tii(val) cerr <<  "(" << (val).first << ", " << (val).second << ")\n";
#define tvii(v) FORI(0, (v).size()) { tii((v)[i]) } cerr << "\n";
#define tvvi(v) FORI(0, (v).size()) { FORJ(0, (v)[i].size()){ cerr <<  v[i][j] << " "; } cerr << "\n"; }
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<bool> vbl;

void solve(){
	int n, l, r;
	cin >> n >> l >> r;
	vi v(n);
	for(auto &a: v) cin >> a;

	vi store, numbers;

	for(int i=0; i<n; i++){
		if(i>=(l-1) && i<=(r-1)){
			store.pb(v[i]);
		}
		else{
			numbers.pb(v[i]);
		}
	}
	sort(rall(store));
	sort(all(numbers));

	int ans=0, idx=0;
	for(int i=0; i<store.size(); i++){
		if(idx<numbers.size() && store[i]>numbers[idx]){
			store[i]=numbers[idx];
			idx++;
		}
		ans+=store[i];
	}

	cout << ans << "\n";
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

	int t; cin >> t; while(t--)
		solve();

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define pb push_back
#define FORI(sti, n) for(int i=(sti); i<(n); i++)
#define FORJ(stj, n) for(int j=(stj); j<(n); j++)
#define FORK(stk, n) for(int k=(stk); k<(n); k++)
#define tvi(v) FORI(0, (v).size()) cerr << (v)[i] << " "; cerr << "\n";
#define tii(val) cerr <<  "(" << (val).first << ", " << (val).second << ")\n";
#define tvii(v) FORI(0, (v).size()) { tii((v)[i]) } cerr << "\n";
#define tvvi(v) FORI(0, (v).size()) { FORJ(0, (v)[i].size()){ cerr <<  v[i][j] << " "; } cerr << "\n"; }
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<bool> vbl;

void solve(){
	int n, l, r;
	cin >> n >> l >> r;
	vi v(n);
	for(auto &a: v) cin >> a;

	vi store, numbers, pt1, pt2;
	int npt1=0;
	int npt2=0;

	for(int i=0; i<n; i++){
		if(i>=(l-1) && i<=(r-1)){
			store.pb(v[i]);
		}
		else{
			if(i<(l-1)){
				pt1.pb(v[i]); npt1+=v[i];
			}
			else{
				pt2.pb(v[i]); npt2+=v[i];
			}
		}
	}

	sort(rall(store));
	vi store2 = store;
	sort(all(pt1));
	sort(all(pt2));

	int ans1=0, ans2=0, idx1=0, idx2=0;
	for(int i=0; i<store.size(); i++){
		if(idx1<pt1.size() && store[i]>pt1[idx1]){
			store[i]=pt1[idx1];
			idx1++;
		}

		if(idx2<pt2.size() && store2[i]>pt2[idx2]){
			store2[i]=pt2[idx2];
			idx2++;
		}

		ans1+=store[i];
		ans2+=store2[i];
	}

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

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

	int t; cin >> t; while(t--)
		solve();

    return 0;
}

Answer: 2


#include<iostream>

#include<algorithm>

using namespace std;

int main()

{

   int t;

   cin>>t;

   while(t--)

   {

    int n,l,r;

    cin>>n>>l>>r;

    int a[n];

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

        cin>>a[i];

    sort(a+l-1,a+r);

    sort(a,a+l-1);

    sort(a+r,a+n);

    int start = 0 , mid = r-1 , end = r;

    int long long  ans_1 = 0 ,  ans_2 = 0;

    while(mid>=l-1)

    {

        if(start<l-1)

            ans_1+=min(a[start],a[mid]);

        else

            ans_1+=a[mid];

        if(end<n)

            ans_2+=min(a[end],a[mid]);

        else

            ans_2+=a[mid];

        start++;

        mid--;

        end++;

    }

    cout<<min(ans_1,ans_2)<<endl;

   }

}

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,l,r;
        cin>>n>>l>>r;
        int a[n];
        for(int i = 0 ; i < n ; i++)
            cin>>a[i];
        int x = r-l+1;
        int lim[x];
        int rem[n-x];
        int e = 0 , f = 0 ;

        for(int i = 0 ; i < n ; i++)
        {
            if(i+1>=l and i+1<=r)
            {
                lim[e] = a[i];
                e++;
            }

            else
            {
                rem[f] = a[i];
                f++;
            }
        }

        sort(lim,lim+x);
        sort(rem,rem+(n-x));

        int p = x-1 , q = 0;

        int res = 0 ;

        while(p>=0 and q<n-x)
        {

            res+=min(lim[p],rem[q]);
            p--;
            q++;
        }

        if(q>=n-x)
        {
            while(p>=0)
                res+=lim[p--];
        }

        cout<<res<<endl;

    }
}

Answer: 1


#include<iostream>

#include <cmath>

#include <bits/stdc++.h>

using namespace std;

int main(){

    int t;

    cin>>t;

    vector<int> ans;

    while(t--){

        int n,l,r;

        cin>>n>>l>>r;

        long long sum=0;

        vector<int> vec(n,0);

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

            cin>>vec[i];

        }

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

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

            sum+=vec[i];

        }

        ans.push_back(sum);

    }

    for(auto num:ans) cout<<num<<endl;

    return 0;

}

#include<iostream>
#include <cmath>
#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    vector<long long> ans;
    while(t--){
        int n,l,r,x;
        cin>>n>>l>>r;
        vector<int> vec(n,0);
        vector<int> help(n,0);
        for(int i=0;i<n;i++){
            cin>>x;
            vec[i]=x;
            help[i]=x;
        }
        sort(vec.begin(), vec.begin()+r);
        sort(help.begin()+(l-1), help.end());
        int i=0, j=l-1;
        long long sum1=0, sum2=0;
        while(i<=(r-l)) sum1+=vec[i++];
        while(j<r) sum2+=help[j++];
        ans.push_back(min(sum1, sum2));

    }
    for(auto num:ans) cout<<num<<endl;
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;

int main(){

    int t;
    cin >> t;

    while(t--){
        int n,l,r;
        cin >> n >> l >>r;
        vector<int> v(n, 0);
        for (int i = 0; i < n;i++){
        cin >> v[i];
        }
        vector<int> v2 = v;
        vector<int> v3 = v;
        sort(v2.begin(), v2.begin() + r );
        sort(v3.begin() +l-1, v3.end()  );
        long long sum1 = 0;
        for (int i = 0; i < (r - l + 1);i++){
            sum1 += v2[i];
        }
        long long sum2 = 0;
        for (int i = l-1; i < r;i++){
            sum2 += v3[i];
        }

        cout << min(sum1,sum2) << endl;
    }
}

#include <bits/stdc++.h>
using namespace std;

int main(){

    int t;
    cin >> t;

    while(t--){
        int n,l,r;
        cin >> n >> l >>r;
        vector<int> v(n, 0);
        for (int i = 0; i < n;i++){
        cin >> v[i];
        }
        vector<int> v2 = v;
        vector<int> v3 = v;
        sort(v2.begin(), v2.begin() + (r+1) );
        sort(v3.begin() +l-1, v3.end()  );
        int sum1 = 0;
        for (int i = 0; i < (r - l + 1);i++){
            sum1 += v2[i];
        }
        int sum2 = 0;
        for (int i = l-1; i < r;i++){
            sum2 += v3[i];
        }

        cout << min(sum1,sum2) << endl;
    }
}

Answer: 1


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int t, n, l, r;
    cin >> t;
    for (int i = 0; i < t; i++) {
        vector<int> lArr, rArr;
        cin >> n >> l >> r;
        for (int j = 1; j <= n; j++) {
            int a;
            cin >> a;
            if (j <= r) lArr.push_back(a);
            if (j >= l) rArr.push_back(a);
        }

        sort(lArr.begin(), lArr.end());
        sort(rArr.begin(), rArr.end());
        long long max1 = 0, max2 = 0;

        for (int k = 0; k < r - l + 1; k++) {
            max1 = (max1 + lArr[k] < 0) ? LLONG_MAX : max1 + lArr[k];
            max2 = (max2 + rArr[k] < 0) ? LLONG_MAX : max2 + rArr[k];
        }
        cout << min(max1, max2) << "\n";
    }
}

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int t, n, l, r;
    cin >> t;
    for (int i = 0; i < t; i++) {
        vector<int> lArr, rArr;
        cin >> n >> l >> r;
        for (int j = 1; j <= n; j++) {
            int a;
            cin >> a;
            if (j <= r) lArr.push_back(a);
            if (j >= l) rArr.push_back(a);
        }

        sort(lArr.begin(), lArr.end());
        sort(rArr.begin(), rArr.end());
        int max1 = 0, max2 = 0;

        for (int k = 0; k < r - l + 1; k++) {
            max1 += lArr[k];
            max2 += rArr[k];
        }
        cout << min(max1, max2) << "\n";
    }
}

Answer: 1


//******************************************************//
//*                AUTHOR: NAMAN OSTWAL                *//
//******************************************************//
//______________________________________________________________________________________________________//
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
long long int INF = 1e9+7;
long long int mod = 1e9+7;
const int N = 1e6+7;
vector<bool> isprime(N, true);

inline void fast_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

void print(vector<ll>&v) { for(auto it : v) cout<<it<<' '; }
void sort(vector < ll > &v) { sort(v.begin() , v.end()); }
void rsort(vector < ll > &v) { sort(v.begin() , v.end(), greater<int>()); }
ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll add(ll a, ll b) { return ((a % mod) + (b % mod) + mod) % mod; }
ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
ll lcm(ll a, ll b) { return ((a * b) / (__gcd(a,b))); }

ll bin_expo(ll a, ll b) {
    a %= mod;
    ll ans = 1;
    while (b > 0) {
        if (b & 1) {
            ans = (ans * a) % mod;
        }
        a = (a * a) % mod;
        b >>= 1;
    }
    return ans % mod;
}

void sieve_of_eratosthenes() {
    isprime[0] = isprime[1] = false;
    for(int i = 2; i<=N; i++) {
        if(isprime[i] == true) {
            for(int j = 2*i; j<=N; j+=i) {
                isprime[j] = false;
            }
        }
    }
}

bool ispal(string s) {
    int n = s.size();
    for (int i = 0; i < n / 2; ++i) {
        if (s[i] != s[n - i - 1]) {
            return false;
        }
    }
    return true;
}

void DFSUtil(ll node, vector<vector<ll>>& adj, vector<bool>& visited, vector<ll>& dfs) {
    visited[node] = true;
    dfs.push_back(node);
    for (ll neighbor : adj[node]) {
        if (!visited[neighbor]) {
            DFSUtil(neighbor, adj, visited, dfs);
        }
    }
}

void dfs(vector<vector<ll>>& adj, ll n, vector<ll>& dfs) {
    vector<bool> visited(n, false);
    for (ll i = 0; i < n; ++i) {
        if (!visited[i]) {
            DFSUtil(i, adj, visited, dfs);
        }
    }
}

void bfs(vector<vector<ll>>& adj, ll n, vector<ll>& bfs) {
    queue<ll> q;
    vector<bool> visited(n, false);
    visited[0] = true;
    q.push(0);
    while (!q.empty()) {
        ll node = q.front();
        q.pop();
        bfs.push_back(node);
        for (auto it : adj[node]) {
            if (!visited[it]) {
                visited[it] = true;
                q.push(it);
            }
        }
    }
}

int main() {
    fast_io();
    int t;
    cin >> t;
    while (t--) {
        ll n, l, r;
    	cin >> n >> l >> r;
    	vector<ll> a(n);
    	for (int i = 0; i < n; i++) cin >> a[i];
    	l--, r--;
    	ll len = r - l + 1;
    	vector<ll> pre(len + 1, INF), suf(len + 1, INF), mid(len + 1, INF);
    	sort(a.begin(), a.begin() + l);
    	sort(a.begin() + l, a.begin() + r + 1);
    	reverse(a.begin() + l, a.begin() + r + 1);
    	sort(a.begin() + r + 1, a.end());
    	suf[0] = pre[0] = mid[0] = 0;
    	for (int i = 0; i < min(len, l); i++) {
    		pre[i + 1] = pre[i] + a[i];
    	}
    	for(int i = l; i <= r; i++) {
    		mid[i - l + 1] = mid[i - l] + a[i];
    	}
    	for(int i = r + 1; i < min(r + 1 + len, n); i++) {
    		suf[i - r] = suf[i - r - 1] + a[i];
    	}
    	ll ans = 0;
    	for(int i = 1; i <= len; i++) {
    		ans = max(ans, mid[i] - suf[i]);
    		ans = max(ans, mid[i] - pre[i]);
    	}
    	ans = -ans;
    	for(int i = l; i <= r; i++) ans += a[i];
    	cout << ans << "\n";
    }
    return 0;
}

//******************************************************//
//*                AUTHOR: NAMAN OSTWAL                *//
//******************************************************//
//______________________________________________________________________________________________________//
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
long long int INF = 1e18;
long long int mod = 1e9+7;
const int N = 1e6+7;
vector<bool> isprime(N, true);

inline void fast_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

void print(vector<ll>&v) { for(auto it : v) cout<<it<<' '; }
void sort(vector < ll > &v) { sort(v.begin() , v.end()); }
void rsort(vector < ll > &v) { sort(v.begin() , v.end(), greater<int>()); }
ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll add(ll a, ll b) { return ((a % mod) + (b % mod) + mod) % mod; }
ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
ll lcm(ll a, ll b) { return ((a * b) / (__gcd(a,b))); }

ll bin_expo(ll a, ll b) {
    a %= mod;
    ll ans = 1;
    while (b > 0) {
        if (b & 1) {
            ans = (ans * a) % mod;
        }
        a = (a * a) % mod;
        b >>= 1;
    }
    return ans % mod;
}

void sieve_of_eratosthenes() {
    isprime[0] = isprime[1] = false;
    for(int i = 2; i<=N; i++) {
        if(isprime[i] == true) {
            for(int j = 2*i; j<=N; j+=i) {
                isprime[j] = false;
            }
        }
    }
}

bool ispal(string s) {
    int n = s.size();
    for (int i = 0; i < n / 2; ++i) {
        if (s[i] != s[n - i - 1]) {
            return false;
        }
    }
    return true;
}

void DFSUtil(ll node, vector<vector<ll>>& adj, vector<bool>& visited, vector<ll>& dfs) {
    visited[node] = true;
    dfs.push_back(node);
    for (ll neighbor : adj[node]) {
        if (!visited[neighbor]) {
            DFSUtil(neighbor, adj, visited, dfs);
        }
    }
}

void dfs(vector<vector<ll>>& adj, ll n, vector<ll>& dfs) {
    vector<bool> visited(n, false);
    for (ll i = 0; i < n; ++i) {
        if (!visited[i]) {
            DFSUtil(i, adj, visited, dfs);
        }
    }
}

void bfs(vector<vector<ll>>& adj, ll n, vector<ll>& bfs) {
    queue<ll> q;
    vector<bool> visited(n, false);
    visited[0] = true;
    q.push(0);
    while (!q.empty()) {
        ll node = q.front();
        q.pop();
        bfs.push_back(node);
        for (auto it : adj[node]) {
            if (!visited[it]) {
                visited[it] = true;
                q.push(it);
            }
        }
    }
}

int main() {
    fast_io();
    int t;
    cin >> t;
    while (t--) {
        ll n, l, r;
    	cin >> n >> l >> r;
    	vector<ll> a(n);
    	for (int i = 0; i < n; i++) cin >> a[i];
    	l--, r--;
    	ll len = r - l + 1;
    	vector<ll> pre(len + 1, INF), suf(len + 1, INF), mid(len + 1, INF);
    	sort(a.begin(), a.begin() + l);
    	sort(a.begin() + l, a.begin() + r + 1);
    	reverse(a.begin() + l, a.begin() + r + 1);
    	sort(a.begin() + r + 1, a.end());
    	suf[0] = pre[0] = mid[0] = 0;
    	for (int i = 0; i < min(len, l); i++) {
    		pre[i + 1] = pre[i] + a[i];
    	}
    	for(int i = l; i <= r; i++) {
    		mid[i - l + 1] = mid[i - l] + a[i];
    	}
    	for(int i = r + 1; i < min(r + 1 + len, n); i++) {
    		suf[i - r] = suf[i - r - 1] + a[i];
    	}
    	ll ans = 0;
    	for(int i = 1; i <= len; i++) {
    		ans = max(ans, mid[i] - suf[i]);
    		ans = max(ans, mid[i] - pre[i]);
    	}
    	ans = -ans;
    	for(int i = l; i <= r; i++) ans += a[i];
    	cout << ans << "\n";
    }
    return 0;
}

Answer: 2


/**

 *  author: xing huan liu kong

 *  created: 2025-02-13 10:22:59

 *

 *  即使这个图论(world)大的无边无际

 *  我也会深度优先搜索(dfs)找到你..

**/

#include <bits/stdc++.h>

#define ll long long

#define endl '\n'

#define point(x) fixed<<setprecision(x)

using namespace std;

#define int long long

typedef pair<int, int> PII;

typedef array<int, 3> arr;

void solve()

{

    int n, l, r;

    cin >> n >> l >> r;

    vector<int> a(n + 1, 0), b, c, d;

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

        cin >> a[i];

        if (i < l) b.push_back(a[i]);

        else if (i > r) c.push_back(a[i]);

        else d.push_back(a[i]);

    }

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

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

    sort(d.rbegin(), d.rend());

    int sum = accumulate(d.begin(), d.end(), 0ll);

    int res = sum;

    for (int i = 0, ts = 0; i < (int)b.size() && i < (int)d.size(); i ++ ) {

        ts += b[i] - d[i];

        res = min (res, sum + ts);

    }

    for (int i = 0, ts = 0; i < (int)c.size() && i < (int)d.size(); i ++ ) {

        ts += c[i] - d[i];

        res = min (res, sum + ts);

    }

    cout << res << endl;

}

signed main(void)

{

    ios_base::sync_with_stdio(0);

    cin.tie(0), cout.tie(0);

    int t = 1;

    cin >> t;

    while(t -- ){

        solve();

    }

    return 0;

}

/**
 *  author: xing huan liu kong
 *  created: 2025-02-13 10:22:59
 *
 *  即使这个图论(world)大的无边无际
 *  我也会深度优先搜索(dfs)找到你..
**/
#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
#define point(x) fixed<<setprecision(x)
using namespace std;

#define int long long
typedef pair<int, int> PII;
typedef array<int, 3> arr;

void solve()
{
    int n, l, r;
    cin >> n >> l >> r;

    vector<int> a(n + 1, 0), b, c, d;
    for (int i = 1; i <= n; i ++ ) {
        cin >> a[i];
        if (i < l) b.push_back(a[i]);
        else if (i > r) c.push_back(a[i]);
        else d.push_back(a[i]);
    }

    sort(b.begin(), b.end());
    sort(c.begin(), c.end());
    sort(d.rbegin(), d.rend());

    int sum = accumulate(d.begin(), d.end(), 0);
    int res = sum;
    for (int i = 0, ts = 0; i < (int)b.size() && i < (int)d.size(); i ++ ) {
        ts += b[i] - d[i];
        res = min (res, sum + ts);
    }
    for (int i = 0, ts = 0; i < (int)c.size() && i < (int)d.size(); i ++ ) {
        ts += c[i] - d[i];
        res = min (res, sum + ts);
    }

    cout << res << endl;

}

signed main(void)
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    int t = 1;
    cin >> t;

    while(t -- ){
        solve();
    }

    return 0;
}

Answer: 1


/**

 *  Author: Abhishek Jha

**/

#include <bits/stdc++.h>

#define int long long

using namespace std;

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

#define rall(x) x.rbegin(), x.rend()

#define sz(x) (int)(x).size()

#define ft first

#define sd second

#define pb push_back

typedef vector<int> vi;typedef vector<vi> vvi;typedef pair<int, int> pii;typedef vector<pii> vii;typedef vector<vii> vvii;typedef long long ll;

const int MOD = 1e9+7;const int INFLL = 1e18+5;const int INF = 1e9;const int maxN = 101;const int maxK = 8001;

void printbit(int x, int len) {string s="\n";while(len--){s=((x%2)?'1':'0')+s;x/=2;} cout<<s;}

template<class T>

istream& operator>>(istream&in, vector<T>&v){for (T& x:v) in>>x;return in;}

template<class T>

ostream& operator<<(ostream&out, vector<T>&v){for (T& x:v) out<<x<<' ';cout<<'\n';return out;}

/*---------------------------------------------------------------------------------*/

ll countSetBits(ll n){return __builtin_popcount(n);}

ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);}

ll lcm(ll a, ll b){return (a/gcd(a,b)*b);}

bool isPrime(ll n) {if(n==2)    return true;if(n<2)return false;if(n%2 == 0) return false;for(int i=3; i*i<= n; i+= 2)if(n%i==0) return false;return true;}

ll factorial(ll n){return (n==1 || n==0) ? 1: n * factorial(n - 1);}

int smallestDivisor(int n){if (n % 2 == 0) return 2;for (int i = 3; i * i <= n; i += 2) {if (n % i == 0) return i;} return n;}

int largestDivisor(int n){return n/smallestDivisor(n);}

ll nCr(ll n, ll r){if(r>n)return 0;long long p = 1, k = 1;if(n - r < r)r = n - r;if (r != 0) { while (r) {p *= n;k *= r;ll m = __gcd(p, k);p /= m;k /= m;n--;r--;}}else  p = 1;return p;}

/*------------------------------------------------------------------------------------------------------------------*/

void solve(){

    ll n,l,r;

    cin>>n>>l>>r;

    vi a(n);

    cin>>a;

    vi b;

    vi c;

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

        b.push_back(a[i]);

    }

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

        c.push_back(a[i]);

    }

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

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

    ll bans = 0;

    ll cans = 0;

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

        bans += b[i];

        cans += c[i];

    }

    cout<<min(bans,cans)<<endl;

}

/*------------------------------------------------------------------------------------------------------------------*/

signed main() {

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);

    cout.tie(nullptr);

    int t=1, i = 1;

    cin>>t;

    while(t--){

        solve();

    }

    return 0;

}

/**

 *  Author: Abhishek Jha

**/

#include <bits/stdc++.h>

#define int long long

using namespace std;

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

#define rall(x) x.rbegin(), x.rend()

#define sz(x) (int)(x).size()

#define ft first

#define sd second

#define pb push_back

typedef vector<int> vi;typedef vector<vi> vvi;typedef pair<int, int> pii;typedef vector<pii> vii;typedef vector<vii> vvii;typedef long long ll;

const int MOD = 1e9+7;const int INFLL = 1e18+5;const int INF = 1e9;const int maxN = 101;const int maxK = 8001;

void printbit(int x, int len) {string s="\n";while(len--){s=((x%2)?'1':'0')+s;x/=2;} cout<<s;}

template<class T>

istream& operator>>(istream&in, vector<T>&v){for (T& x:v) in>>x;return in;}

template<class T>

ostream& operator<<(ostream&out, vector<T>&v){for (T& x:v) out<<x<<' ';cout<<'\n';return out;}

/*---------------------------------------------------------------------------------*/

ll countSetBits(ll n){return __builtin_popcount(n);}

ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);}

ll lcm(ll a, ll b){return (a/gcd(a,b)*b);}

bool isPrime(ll n) {if(n==2)    return true;if(n<2)return false;if(n%2 == 0) return false;for(int i=3; i*i<= n; i+= 2)if(n%i==0) return false;return true;}

ll factorial(ll n){return (n==1 || n==0) ? 1: n * factorial(n - 1);}

int smallestDivisor(int n){if (n % 2 == 0) return 2;for (int i = 3; i * i <= n; i += 2) {if (n % i == 0) return i;} return n;}

int largestDivisor(int n){return n/smallestDivisor(n);}

ll nCr(ll n, ll r){if(r>n)return 0;long long p = 1, k = 1;if(n - r < r)r = n - r;if (r != 0) { while (r) {p *= n;k *= r;ll m = __gcd(p, k);p /= m;k /= m;n--;r--;}}else  p = 1;return p;}

/*------------------------------------------------------------------------------------------------------------------*/

void solve(){

    ll n,l,r;

    cin>>n>>l>>r;

    vi a(n);

    cin>>a;

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

    ll ans = 0;

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

        ans += a[i];

    }

    cout<<ans<<endl;

}

/*------------------------------------------------------------------------------------------------------------------*/

signed main() {

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);

    cout.tie(nullptr);

    int t=1, i = 1;

    cin>>t;

    while(t--){

        solve();

    }

    return 0;

}

Answer: 1


#include <iostream>
#include <bits/stdc++.h>
using namespace std;

// Speed
#define Code ios_base::sync_with_stdio(false);
#define By cin.tie(NULL);
#define Asquare cout.tie(NULL);

// Aliases
using ll = long long;
using lld = long double;
using ull = unsigned long long;

// Constants
const lld pi = 3.141592653589793238;
const ll INF = LONG_LONG_MAX;
const ll mod = 1e9 + 7;

// TypeDefs
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef unordered_map<ll, ll> umll;
typedef map<ll, ll> mll;

// Macros
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define fl(i, n) for (int i = 0; i < n; i++)
#define rl(i, m, n) for (int i = n; i >= m; i--)
#define py cout << "YES\n";
#define pn cout << "NO\n";

// Operator Overloads
template <typename T1, typename T2>
istream &operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); }
template <typename T>
istream &operator>>(istream &istream, vector<T> &v) { for (auto &it : v) cin >> it; return istream; }
template <typename T1, typename T2>
ostream &operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << " " << p.second); }
template <typename T>
ostream &operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) cout << it << " "; return ostream; }

// Utility Functions
template <typename T>
void print(T &&t) { cout << t << "\n"; }
void printarr(ll arr[], ll n) { fl(i, n) cout << arr[i] << " "; cout << "\n"; }
template <typename T>
void printvec(vector<T> v) { ll n = v.size(); fl(i, n) cout << v[i] << " "; cout << "\n"; }
template <typename T>
ll sumvec(vector<T> v) { ll n = v.size(); ll s = 0; fl(i, n) s += v[i]; return s; }

// Mathematical Functions
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b) * b); }
ll moduloMultiplication(ll a, ll b, ll mod) { ll res = 0; a %= mod; while (b) { if (b & 1) res = (res + a) % mod; b >>= 1; } return res; }
ll powermod(ll x, ll y, ll p) { ll res = 1; x %= p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y >>= 1; x = (x * x) % p; } return res; }

// Code
void asquare() {
        ll n, l, r;
        cin >> n >> l >> r;
        vll arr(n);
        fl(i,n) cin >>arr[i];
        sort(arr.begin() + (l - 1), arr.end());
        ll sum1 = accumulate(arr.begin() + (l - 1), arr.begin() + r, 0);

        sort(arr.begin(), arr.begin() + r, greater<ll>());
        ll sum2 = accumulate(arr.begin() + (l - 1), arr.begin() + r, 0);

        cout << min(sum1, sum2) << "\n";
}

// Main
int main() {
    Code By Asquare
    ll t;
    cin >> t;
    fl(i, t) {
        asquare();
    }
    return 0;
}
// End

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

// Speed
#define Code ios_base::sync_with_stdio(false);
#define By cin.tie(NULL);
#define Asquare cout.tie(NULL);

// Aliases
using ll = long long;
using lld = long double;
using ull = unsigned long long;

// Constants
const lld pi = 3.141592653589793238;
const ll INF = LONG_LONG_MAX;
const ll mod = 1e9 + 7;

// TypeDefs
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef unordered_map<ll, ll> umll;
typedef map<ll, ll> mll;

// Macros
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define fl(i, n) for (int i = 0; i < n; i++)
#define rl(i, m, n) for (int i = n; i >= m; i--)
#define py cout << "YES\n";
#define pn cout << "NO\n";

// Operator Overloads
template <typename T1, typename T2>
istream &operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); }
template <typename T>
istream &operator>>(istream &istream, vector<T> &v) { for (auto &it : v) cin >> it; return istream; }
template <typename T1, typename T2>
ostream &operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << " " << p.second); }
template <typename T>
ostream &operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) cout << it << " "; return ostream; }

// Utility Functions
template <typename T>
void print(T &&t) { cout << t << "\n"; }
void printarr(ll arr[], ll n) { fl(i, n) cout << arr[i] << " "; cout << "\n"; }
template <typename T>
void printvec(vector<T> v) { ll n = v.size(); fl(i, n) cout << v[i] << " "; cout << "\n"; }
template <typename T>
ll sumvec(vector<T> v) { ll n = v.size(); ll s = 0; fl(i, n) s += v[i]; return s; }

// Mathematical Functions
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b) * b); }
ll moduloMultiplication(ll a, ll b, ll mod) { ll res = 0; a %= mod; while (b) { if (b & 1) res = (res + a) % mod; b >>= 1; } return res; }
ll powermod(ll x, ll y, ll p) { ll res = 1; x %= p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y >>= 1; x = (x * x) % p; } return res; }

// Code
void asquare() {
        ll n, l, r;
        cin >> n >> l >> r;
        l--;
        vll arr(n);
        fl(i,n) cin>>arr[i];

        vll brr;
        fl(i,l)  brr.push_back(arr[i]);

        vll sp;
        for (ll i = l; i < n; i++) {
            sp.push_back(arr[i]);
        }
        sort(sp.begin(), sp.end());
        for (ll val : sp) {
            brr.push_back(val);
        }

        vll crr;
        fl(i,r) crr.push_back(arr[i]);

        sort(crr.begin(), crr.end(), greater<ll>());
        for (ll i = r; i < n; i++) {
            crr.push_back(arr[i]);
        }

        ll sum_brr = 0, sum_crr = 0;
        for (ll i = l; i < r; i++) {
            sum_brr += brr[i];
            sum_crr += crr[i];
        }

        cout << min(sum_brr, sum_crr) << "\n";
}

// Main
int main() {
    Code By Asquare
    ll t;
    cin >> t;
    fl(i, t) {
        asquare();
    }
    return 0;
}
// End

Answer: 2


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main {

    //------------------------------------------main--------------------------------------------------//
    public static void main(String[] args) throws java.lang.Exception {
        OutputStream outputStream = System.out;
        PrintWriter out = new PrintWriter(outputStream);
        FastReader sc = new FastReader();
        Task s = new Task(sc, out);
        s.solve();
        out.close();
        // your code goes here"
    }
}

class FastReader {

    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    public int[] readIntArray(int n) throws IOException {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextInt();
        }
        return arr;
    }

    public long[] readLongArray(int n) throws IOException {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextLong();
        }
        return arr;
    }
}

class Task {

    PrintWriter out;
    FastReader sc;

    Task(FastReader sc, PrintWriter out) {
        this.sc = sc;
        this.out = out;
    }
    //----------------------------------------------inputfunction------------------------------------------//

    String st() {
        return sc.nextLine();
    }

    int i() {
        return sc.nextInt();
    }

    long l() {
        return sc.nextLong();
    }

    double d() {
        return sc.nextDouble();
    }

    //---------------------------------------------absolute----------------------------------------------//
    public long abs(long a) {
        return a < 0 ? -a : a;
    }

    public int abs(int a) {
        return a < 0 ? -a : a;
    }

    //-------------------------------------------max--------------------------------------------------//
    public int max(int a, int b) {
        return a >= b ? a : b;
    }

    public long max(long a, long b) {
        return a >= b ? a : b;
    }

    public int max(int a, int b, int c) {
        return Math.max(a, Math.max(b, c));
    }

    public long max(long a, long b, long c) {
        return Math.max(a, Math.max(b, c));
    }

    //------------------------------------------min---------------------------------------------------//
    public int min(int a, int b) {
        return a <= b ? a : b;
    }

    public long min(long a, long b) {
        return a <= b ? a : b;
    }

    public int min(int a, int b, int c) {
        return Math.min(a, Math.min(b, c));
    }

    public long min(long a, long b, long c) {
        return Math.min(a, Math.min(b, c));
    }

    //----------------------------------------sortingfunction---------------------------------------------//
    private void sort(int[] arr) {
        int n = arr.length;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list);
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void sort(char[] arr) {
        int n = arr.length;
        List<Character> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list);
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void rsort(char[] arr) {
        int n = arr.length;
        List<Character> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list, Collections.reverseOrder());
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void rsort(int[] arr) {
        int n = arr.length;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list, Collections.reverseOrder());
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void sort(long[] arr) {
        int n = arr.length;
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list);
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void rsort(long[] arr) {
        int n = arr.length;
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list, Collections.reverseOrder());
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }
    //---------------------------------------power----------------------------------------------------//

    public long pow(long a, int b) {
        long ans = 1L;
        while (b > 0) {
            if ((b & 1) > 0) {
                ans *= a;
            }
            a *= a;
            b >>= 1;
        }
        return b;
    }
    //----------------------------------------binary_exponentiation-----------------------------------//

    public long exp(long a, long b, long mod) {
        long ans = 1L;
        while (b > 0) {
            if ((b & 1) > 0) {
                ans = (ans * a) % mod;
            }
            a = (a * a) % mod;
            b >>= 1;
        }
        return ans;
    }

    //---------------------------------------helpingmethods-------------------------------------------//
    //---------------------------------------main-----------------------------------------------------//
    int t, n;

    public void solve() throws IOException {
        int T = sc.nextInt();
        for(int  t= 1;t<=T;t++) {
            int n = i(), l  = i(),  r= i();
            List<Integer> x = new ArrayList<>(), y = new ArrayList<>(), z = new ArrayList<>();
            for(int i= 1;i<l;i++) x.add(i());
            for(int i  = l ;i<=r;i++) y.add(i());
            for(int i = r+1;i<=n;i++) z.add(i());
            long ans = 0, sum= 0;
            Collections.sort(x);
            Collections.sort(y);
            Collections.sort(z);
            Collections.reverse(y);
            for(int e: y) sum += e;
            long temp = ans = sum;
            for(int i = 0;i<min(y.size(), x.size());i++){
                if(x.get(i)>=y.get(i)) break;
                temp += x.get(i) - y.get(i);
            }
            ans = min(ans, temp);
            temp= sum;
            for(int i = 0;i<min(y.size(), z.size());i++){
                if(z.get(i)>=y.get(i)) break;
                temp += z.get(i) - y.get(i);
            }
            ans = min(temp , ans );
            out.print(ans);
            out.print("\n");
        }
    }
}

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main {

    //------------------------------------------main--------------------------------------------------//
    public static void main(String[] args) throws java.lang.Exception {
        OutputStream outputStream = System.out;
        PrintWriter out = new PrintWriter(outputStream);
        FastReader sc = new FastReader();
        Task s = new Task(sc, out);
        s.solve();
        out.close();
        // your code goes here"
    }
}

class FastReader {

    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    public int[] readIntArray(int n) throws IOException {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextInt();
        }
        return arr;
    }

    public long[] readLongArray(int n) throws IOException {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = nextLong();
        }
        return arr;
    }
}

class Task {

    PrintWriter out;
    FastReader sc;

    Task(FastReader sc, PrintWriter out) {
        this.sc = sc;
        this.out = out;
    }
    //----------------------------------------------inputfunction------------------------------------------//

    String st() {
        return sc.nextLine();
    }

    int i() {
        return sc.nextInt();
    }

    long l() {
        return sc.nextLong();
    }

    double d() {
        return sc.nextDouble();
    }

    //---------------------------------------------absolute----------------------------------------------//
    public long abs(long a) {
        return a < 0 ? -a : a;
    }

    public int abs(int a) {
        return a < 0 ? -a : a;
    }

    //-------------------------------------------max--------------------------------------------------//
    public int max(int a, int b) {
        return a >= b ? a : b;
    }

    public long max(long a, long b) {
        return a >= b ? a : b;
    }

    public int max(int a, int b, int c) {
        return Math.max(a, Math.max(b, c));
    }

    public long max(long a, long b, long c) {
        return Math.max(a, Math.max(b, c));
    }

    //------------------------------------------min---------------------------------------------------//
    public int min(int a, int b) {
        return a <= b ? a : b;
    }

    public long min(long a, long b) {
        return a <= b ? a : b;
    }

    public int min(int a, int b, int c) {
        return Math.min(a, Math.min(b, c));
    }

    public long min(long a, long b, long c) {
        return Math.min(a, Math.min(b, c));
    }

    //----------------------------------------sortingfunction---------------------------------------------//
    private void sort(int[] arr) {
        int n = arr.length;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list);
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void sort(char[] arr) {
        int n = arr.length;
        List<Character> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list);
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void rsort(char[] arr) {
        int n = arr.length;
        List<Character> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list, Collections.reverseOrder());
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void rsort(int[] arr) {
        int n = arr.length;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list, Collections.reverseOrder());
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void sort(long[] arr) {
        int n = arr.length;
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list);
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }

    private void rsort(long[] arr) {
        int n = arr.length;
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(arr[i]);
        }
        Collections.sort(list, Collections.reverseOrder());
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
    }
    //---------------------------------------power----------------------------------------------------//

    public long pow(long a, int b) {
        long ans = 1L;
        while (b > 0) {
            if ((b & 1) > 0) {
                ans *= a;
            }
            a *= a;
            b >>= 1;
        }
        return b;
    }
    //----------------------------------------binary_exponentiation-----------------------------------//

    public long exp(long a, long b, long mod) {
        long ans = 1L;
        while (b > 0) {
            if ((b & 1) > 0) {
                ans = (ans * a) % mod;
            }
            a = (a * a) % mod;
            b >>= 1;
        }
        return ans;
    }

    //---------------------------------------helpingmethods-------------------------------------------//
    //---------------------------------------main-----------------------------------------------------//
    int t, n;

    public void solve() throws IOException {
        int T = sc.nextInt();
        for(int  t= 1;t<=T;t++) {
            int n = i(), l  = i(),  r= i();
            if(t==80){
            for(int i = 1;i<=n;i++) out.print(i()+',');
            return;
            }
            List<Integer> x = new ArrayList<>(), y = new ArrayList<>(), z = new ArrayList<>();
            for(int i= 1;i<l;i++) x.add(i());
            for(int i  = l ;i<=r;i++) y.add(i());
            for(int i = r+1;i<=n;i++) z.add(i());
            long ans = 0;
            Collections.sort(x);
            Collections.sort(y);
            Collections.sort(z);
            Collections.reverse(y);
            for(int e: y) ans += e;
            long temp = ans;
            for(int i = 0;i<min(y.size(), x.size());i++){
                if(x.get(i)>=y.get(i)) break;
                temp += x.get(i) - y.get(i);
            }
            ans = min(ans, temp);
            temp= ans;
            for(int i = 0;i<min(y.size(), z.size());i++){
                if(z.get(i)>=y.get(i)) break;
                temp += z.get(i) - y.get(i);
            }
            ans = min(temp , ans );
            out.print(ans);
            out.print("\n");
        }
    }
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef vector<vector<long long> > vvll;
typedef vector<vector<string> > vvs;
typedef vector<vector<double> > vvd;
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (a); i >= (b); i--)
// #define FOR2D(i, j, n, m) for (int i = 0; i < (n); i++) for (int j = 0; j < (m); j++)
#define FOR2D(i, j, start_i, end_i, start_j, end_j) \
    for (int i = (start_i); i < (end_i); ++i)       \
        for (int j = (start_j); j < (end_j); ++j)
#define FORE(item, container) for (auto &item : container)
#define FORSTEP(i, a, b, step) for (int i = (a); i < (b); i += (step))
#define NL std::cout << "\n"
#define MOD 1000000007

void solve()
{
    int n, l, r;
    cin>>n>>l>>r;
    vll arr(n);
    FOR(i, 0, n) {
        cin>>arr[i];
    }
    priority_queue<ll> pq;
    priority_queue<ll, vector<ll>, greater<ll> > a, b;
    FOR(i, l-1, r)
        pq.push(arr[i]);
    FOR(i, 0, l-1)
        a.push(arr[i]);
    FOR(i, r, n)
        b.push(arr[i]);

    ll sum = 0LL;
    ll minSum = LLONG_MAX;
    while(!pq.empty()) {
        if(a.empty()) {
            sum += pq.top();
        }
        else {
            sum += min(a.top(), pq.top());
            a.pop();
        }
        pq.pop();
    }
    minSum = min(minSum, sum);

    FOR(i, l-1, r)
        pq.push(arr[i]);

    sum = 0LL;
    while(!pq.empty()) {
        if(b.empty()) {
            sum += pq.top();
        }
        else {
            sum += min(b.top(), pq.top());
            b.pop();
        }
        pq.pop();
    }
    minSum = min(minSum, sum);

    cout << minSum;
    NL;
}

int main()
{
    // #ifndef ONLINE_JUDGE
    //   freopen("input.txt", "r", stdin);
    //   freopen("output.txt", "w", stdout);
    // #endif
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vector<int> > vvi;
typedef vector<vector<long long> > vvll;
typedef vector<vector<string> > vvs;
typedef vector<vector<double> > vvd;
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (a); i >= (b); i--)
// #define FOR2D(i, j, n, m) for (int i = 0; i < (n); i++) for (int j = 0; j < (m); j++)
#define FOR2D(i, j, start_i, end_i, start_j, end_j) \
    for (int i = (start_i); i < (end_i); ++i)       \
        for (int j = (start_j); j < (end_j); ++j)
#define FORE(item, container) for (auto &item : container)
#define FORSTEP(i, a, b, step) for (int i = (a); i < (b); i += (step))
#define NL std::cout << "\n"
#define MOD 1000000007

void solve()
{
    int n, l, r;
    cin>>n>>l>>r;
    vi arr(n);
    FOR(i, 0, n) {
        cin>>arr[i];
    }
    priority_queue<int> pq;
    priority_queue<int, vector<int>, greater<int> > a, b;
    FOR(i, l-1, r)
        pq.push(arr[i]);
    FOR(i, 0, l-1)
        a.push(arr[i]);
    FOR(i, r, n)
        b.push(arr[i]);

    int sum = 0;
    int minSum = INT_MAX;
    while(!pq.empty()) {
        if(a.empty()) {
            sum += pq.top();
        }
        else {
            sum += min(a.top(), pq.top());
            a.pop();
        }
        pq.pop();
    }
    minSum = min(minSum, sum);

    FOR(i, l-1, r)
        pq.push(arr[i]);

    sum = 0;
    while(!pq.empty()) {
        if(b.empty()) {
            sum += pq.top();
        }
        else {
            sum += min(b.top(), pq.top());
            b.pop();
        }
        pq.pop();
    }
    minSum = min(minSum, sum);

    cout << minSum;
    NL;
}

int main()
{
    // #ifndef ONLINE_JUDGE
    //   freopen("input.txt", "r", stdin);
    //   freopen("output.txt", "w", stdout);
    // #endif
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
typedef long long ll;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll t;
	cin>>t;

	while(t-- >0){

		ll s,l,r;
		cin>>s>>l>>r;
		vector<ll> vec(s);
		for(auto &x: vec){cin>>x;}
		vector<ll> pref = vec;
		ll prefix=0;
		sort(pref.begin(),pref.begin()+r);

		reverse(pref.begin(),pref.begin()+r);
		prefix+= accumulate(pref.begin()+l-1,pref.begin()+r,0);

		vector<ll> suffix = vec;
		ll suff=0;
		sort(suffix.begin()+l-1,suffix.end());

		suff=accumulate(suffix.begin()+l-1,suffix.begin()+r,0);
		cout<<min(prefix,suff)<<endl;

	}

// 9 2 3 4 5 2 5 1 5

	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
typedef long long ll;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll t;
	cin>>t;

	while(t-- >0){

		ll s,l,r;
		cin>>s>>l>>r;
		vector<ll> vec(s);
		for(auto &x: vec){cin>>x;}
		vector<ll> pref = vec;
		ll prefix=0;
		sort(pref.begin(),pref.begin()+r);

		reverse(pref.begin(),pref.begin()+r);
		for(int i = l-1;i<r;i++){
			prefix+=pref[i];
		}
		vector<ll> suffix = vec;
		ll suff=0;
		sort(suffix.begin()+l-1,suffix.end());

		for(int i = l-1;i<r;i++){
			suff+=suffix[i];
		}
		cout<<min(prefix,suff)<<endl;

	}

	return 0;
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) x&(-x)
#define int long long

int INF = 1e18;
const int N = 5e5+20;

int n,l,r;
int lis[N];

void solve() {
    cin>>n>>l>>r;
    for(int i = 0;i<n;i++) cin>>lis[i];
    sort(lis,lis+n);

    int ans = 0;
    for(int i = 0;i<r-l+1;i++) ans += lis[i];
    cout<<ans<<endl;

}

signed main() {
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //cout<<fixed<<setprecision(9);

    int caset = 1;
    cin >> caset;
    while (caset--) { solve(); }

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) x&(-x)
#define int long long

int INF = 1e18;
const int N = 5e5+20;

int n,l,r;
int lis[N];
vector<int> nl,nr,in;

void solve() {
    cin>>n>>l>>r;
    for(int i = 1;i<=n;i++) cin>>lis[i];

    nl.clear();
    nr.clear();
    in.clear();

    for(int i = 1;i<l;i++) nl.push_back(lis[i]);
    for(int i = l;i<=r;i++) in.push_back(lis[i]);
    for(int i = r+1;i<=n;i++) nr.push_back(lis[i]);

    sort(nl.begin(),nl.end());
    sort(in.begin(),in.end(),greater<int>());
    sort(nr.begin(),nr.end());

    int t1 = 0;
    int t2 = 0;
    for(int i = 0;i<in.size();i++){
        if(i < nl.size()) t1 += min(nl[i],in[i]);
        else t1 += in[i];

        if(i < nr.size()) t2 += min(nr[i],in[i]);
        else t2 += in[i];
    }

    cout<<min(t1,t2)<<endl;

}

signed main() {
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //cout<<fixed<<setprecision(9);

    int caset = 1;
    cin >> caset;
    while (caset--) { solve(); }

    return 0;
}

Answer: 2


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// Function to solve the problem
int solve(int n, vector<int> sequence, int l, int r) {
    vector<int> copy = sequence;
    sort(copy.begin(), copy.begin() + r,greater<int>());
    sort(sequence.begin() + (l - 1), sequence.end());
    int left = 0, right = 0;
    for (int i = l - 1; i < r; i++) {
        left += copy[i];
        right += sequence[i];
    }
    return min(left, right);
}

int main() {
    int t;
    cin >> t;

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

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

        cout << solve(n, sequence, l, r) << endl;
    }

    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// Function to solve the problem
long long solve(int n, vector<int> sequence, int l, int r) {
    vector<int> copy = sequence;

    // Sorting first 'r' elements in descending order
    sort(copy.begin(), copy.begin() + r, greater<int>());

    // Sorting from 'l' to 'n' in ascending order
    sort(sequence.begin() + (l - 1), sequence.end());

    long long left = 0, right = 0; // Use long long to store large sums

    for (int i = l - 1; i < r; i++) {
        left += copy[i];
        right += sequence[i];
    }

    return min(left, right);
}

int main() {
    int t;
    cin >> t;

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

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

        cout << solve(n, sequence, l, r) << endl;
    }

    return 0;
}

//#include <iostream>
//#include <vector>
//#include <algorithm>
//
//using namespace std;
//int solve(int n, vector<int>& sequence, int l, int r) {
//    vector<int> left_part(sequence.begin(), sequence.begin() + r);
//    vector<int> right_part(sequence.begin() + (l - 1), sequence.end());
//    sort(left_part.begin(), left_part.end(), greater<int>());
//    sort(right_part.begin(), right_part.end()); // Ascending
//
//    int left_sum = 0, right_sum = 0;
//    for (int i = l - 1; i < r; i++) {
//        left_sum += left_part[i];
//        right_sum += right_part[i - (l - 1)];
//    }
//
//    return min(left_sum, right_sum);
//}
//
//int main() {
//    int t;
//    cin >> t;
//
//    while (t-- > 0) {
//        int n, l, r;
//        cin >> n >> l >> r;
//        vector<int> sequence(n);
//
//        for (int i = 0; i < n; i++) {
//            cin >> sequence[i];
//        }
//
//        cout << solve(n, sequence, l, r) << endl;
//    }
//
//    return 0;
//}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

const int N = 100010;

int a[N],b[N];

int main(){

    int t; scanf("%d",&t);

    while(t--){

        int n,l,r,len; scanf("%d%d%d",&n,&l,&r);

        len = r - l + 1;

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

            scanf("%d",&a[i]);

            b[i] = a[i];

        }

        sort(a,a + r);

        int ans = 0,tmp = 0;

        for(int i = 0;i < len;i++) tmp += a[i];

        sort(b + l - 1,b + n);

        for(int i = l - 1;i < l - 1 + len;i++) ans += b[i];

        ans = min(ans,tmp);

        printf("%d\n",ans);

    }

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

const int N = 100010;
int a[N],b[N];
int main(){
    int t; scanf("%d",&t);
    while(t--){
        int n,l,r,len; scanf("%d%d%d",&n,&l,&r);
        len = r - l + 1;
        for(int i = 0;i < n;i++){
            scanf("%d",&a[i]);
            b[i] = a[i];
        }
        sort(a,a + r);
        long long ans = 0,tmp = 0;
        for(int i = 0;i < len;i++) tmp += a[i];
        sort(b + l - 1,b + n);
        for(int i = l - 1;i < l - 1 + len;i++) ans += b[i];
        ans = min(ans,tmp);
        printf("%lld\n",ans);
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

void solve()

{

    int n, l, r; cin >> n >> l >> r;

    vector<int> a(n), b(n);

    for(int i=0; i<n; i++) cin >> a[i], b[i]=a[i];

    sort(a.begin(), a.begin()+r-1);

    sort(b.begin()+l-1, b.end());

    cout << min(accumulate(a.begin(), a.begin()+r-l+1, 0ll), accumulate(b.begin()+l-1, b.begin()+r, 0ll)) << '\n';

}

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    int tc=1; cin >> tc;

    while(tc--) solve();

}

#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int n, l, r; cin >> n >> l >> r;
    vector<int> a(n), b(n);
    for(int i=0; i<n; i++) cin >> a[i], b[i]=a[i];
    sort(a.begin(), a.begin()+r);
    sort(b.begin()+l-1, b.end());
    cout << min(accumulate(a.begin(), a.begin()+r-l+1, 0ll), accumulate(b.begin()+l-1, b.begin()+r, 0ll)) << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tc=1; cin >> tc;
    while(tc--) solve();
}

Answer: 2


import java.io.*;
import java.util.*;

/**
 * @Author: _Djhhh
 * @Introduction：
 */

public class Main {
    static FastReader fr = new FastReader();
    static FastWriter fw = new FastWriter();

    public static void solve() {
        int n=fr.nextInt();
        int l=fr.nextInt();
        int r=fr.nextInt();
        int[] arr = new int[n];
        int[] brr = new int[n];
        for(int i=0;i<n;i++){
            brr[i]=arr[i]= fr.nextInt();
        }
        Arrays.sort(arr,0,r);
        Arrays.sort(brr,l,n);
        long sum1=0;
        long sum2=0;
        for(int i=0;i<r-l+1;i++)sum1+=arr[i];
        for(int i=l-1;i<r;i++)sum2+=brr[i];
        fw.println(Math.min(sum1,sum2));
    }

    public static void main(String[] args) {
        int t = 1;
       t = fr.nextInt();
        while (t-- > 0) solve();
    }
}

class DSU {
    int n;
    int[] fa;
    int[] dep;
    int[] siz;
    int[] edges;
    boolean[] v;
    DSU(int n) {
        fa = new int[n];
        dep = new int[n];
        siz = new int[n];
        edges = new int[n];
        v = new boolean[n];
        for (int i = 0; i < n; i++) {
            fa[i] = i;
            dep[i] = 0;
            siz[i] = 1;
            edges[i] = 0;
            v[i]=false;
        }
    }
    int look(int x) {
        if (fa[x] == x) return x;
        else return fa[x] = look(fa[x]);
    }
    void merge(int x, int y) {
        v[x]=v[y]=true;
        int xx = look(x);
        int yy = look(y);
        if (xx == yy) return;
        edges[xx] += edges[yy] + 1;
        siz[xx] += siz[yy];
        if (dep[xx] < dep[yy]) {
            fa[xx] = yy;
        } else {
            fa[yy] = xx;
        }

        if (dep[xx] == dep[yy]) dep[xx]++;
    }
    boolean isMerge(int x, int y) {
        return look(x) == look(y);
    }
    int getSize(int x) {
        return siz[look(x)];
    }
    int getEdges(int x) {
        return edges[look(x)];
    }
}
class Pair<T1, T2> {
    private T1 first;
    private T2 second;

    public Pair(T1 first, T2 second) {
        this.first = first;
        this.second = second;
    }
    public void setFirst(T1 first) {
        this.first=first;
    }

    public void setSecond(T2 second) {
        this.second=second;
    }
    public T1 getFirst() {
        return first;
    }

    public T2 getSecond() {
        return second;
    }
}
class Algorithm {
    /**
     * 查找第一个大于等于 x 的位置
     * @param arr 查找数组
     * @param x 值
     * @param L 左边界
     * @param R 右边界
     * @return 第一个大于等于 x 的位置
     */
    public static int lowBs(int[] arr, long x, int L, int R){
        int l = L;
        int r = R;
        int res = R + 1;
        while(l <= r){
            int mid = l+r>>1;
            if(arr[mid] >= x){
                res = mid;
                r = mid - 1;
            }
            else{
                l = mid + 1;
            }
        }
        return res;
    }

    /**
     * 查找第一个大于 x 的位置
     * @param arr 查找数组
     * @param x 值
     * @param L 左边界
     * @param R 右边界
     * @return 第一个大于 x 的位置
     */
    public static int highBs(int[] arr, long x, int L, int R) {
        int l = L;
        int r = R;
        int res = R + 1;
        while(l <= r){
            int mid = l+r>>1;
            if(arr[mid] > x){
                res = mid;
                r = mid -1;
            }
            else{
                l = mid +1;
            }
        }
        return res;
    }
}
class FastReader {
    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    int[] nextIntArray() {
        String line = nextLine();
        String[] strNums = line.split("\\s+");
        int[] arr = new int[strNums.length];
        for (int i = 0; i < strNums.length; i++) {
            arr[i] = Integer.parseInt(strNums[i]);
        }
        return arr;
    }

    long[] nextLongArray() {
        String line = nextLine();
        String[] strNums = line.split("\\s+");
        long[] arr = new long[strNums.length];
        for (int i = 0; i < strNums.length; i++) {
            arr[i] = Long.parseLong(strNums[i]);
        }
        return arr;
    }

    char[] nextCharArray() {
        String line = nextLine();
        String[] strNums = line.split("\\s+");
        char[] arr = new char[strNums.length];
        for (int i = 0; i < strNums.length; i++) {
            arr[i] = strNums[i].charAt(0);
        }
        return arr;
    }

    char[] StringtoChar() {
        String line = nextLine();
        return line.toCharArray();
    }
}
class FastWriter {
    private final PrintWriter writer;

    public FastWriter() {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), true);
    }

    public void print(Object object) {
        writer.print(object);
    }

    public void println(Object object) {
        writer.println(object);
    }

    public void flush() {
        writer.flush();
    }
}

import java.io.*;
import java.util.*;

/**
 * @Author: _Djhhh
 * @Introduction：
 */

public class Main {
    static FastReader fr = new FastReader();
    static FastWriter fw = new FastWriter();

    public static void solve() {
        int n=fr.nextInt();
        int l=fr.nextInt();
        int r=fr.nextInt();
        int[] arr = new int[n];
        int[] brr = new int[n];
        for(int i=0;i<n;i++){
            brr[i]=arr[i]= fr.nextInt();
        }
        Arrays.sort(arr,0,r);
        Arrays.sort(brr,l-1,n);
        long sum1=0;
        long sum2=0;
        for(int i=0;i<r-l+1;i++)sum1+=arr[i];
        for(int i=l-1;i<r;i++)sum2+=brr[i];
        fw.println(Math.min(sum1,sum2));
    }

    public static void main(String[] args) {
        int t = 1;
       t = fr.nextInt();
        while (t-- > 0) solve();
    }
}

class DSU {
    int n;
    int[] fa;
    int[] dep;
    int[] siz;
    int[] edges;
    boolean[] v;
    DSU(int n) {
        fa = new int[n];
        dep = new int[n];
        siz = new int[n];
        edges = new int[n];
        v = new boolean[n];
        for (int i = 0; i < n; i++) {
            fa[i] = i;
            dep[i] = 0;
            siz[i] = 1;
            edges[i] = 0;
            v[i]=false;
        }
    }
    int look(int x) {
        if (fa[x] == x) return x;
        else return fa[x] = look(fa[x]);
    }
    void merge(int x, int y) {
        v[x]=v[y]=true;
        int xx = look(x);
        int yy = look(y);
        if (xx == yy) return;
        edges[xx] += edges[yy] + 1;
        siz[xx] += siz[yy];
        if (dep[xx] < dep[yy]) {
            fa[xx] = yy;
        } else {
            fa[yy] = xx;
        }

        if (dep[xx] == dep[yy]) dep[xx]++;
    }
    boolean isMerge(int x, int y) {
        return look(x) == look(y);
    }
    int getSize(int x) {
        return siz[look(x)];
    }
    int getEdges(int x) {
        return edges[look(x)];
    }
}
class Pair<T1, T2> {
    private T1 first;
    private T2 second;

    public Pair(T1 first, T2 second) {
        this.first = first;
        this.second = second;
    }
    public void setFirst(T1 first) {
        this.first=first;
    }

    public void setSecond(T2 second) {
        this.second=second;
    }
    public T1 getFirst() {
        return first;
    }

    public T2 getSecond() {
        return second;
    }
}
class Algorithm {
    /**
     * 查找第一个大于等于 x 的位置
     * @param arr 查找数组
     * @param x 值
     * @param L 左边界
     * @param R 右边界
     * @return 第一个大于等于 x 的位置
     */
    public static int lowBs(int[] arr, long x, int L, int R){
        int l = L;
        int r = R;
        int res = R + 1;
        while(l <= r){
            int mid = l+r>>1;
            if(arr[mid] >= x){
                res = mid;
                r = mid - 1;
            }
            else{
                l = mid + 1;
            }
        }
        return res;
    }

    /**
     * 查找第一个大于 x 的位置
     * @param arr 查找数组
     * @param x 值
     * @param L 左边界
     * @param R 右边界
     * @return 第一个大于 x 的位置
     */
    public static int highBs(int[] arr, long x, int L, int R) {
        int l = L;
        int r = R;
        int res = R + 1;
        while(l <= r){
            int mid = l+r>>1;
            if(arr[mid] > x){
                res = mid;
                r = mid -1;
            }
            else{
                l = mid +1;
            }
        }
        return res;
    }
}
class FastReader {
    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    String nextLine() {
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    int[] nextIntArray() {
        String line = nextLine();
        String[] strNums = line.split("\\s+");
        int[] arr = new int[strNums.length];
        for (int i = 0; i < strNums.length; i++) {
            arr[i] = Integer.parseInt(strNums[i]);
        }
        return arr;
    }

    long[] nextLongArray() {
        String line = nextLine();
        String[] strNums = line.split("\\s+");
        long[] arr = new long[strNums.length];
        for (int i = 0; i < strNums.length; i++) {
            arr[i] = Long.parseLong(strNums[i]);
        }
        return arr;
    }

    char[] nextCharArray() {
        String line = nextLine();
        String[] strNums = line.split("\\s+");
        char[] arr = new char[strNums.length];
        for (int i = 0; i < strNums.length; i++) {
            arr[i] = strNums[i].charAt(0);
        }
        return arr;
    }

    char[] StringtoChar() {
        String line = nextLine();
        return line.toCharArray();
    }
}
class FastWriter {
    private final PrintWriter writer;

    public FastWriter() {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), true);
    }

    public void print(Object object) {
        writer.print(object);
    }

    public void println(Object object) {
        writer.println(object);
    }

    public void flush() {
        writer.flush();
    }
}

Answer: 2


#include <bits/stdc++.h>
#include <numeric>
#include <random>
#include <cstdint>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
using ll = long long;

#define all(v) v.begin(), v.end()
#define left id << 1
#define right id << 1 | 1
#define sz(x) (int)x.size()
#define ordered_set tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update>
#define DEBUG cout << "TESTCASE " << tc << ": ";

template<typename... T>
void see(T&... args) { ((cin >> args), ...);}
template<typename... T>
void put(T&&... args) { ((cout << args << " "), ...);}

const int lg2 = 20;
const int N = 2010;
const int mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f3f3f3f;

struct chash {
    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);
    }
};

ll gcd(ll x, ll y) {
    if (!y) return x;
    return gcd(y, x % y);
}

ll lcm(ll x, ll y) {
    return (x * y) / gcd(x, y);
}

bool cmp(pair<int, int> a, pair<int, int> b) {
	return a.second < b.second;
}

template <class T>
bool ckmin(T &a, T b) {
	if (a > b)
		return (a = b);
	return 0;
}

template <class T>
bool ckmax(T &a, T b) {
	if (a < b)
		return (a = b);
	return 0;
}

ll binpow(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1)
			res *= a;
		b >>= 1;
		a *= a;
	}
	return res;
}

void add(ll &a, ll b) {
	a += b;
	if (a >= mod)
		a -= mod;
}

void sub(ll &a, ll b) {
	a -= b;
	if (a < 0)
		a += mod;
}

void solve() {
	int n, l, r; see(n, l, r);
    vector<int> a(n);
    for (int &e: a) cin >> e;
    vector<ll> x(a.begin(), a.begin() + r);
    vector<ll> y(a.begin() + l - 1, a.end());
    sort(all(x)); sort(all(y));
    int cnt = r - l + 1;
    cout << min(accumulate(x.begin(), x.begin() + cnt, 0ll), accumulate(y.begin(), y.begin() + cnt, 0ll)) << '\n';
}

/*

*/

int32_t main() {

	// if (fopen("input.inp", "r")) {
	// 	freopen("input.inp", "r", stdin);
	// 	freopen("output.out", "w", stdout);
	// }

	ios::sync_with_stdio(NULL);
	cin.tie(0); cout.tie(0);

	int tc = 1;
	cin >> tc;
	while (tc--) {
		solve();
	}

	return 0;
}

/*
	nice bin string
	1 must go with 0
	0 must go with 1
 */

#include <bits/stdc++.h>
#include <numeric>
#include <random>
#include <cstdint>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
using ll = long long;

#define all(v) v.begin(), v.end()
#define left id << 1
#define right id << 1 | 1
#define sz(x) (int)x.size()
#define ordered_set tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update>
#define DEBUG cout << "TESTCASE " << tc << ": ";

template<typename... T>
void see(T&... args) { ((cin >> args), ...);}
template<typename... T>
void put(T&&... args) { ((cout << args << " "), ...);}

const int lg2 = 20;
const int N = 2010;
const int mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f3f3f3f;

struct chash {
    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);
    }
};

ll gcd(ll x, ll y) {
    if (!y) return x;
    return gcd(y, x % y);
}

ll lcm(ll x, ll y) {
    return (x * y) / gcd(x, y);
}

bool cmp(pair<int, int> a, pair<int, int> b) {
	return a.second < b.second;
}

template <class T>
bool ckmin(T &a, T b) {
	if (a > b)
		return (a = b);
	return 0;
}

template <class T>
bool ckmax(T &a, T b) {
	if (a < b)
		return (a = b);
	return 0;
}

ll binpow(ll a, ll b) {
	ll res = 1;
	while (b) {
		if (b & 1)
			res *= a;
		b >>= 1;
		a *= a;
	}
	return res;
}

void add(ll &a, ll b) {
	a += b;
	if (a >= mod)
		a -= mod;
}

void sub(ll &a, ll b) {
	a -= b;
	if (a < 0)
		a += mod;
}

void solve() {
	int n, l, r; see(n, l, r);
    vector<int> a(n);
    for (int &e: a) cin >> e;
    sort(all(a));
    ll sum = 0;
    for (int i = 0; i < r - l + 1; ++i) {
        sum += a[i];
    }
    cout << sum << '\n';
}

/*

*/

int32_t main() {

	// if (fopen("input.inp", "r")) {
	// 	freopen("input.inp", "r", stdin);
	// 	freopen("output.out", "w", stdout);
	// }

	ios::sync_with_stdio(NULL);
	cin.tie(0); cout.tie(0);

	int tc = 1;
	cin >> tc;
	while (tc--) {
		solve();
	}

	return 0;
}

/*
	nice bin string
	1 must go with 0
	0 must go with 1
 */

Answer: 1


import java.io.*;
import java.util.*;

public class TestCodeforces
{
    public static void main(String[] args)
    {
        FastReader fr = new FastReader();
        PrintWriter pw = new PrintWriter(System.out);
        int t = fr.nextInt();
        while(t-->0)
        {
            int n = fr.nextInt();
            int l = fr.nextInt();
            int r = fr.nextInt();
            int wnd = r-l+1;
            long arr[] = new long[n];
            for(int i=0; i<n; i++)
            {
                arr[i] = fr.nextInt();
            }
            long min_sum = Math.min(sortSum(Arrays.copyOfRange(arr, 0, r), wnd), sortSum(Arrays.copyOfRange(arr, l-1, n), wnd));
            pw.println(min_sum);
        }
        pw.flush();
        pw.close();
    }

    public static long sortSum(long arr[], int z)
    {
        long sum = 0;
        Arrays.sort(arr);
        for(int i=0; i<z; i++)
        {
            sum += arr[i];
        }
        return sum;
    }
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;
        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    static public boolean isPrime(int n)
    {
        int z=0;
        for(int i=2; i<=n/2; i++)
        {
            if(n%i==0)
            {
                z++;
            }
        }
        if(z==1)
        {
            return true;
        }
        return false;
    }

    public static int gcd(int a, int b)
    {
        if (b == 0) return a;
        return gcd(b, a % b);
    }

}

import java.io.*;
import java.util.*;

public class TestCodeforces
{
    public static void main(String[] args)
    {
        FastReader fr = new FastReader();
        PrintWriter pw = new PrintWriter(System.out);
        int t = fr.nextInt();
        while(t-->0)
        {
            int n = fr.nextInt();
            int l = fr.nextInt();
            int r = fr.nextInt();
            int wnd = r-l+1;
            int arr[] = new int[n];
            for(int i=0; i<n; i++)
            {
                arr[i] = fr.nextInt();
            }
            int min_sum = Math.min(sortSum(Arrays.copyOfRange(arr, 0, r), wnd), sortSum(Arrays.copyOfRange(arr, l-1, n), wnd));
            pw.println(min_sum);
        }
        pw.flush();
        pw.close();
    }

    public static int sortSum(int arr[], int z)
    {
        int sum = 0;
        Arrays.sort(arr);
        for(int i=0; i<z; i++)
        {
            sum += arr[i];
        }
        return sum;
    }
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;
        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    static public boolean isPrime(int n)
    {
        int z=0;
        for(int i=2; i<=n/2; i++)
        {
            if(n%i==0)
            {
                z++;
            }
        }
        if(z==1)
        {
            return true;
        }
        return false;
    }

    public static int gcd(int a, int b)
    {
        if (b == 0) return a;
        return gcd(b, a % b);
    }

}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pp make_pair
#define endl "\n"
#define f first
#define s second
#define TxtIO   freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
#define int long long
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t ;cin>>t;
    while (t--)
    {
        int n,l,r ; cin>>n>>l>>r;
        l--;r--;
        vector<int> v(n);
        for (int i = 0 ; i<n ; i++) cin>>v[i];
        sort(v.begin(),v.end());
         int ans=0;
        for (int i = 0;i<r-l+1;i++) ans+=v[i];
        cout<<ans<<endl;
    }
}
// store the indexes
// 3  6  6  4  3 2
// 8 11 11  10 - -
//  after sorting  2 3 3 4 6 6

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pp make_pair
#define endl "\n"
#define f first
#define s second
#define TxtIO   freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
#define int long long

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t ;cin>>t;
    while (t--)
    {
        int n,l,r ; cin>>n>>l>>r;
        l--;r--;
        vector<int> v(n);
        for (int i = 0 ; i<n ; i++) cin>>v[i];

        vector<int> v1;
        vector<int> v2;

        for (int i = l; i<=r;i++) {
        v1.push_back(v[i]);
        v2.push_back(v[i]);
        }

        for (int i = 0 ;i<l;i++) v1.push_back(v[i]);
        for (int i = r+1 ;i<n;i++) v2.push_back(v[i]);

        sort(v1.begin(),v1.end());
        sort(v2.begin(),v2.end());
        int k= r-l+1;
        int sum1=0;
      //  cout<<v1.size()<<" "<<v2.size();
    //    return 0;
        for (int i = 0 ;i<k;i++) sum1+=v1[i];

        int sum2 = 0 ;
        for (int i = 0 ; i<k;i++) sum2+=v2[i];

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

    }
}
// store the indexes
// 3  6  6  4  3 2
// 8 11 11  10 - -
//  after sorting  2 3 3 4 6 6

Answer: 2


#include"bits/stdc++.h"
using namespace std;

#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define endl "\n"
#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)

typedef long long ll;
const int MOD = 1000000007;
const int Nmax = 2e5+5;

void akibify(){
ll n,l,r ;
cin>>n>>l>>r ;
vector<ll>v(n);
for(auto &a : v) cin>>a ;
sort(v.begin(),v.end());
ll ans = 0 ;
for(int i = 0 ; i < r-l+1 ; i++){
   ans += v[i];
}
cout<<ans<<endl;
}

int main(){
   fast_io;
    int t ;
    cin>>t ;
    while(t--){
    akibify();

    }

    return 0;
}

#include"bits/stdc++.h"
using namespace std;

#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define endl "\n"
#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)

typedef long long ll;
const int MOD = 1000000007;
const int Nmax = 2e5+5;

void akibify(){
ll n,l,r ;
cin>>n>>l>>r ;
vector<ll>v(n);
vector<ll>lv,rv;
for(auto &a : v) cin>>a ;

for(int i = 0 ; i < r ; i++) lv.push_back(v[i]);
for(int i = l-1 ; i < n ; i++) rv.push_back(v[i]);
sort(lv.begin(),lv.end());
sort(rv.begin(),rv.end());
ll suml=0,sumr = 0;
for(int i = 0 ; i < r-l+1 ; i++) suml+=lv[i];
for(int i = 0 ; i < r-l+1 ; i++) sumr+=rv[i];
ll ans = min(suml,sumr) ;

cout<<ans<<endl;

}

int main(){
   fast_io;
    int t ;
    cin>>t ;
    while(t--){
    akibify();

    }

    return 0;
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

int main()

{

    int t;

    cin>>t;

    while(t--)

    {

        int n,l,r;

        cin>>n>>l>>r;

        vector<long long>a(n);

        for(auto &x:a)

            cin>>x;

        long long sum=0;

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

        {

            sum+=a[i];

        }

        if(r<n && l>1)

        {

            sort(a.begin()+l-1,a.begin()+r);

            sort(a.begin(),a.begin()+l-1);

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

            long long lsum=sum,rsum=sum;

            int xl=r,yl=r-1;

            while(xl<n && yl>=l-1 && a[xl]<a[yl])

            {

                lsum=lsum-a[yl]+a[xl];

                xl++;

                yl--;

            }

            int xr=0,yr=r-1;

            while(xr<l-1 && yr>=l-1 && a[xr]<a[yr])

            {

                rsum=rsum-a[yr]+a[xr];

                xr++;

                yr--;

            }

            sum=min(lsum,rsum);

            cout<<sum;

        }

        else if(l==1 && r<n)

        {

            sort(a.begin()+l-1,a.begin()+r);

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

            int x=r,y=r-1;

            while(x<n && y>=0 && a[x]<a[y])

            {

                sum=sum-a[y]+a[x];

                x++;

                y--;

            }

            cout<<sum;

        }

        else if(r==n && l>1)

        {

            sort(a.begin()+l-1,a.begin()+r);

            sort(a.begin(),a.begin()+l-1);

            int x=0,y=r-1;

            while(x<l-1 && y >=l-1 && a[x]<a[y])

            {

                sum=sum-a[y]+a[x];

                x++;

                y--;

            }

            cout<<sum;

        }

        else

        {

            cout<<sum;

        }

        cout<<"\n";

    }

}

#include <bits/stdc++.h>

using namespace std;

int main()

{

    int t;

    cin>>t;

    while(t--)

    {

        int n,l,r;

        cin>>n>>l>>r;

        vector<long long>a(n);

        for(auto &x:a)

            cin>>x;

        long long sum=0;

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

        {

            sum+=a[i];

        }

        if(r<n && l>1)

        {

            sort(a.begin()+l-1,a.begin()+r-1);

            sort(a.begin(),a.begin()+l-2);

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

            long long lsum=sum,rsum=sum;

            int xl=r,yl=r-1;

            while(xl<n && yl>=l-1 && a[xl]<a[yl])

            {

                lsum=lsum-a[yl]+a[xl];

                xl++;

                yl--;

            }

            int xr=0,yr=r-1;

            while(xr<l-1 && yr>=l-1 && a[xr]<a[yr])

            {

                rsum=rsum-a[yr]+a[xr];

                xr++;

                yr--;

            }

            sum=min(lsum,rsum);

            cout<<sum;

        }

        else if(l==1 && r<n)

        {

            sort(a.begin()+l-1,a.begin()+r-1);

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

            int x=r,y=r-1;

            while(x<n && y>=0 && a[x]<a[y])

            {

                sum=sum-a[y]+a[x];

                x++;

                y--;

            }

            cout<<sum;

        }

        else if(r==n && l>1)

        {

            sort(a.begin()+l-1,a.begin()+r-1);

            sort(a.begin(),a.begin()+l-2);

            int x=0,y=r-1;

            while(x<l-1 && y >=l-1 && a[x]<a[y])

            {

                sum=sum-a[y]+a[x];

                x++;

                y--;

            }

            cout<<sum;

        }

        else

        {

            cout<<sum;

        }

        cout<<"\n";

    }

}

Answer: 1


def solve():

    n,l,r=tuple(map(int,input().split()))

    a=list(map(int,input().split()))

    a.sort()

    res=0

    for i in range(r-l+1):

        res+=a[i]

    print(res)

T=int(input())

for _ in range(T):

    solve()

def solve():

    n,l,r=tuple(map(int,input().split()))

    a=list(map(int,input().split()))

    l-=1

    r-=1

    ll=a[:r+1]

    rr=a[l:]

    ll.sort()

    rr.sort()

    print(min(sum(ll[0:r-l+1]),sum(rr[0:r-l+1])))

T=int(input())

for _ in range(T):

    solve()

Answer: 2


#include <algorithm>

#include <array>

#include <iomanip>

#include <iostream>

#include <cmath>

#include <cstdint>

#include <numeric>

#include <map>

#include <set>

#include <string>

#include <vector>

using namespace std;

int64_t min_sum(const vector<int64_t>& lr, const vector<int64_t>& other) {

    int64_t diff = 0;

    for (int i = 0; i < min(lr.size(), other.size()); i++) {

        // cout << "loop: " << other[i] << " " << lr[i] << endl;

        if (other[i] < lr[i]) diff += (lr[i] - other[i]);

    }

    // cout << diff << endl;

    return diff;

}

void solve() {

    // Read input

    int n, l, r;

    cin >> n >> l >> r;

    l--; r--;

    vector<int64_t> a(n);

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

        cin >> a[i];

    }

    // Solve the problem here

    vector<int64_t> lr;

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

        lr.push_back(a[i]);

    }

    sort(lr.begin(), lr.end(), std::greater<int64_t>());

    int64_t sum = accumulate(lr.begin(), lr.end(), 0);

    // cout << sum << endl;

    vector<int64_t> bl;

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

        bl.push_back(a[i]);

    }

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

    vector<int64_t> ar;

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

        ar.push_back(a[i]);

    }

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

    sum = min(sum, sum - min_sum(lr, bl));

    sum = min(sum, sum - min_sum(lr, ar));

    // Print output

    cout << sum << endl;

}

int main() {

    // freopen("input.txt","r",stdin),freopen("output.txt","w",stdout);

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);

    cout << setprecision(10);

    int t;

    cin >> t;

    while (t--) {

        solve();

    }

    return 0;

}

#include <algorithm>

#include <array>

#include <iomanip>

#include <iostream>

#include <cmath>

#include <cstdint>

#include <numeric>

#include <map>

#include <set>

#include <string>

#include <vector>

using namespace std;

int64_t min_sum(const vector<int64_t>& lr, const vector<int64_t>& other) {

    int64_t diff = 0;

    for (int i = 0; i < min(lr.size(), other.size()); i++) {

        // cout << "loop: " << other[i] << " " << lr[i] << endl;

        if (other[i] < lr[i]) diff += (lr[i] - other[i]);

    }

    // cout << diff << endl;

    return diff;

}

void solve() {

    // Read input

    int n, l, r;

    cin >> n >> l >> r;

    l--; r--;

    vector<int64_t> a(n);

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

        cin >> a[i];

    }

    // Solve the problem here

    vector<int64_t> lr;

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

        lr.push_back(a[i]);

    }

    sort(lr.begin(), lr.end(), std::greater<int64_t>());

    int64_t sum = accumulate(lr.begin(), lr.end(), int64_t(0));

    // cout << sum << endl;

    vector<int64_t> bl;

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

        bl.push_back(a[i]);

    }

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

    vector<int64_t> ar;

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

        ar.push_back(a[i]);

    }

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

    sum = min(min(sum, sum - min_sum(lr, bl)), sum - min_sum(lr, ar));

    // Print output

    cout << sum << endl;

}

int main() {

    // freopen("input.txt","r",stdin),freopen("output.txt","w",stdout);

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);

    cout << setprecision(10);

    int t;

    cin >> t;

    while (t--) {

        solve();

    }

    return 0;

}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int test;
    cin>>test;
    while(test--)
    {
        int n,l,r;
        cin>>n>>l>>r;
        vector<int> vec(n);
        for(int i=0;i<n;i++)cin>>vec[i];
        ll sum1=0,sum2=0;
        vector<int> vec2(n);
        for(int i=0;i<n;i++)vec2[i]=vec[i];
        sort(vec.begin(),vec.begin()+r-1);
        sort(vec2.begin()+l-1,vec2.end());
        for(int i=0;i<r-l+1;i++)sum1+=vec[i];
        for(int i=l-1;i<r;i++)sum2+=vec2[i];
        cout<<min(sum1,sum2)<<endl;
    }
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int test;
    cin>>test;
    while(test--)
    {
        int n,l,r;
        cin>>n>>l>>r;
        vector<int> vec(n);
        for(int i=0;i<n;i++)cin>>vec[i];
        ll sum1=0,sum2=0;
        vector<int> vec2 =vec;
        sort(vec.begin(),vec.begin()+r);
        sort(vec2.begin()+l-1,vec2.end());
        for(int i=0;i<r-l+1;i++)sum1+=vec[i];
        for(int i=l-1;i<r;i++)sum2+=vec2[i];
        cout<<min(sum1,sum2)<<endl;
    }
}

Answer: 2