#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define mod 1000000007

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

#define pb push_back

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

#define nn '\n'

// Fast Input-Output

void fastIO()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);

}

int main()

{

    fastIO();

    int t;

    cin >> t;

    while (t--)

    {

        int n, l, r;

        cin >> n >> l >> r;

        vector<int> a(n);

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

        {

            cin >> a[i];

        }

        vector<int> before, after, btw;

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

        {

            if (i < l - 1)

                before.pb(a[i]);

            else if (i > r - 1)

                after.pb(a[i]);

            else

                btw.pb(a[i]);

        }

        sort(all(before));

        sort(all(after));

        sort(all(btw));

        int sum = accumulate(all(btw), 0);

        int mini = sum;

        int i = 0, j = btw.size() - 1;

        while (i < before.size() && j >= 0 && before[i] < btw[j])

        {

            sum = sum - btw[j] + before[i];

            mini = min(mini, sum);

            i++;

            j--;

        }

        sum = accumulate(all(btw), 0);

        i = 0, j = btw.size() - 1;

        while (i < after.size() && j >= 0 && after[i] < btw[j])

        {

            sum = sum - btw[j] + after[i];

            mini = min(mini, sum);

            i++;

            j--;

        }

        cout << mini << nn;

    }

    return 0;

}

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

#define ll long long
#define pb push_back
#define all(v) v.begin(), v.end()
#define nn '\n'

// Fast Input-Output
void fastIO()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

int main()
{
    fastIO();

    int t;
    cin >> t;
    while (t--)
    {
        int n, l, r;
        cin >> n >> l >> r;
        l--; // Adjust for 0-based index

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

        vector<ll> brr = arr;
        sort(brr.begin() + l, brr.end());

        vector<ll> crr = arr;
        sort(crr.begin(), crr.begin() + r, greater<ll>());

        ll sum_brr = accumulate(brr.begin() + l, brr.begin() + r, 0LL);
        ll sum_crr = accumulate(crr.begin() + l, crr.begin() + r, 0LL);

        cout << min(sum_brr, sum_crr) << nn;
    }

    return 0;
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define ull unsigned long long

#define pb push_back

#define INF 1e18

#define yes cout << "YES" << endl;

#define no cout << "NO" << endl;

#define F first

#define S second

#define sz(x) x.size()

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

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

#define gcd(a, b) __gcd(a, b)

#define lcm(a, b) ((a) * ((b) / gcd(a, b)))

#define MAX3(a, b, c) max(a, max(b, c))

#define MIN3(a, b, c) min(a, min(b, c))

#define vi vector<ll>

#define vvi vector<vector<ll>>

#define pii pair<ll, ll>

#define vii vector<pair<ll, ll>>

void solve()

{

    ll n, l, r;

    cin >> n >> l >> r;

    vector<ll> v(n);

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

    {

        cin >> v[i];

    }

    sort(all(v));

    vi p(n, 0);

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

    {

        if (i == 0)

        {

            p[i] = v[i];

        }

        else

        {

            p[i] = p[i - 1] + v[i];

        }

    }

    ll dif = r - l + 1;

    cout << p[dif - 1] << endl;

}

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);

    int t;

    cin >> t;

    while (t--)

        solve();

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define ull unsigned long long

#define pb push_back

#define INF 1e18

#define yes cout << "YES" << endl;

#define no cout << "NO" << endl;

#define F first

#define S second

#define sz(x) x.size()

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

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

#define gcd(a, b) __gcd(a, b)

#define lcm(a, b) ((a) * ((b) / gcd(a, b)))

#define MAX3(a, b, c) max(a, max(b, c))

#define MIN3(a, b, c) min(a, min(b, c))

#define vi vector<ll>

#define vvi vector<vector<ll>>

#define pii pair<ll, ll>

#define vii vector<pair<ll, ll>>

void solve()

{

    ll n, l, r;

    cin >> n >> l >> r;

    l--;

    r--;

    vector<ll> v(n);

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

    {

        cin >> v[i];

    }

    vi f, b;

    for (ll i = 0; i <= r; i++)

    {

        f.pb(v[i]);

    }

    for (ll i = l; i < n; i++)

    {

        b.pb(v[i]);

    }

    sort(all(f));

    sort(all(b));

    ll sum1 = accumulate(f.begin(), f.begin() + (int)(r - l + 1), 0LL);

    ll sum2 = accumulate(b.begin(), b.begin() + (int)(r - l + 1), 0LL);

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

}

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);

    int t;

    cin >> t;

    while (t--)

        solve();

    return 0;

}

Answer: 2


#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

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

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

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

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

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

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

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

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

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

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

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

    solve();

    return 0;
}

#include <iostream>

#include <vector>

#include <algorithm>

#include <numeric>

using namespace std;

// פונקציה שמקטינה את סכום הקטע [l, r] על ידי החלפה עם חלק חיצוני

long long minimizeSegment(vector<int>& segment, vector<int>& external) {

    sort(segment.rbegin(), segment.rend());  // מיון יורד (הגדולים ראשונים)

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

    int minSize = min(segment.size(), external.size());

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

        if (segment[i] > external[i]) {

            swap(segment[i], external[i]);

        } else {

            break;

        }

    }

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

}

void solve() {

    int t;

    cin >> t;

    while (t--) {

        int n, l, r;

        cin >> n >> l >> r;

        vector<int> arr(n);

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

            cin >> arr[i];

        }

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

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

        vector<int> suffix(arr.begin() + r, arr.end());

        long long sumWithPrefix = prefix.empty() ? LLONG_MAX : minimizeSegment(segment, prefix);

        long long sumWithSuffix = suffix.empty() ? LLONG_MAX : minimizeSegment(segment, suffix);

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

    }

}

int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    solve();

    return 0;

}

Answer: 1


#include <bits/stdc++.h>

using namespace std;

using ll = long long;

using ull = unsigned long long;

#define endl "\n";

void solve()

{

    int k;

    cin >> k;

    int l,r;

    cin>>l>>r;

    int len=r-l+1;

    vector<ll>a;

    vector<ll>b(len);

    vector<ll>c;

    int m=0,n=0;

    ll sum1=0,sum2=0;

    int len1=0,len2=0;

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

    {

        if(i+1>=l&&i+1<=r)

        {

            cin>>b[n];

            sum1+=b[n];

            n++;

        }

        else if(i+1<l)

        {

            int num;

            cin>>num;

            a.push_back(num);

            len1++;

        }

        else if(i+1>r)

        {

            int num2;

            cin>>num2;

            c.push_back(num2);

            len2++;

        }

    }

    sum2=sum1;

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

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

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

    for(int i=0;i<min(len,len1);i++)

    {

        if(a[i]<b[len-1-i])

        {

            sum1-=b[len-1-i];

            sum1+=a[i];

        }

    }

    for(int i=0;i<min(len,len2);i++)

    {

        if(c[i]<b[len-1-i])

        {

            sum2-=b[len-1-i];

            sum2+=c[i];

        }

    }

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

}

int main()

{

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

    int t;

    cin >> t;

    while (t--)

    {

        solve();

    }

    return 0;

}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define endl "\n";

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

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

    int len=r-l+1;

    vector<ll>a(k-len);
    vector<ll>b(len);

    int m=0,n=0;
    ll sum1=0,sum2=0;
    for(int i=0;i<k;i++)
    {
        if(i+1>=l&&i+1<=r)
        {
            cin>>b[n];
            sum2+=b[n];
            n++;
        }
        else
        {
            cin>>a[m];
            sum1+=a[m];
            m++;
        }
    }

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

    for(int i=0;i<min(len,k-len);i++)
    {
        if(a[i]<b[len-1-i])
        {
            sum2-=b[len-1-i];
            sum2+=a[i];
        }
    }

    cout<<sum2<<endl;
}
int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int t;
    cin >> t;

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

    return 0;
}

Answer: 1


#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define endl '\n'
#define int long long
#define PII pair<int, int>
#define _rep(i,a,b) for( int i=(a); i<=(b); ++i)
#define _per(i,a,b) for( int i=(a); i>=(b); --i)
using namespace std;
const int N = 1e6+10 ;
const int M = 1e3+10 ;
const int INF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7  ;
const double eps = 1e-9 ;
int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
int lcm(int a, int b){ return a * b / gcd(a,b); }
int qmi(int a, int b){
    int res = 1 ;
    while(b){
        if(b&1) res = a * res % MOD ;
        b >>= 1 ;
        a = a * a % MOD ;
    }
    return res ;
}
int T = 1 ;
int ans = 0 ;
int n , m, k, q ;
int a[N] ;
void solve(){
    int l, r ;
    cin >> n >> l >> r ;
    for(int i = 1; i <= n ; ++ i){
        cin >> a[i] ;
    }
    vector<int> vec1 ;
    vector<int> vec2 ;
    for(int i = 1; i <= n ; ++ i){
        if(i <= r) vec1.push_back(a[i]) ;
        if(i >=l ) vec2.push_back(a[i]) ;
    }
    sort(vec1.begin(), vec1.end()) ;
    sort(vec2.begin(), vec2.end()) ;
    int ans1 = 0 ;
    int ans2 = 0 ;
    for(int i = 0 ; i < r - l + 1; ++ i){
        ans1 += vec1[i] ;
        ans2 += vec2[i] ;
    }
    ans = min(ans1, ans2) ;
    cout << ans << endl ;
}
signed main(){
    IOS ;
    cin >> T ;
    while(T --){
        solve() ;
    }
    return 0;

}

#include<bits/stdc++.h>

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

#define endl '\n'

#define int long long

#define PII pair<int, int>

#define _rep(i,a,b) for( int i=(a); i<=(b); ++i)

#define _per(i,a,b) for( int i=(a); i>=(b); --i)

using namespace std;

const int N = 1e6+10 ;

const int M = 1e3+10 ;

const int INF = 0x3f3f3f3f3f3f3f3f;

const int MOD = 1e9 + 7  ;

const double eps = 1e-9 ;

int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }

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

int qmi(int a, int b){

    int res = 1 ;

    while(b){

        if(b&1) res = a * res % MOD ;

        b >>= 1 ;

        a = a * a % MOD ;

    }

    return res ;

}

int T = 1 ;

int ans = 0 ;

int n , m, k, q ;

int a[N] ;

void solve(){

    int l, r ;

    cin >> n >> l >> r ;

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

        cin >> a[i] ;

    }

    ans = 0 ;

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

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

        ans += a[i] ;

    }

    cout << ans << endl ;

}

signed main(){

    IOS ;

    cin >> T ;

    while(T --){

        solve() ;

    }

    return 0;

}

Answer: 1


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

void solve(){
	int n,l,r;
	cin >> n >> l >> r;
	int a[n+1];
	for(int i=0;i<n;i++) cin >> a[i];
	sort(a,a+n);
	int ans=0;
	for(int i=0;i<=r-l;i++) ans+=a[i];
	cout << ans << endl;

}

signed main(){
	int t; cin >> t;
	while(t--){
		solve();
	}
	return 0;
}

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

bool cmp(int a,int b){
	return a>b;
}

void solve(){
	int n,l,r;
	cin >> n >> l >> r;
	int a[n+1];
	vector<int> v1,v2,v3;
	int sum=0;
	for(int i=1;i<=n;i++){
		cin >> a[i];
		if(i<=l-1) v1.push_back(a[i]);
		else if(i>=l&&i<=r) v2.push_back(a[i]),sum+=a[i];
		else v3.push_back(a[i]);
	}
	sort(v1.begin(),v1.end());
	sort(v2.begin(),v2.end(),cmp);
	sort(v3.begin(),v3.end());

	int sum1=sum,sum2=sum;
	for(int i=0;i<min(v2.size(),v1.size());i++){
		if(v1[i]<v2[i]) sum1-=(v2[i]-v1[i]);
	}
	for(int i=0;i<min(v2.size(),v3.size());i++){
		if(v3[i]<v2[i]) sum2-=(v2[i]-v3[i]);
	}
	cout << min(sum1,sum2) << endl;

}

signed main(){
	int t; cin >> t;
	while(t--){
		solve();
	}
	return 0;
}

Answer: 2


#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define ll long long int

int main(){
    int t;
    cin>>t;
    while(t--){
        int n,l,r;
        cin>>n>>l>>r;
        l--;
        r--;
        vector<ll> a(n);
        vector<ll> v;

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

       for(int i=r;i>=l;i--){
        v.push_back(a[i]);
        a.erase(a.begin()+i);
       }

       ll ma=0;
       ll mi=0;
       ll x=0;
       while(1){
         mi=*min_element(a.begin(),a.end());
         ma=*max_element(v.begin(),v.end());
          if(ma<=mi) break;
          if(a.empty() || v.empty()) break;
         v.push_back(mi);
         a.push_back(ma);
         v.erase(find(v.begin(),v.end(),ma));
         a.erase(find(a.begin(),a.end(),mi));

       }

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

       cout<<sum<<endl;

    }
}

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define ll long long int

int main(){
    int t;
    cin>>t;
    while(t--){
        int n,l,r;
        cin>>n>>l>>r;
        l--;
        r--;
        vector<ll> a(n);
        for(int i=0;i<n;i++){
            cin>>a[i];
        }

        vector<ll> lft;
        vector<ll> rht;

       for(int i=0;i<=r;i++){
        lft.push_back(a[i]);
       }

       for(int i=l;i<n;i++){
        rht.push_back(a[i]);
       }

       sort(lft.begin(),lft.end());
       sort(rht.begin(),rht.end());
       ll suml=0,sumr=0;
       for(int i=0;i<(r-l+1);i++){
         suml+=lft[i];
         sumr+=rht[i];
       }

       ll sum=min(suml,sumr);
       cout<<sum<<endl;
    }
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

#define int long long

#define endl '\n'

#define pii pair<int, int>

#define IOS                       \

    ios_base::sync_with_stdio(0); \

    cin.tie(0);                   \

    cout.tie(0);

constexpr int N = 5e5 + 10;

constexpr int mod = 1e9 + 7;

constexpr int inf = 0x3f3f3f3f;

void solve()

{

    int n, l, r;

    cin >> n >> l >> r;

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

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

        cin >> a[i];

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

    {

        b.push_back(a[i]);

    }

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

    {

        c.push_back(a[i]);

    }

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

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

    int sum1 = 0, sum2 = 0;

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

        sum1 += b[i];

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

        sum2 += c[i];

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

}

signed main()

{

    IOS

    int _ = 1;

    cin >> _;

    while(_ --)

    {

        solve();

    }

    return 0;

}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define pii pair<int, int>
#define IOS                       \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
constexpr int N = 5e5 + 10;
constexpr int mod = 1e9 + 7;
constexpr int inf = 0x3f3f3f3f;
void solve()
{
    int n, l, r;
    cin >> n >> l >> r;
    vector<int> a(n);
    for(int i = 0; i < n; i ++)
        cin >> a[i];
    sort(a.begin(), a.end());
    int sum = 0;
    for(int i = 0; i <= r - l; i ++)
        sum += a[i];
    cout << sum << endl;
}
signed main()
{
    IOS
    int _ = 1;
    cin >> _;
    while(_ --)
    {
        solve();
    }
    return 0;
}

Answer: 1


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

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        long long n,l,r,s1,s2;
        s1=s2=0;
        cin >> n >> l >> r;
        vector<long long> a(n),a1,a2;
        for (int i=0;i<n;i++)
        cin >> a[i];
        for (int i=0;i<r;i++)
        a1.push_back(a[i]);
        for (int i=l-1;i<n;i++)
        a2.push_back(a[i]);

        sort(a1.begin(),a1.end());
        sort(a2.begin(),a2.end());

        for (int i=0;i<r-l+1;i++)
        s1+=a1[i];
        for (int i=0;i<r-l+1;i++)
        s2+=a2[i];

        s1 = min(s1,s2);
        cout << s1 << "\n";
    }
}

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

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        long n,l,r,s1,s2;
        s1=s2=0;
        cin >> n >> l >> r;
        vector<long> a(n),a1,a2;
        for (int i=0;i<n;i++)
        cin >> a[i];
        for (int i=0;i<r;i++)
        a1.push_back(a[i]);
        for (int i=l-1;i<n;i++)
        a2.push_back(a[i]);

        sort(a1.begin(),a1.end());
        sort(a2.begin(),a2.end());

        for (int i=0;i<r-l+1;i++)
        s1+=a1[i];
        for (int i=0;i<r-l+1;i++)
        s2+=a2[i];

        s1 = min(s1,s2);
        cout << s1 << "\n";
    }
}

Answer: 1


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

#define int long long

#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

void fastio(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

const int maxN=2e3;
const int INF=1e9;
const int MOD=1e9+7;

int t, n, l, r, ans1, ans2, a;
vi arr1, arr2;

signed main(){
    fastio();
    cin >> t;
    while(t--){
        cin >> n >> l >> r;
        arr1.clear();
        arr2.clear();
        l--; r--;
        for(int i=0; i<n; i++){
            cin >> a;
            if(i<=r) arr1.pb(a);
            if(i>=l) arr2.pb(a);
        }
        ans1=ans2=0;
        sort(all(arr1));
        sort(all(arr2));
        for(int i=0; i<=r-l; i++){
            ans1+=arr1[i];
            ans2+=arr2[i];
        }
        cout << min(ans1, ans2) << endl;
    }
    return 0;
}

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

#define int long long

#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

void fastio(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

const int maxN=2e3;
const int INF=1e9;
const int MOD=1e9+7;

int t, n, l, r, ans1, ans2;
vi arr, a1, a2;

bool invComp(int a, int b){
    return a>=b;
}

signed main(){
    fastio();
    cin >> t;
    while(t--){
        cin >> n >> l >> r;
        arr.resize(n, 0);
        for(int i=0; i<n; i++){
            cin >> arr[i];
        }
        l--; r--;
        a1=a2=arr;
        sort(a1.begin(), a1.begin()+r+1, invComp);
        sort(a2.begin()+l, a2.end());
        ans1=ans2=0;
        for(int i=l; i<=r; i++){
            ans1+=a1[i];
            ans2+=a2[i];
        }
        cout << min(ans1, ans2) << endl;
    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>

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

using namespace std;

using ll = long long;

void solve() {
    ll n, l, r;
    cin >> n >> l >> r;
    vector<ll> a(n);
    for (ll i = 0; i < n; ++i) {
        cin >> a[i];
    }
    vector<ll> x, y;
    for (ll i = 0; i < r; ++i) {
        x.push_back(a[i]);
    }
    for (ll i = l - 1; i < n; ++i) {
        y.push_back(a[i]);
    }
    sort(all(x));
    sort(all(y));
    ll ans1 = 0, ans2 = 0;
    for (ll i = 0; i < r - l + 1; ++i) {
        ans1 += x[i];
    }
    for (ll i = 0; i < r - l + 1; ++i) {
        ans2 += y[i];
    }
    cout << min(ans1, ans2) << endl;
}

signed main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}

#include <bits/stdc++.h>

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

using namespace std;

using ll = long long;

void solve() {

    ll n, l, r;

    cin >> n >> l >> r;

    vector<ll> a(n);

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

        cin >> a[i];

    }

    sort(all(a));

    ll s = 0;

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

        s += a[i];

    }

    cout << s << endl;

}

signed main() {

    int t;

    cin >> t;

    while (t--) {

        solve();

    }

}

Answer: 1


#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int MAX_N = 1e5;

vector<vector<int>> adj(MAX_N,vector<int>());

vector<bool> processed(MAX_N,0);

void setIO(string s) {

	freopen((s + ".in").c_str(), "r", stdin);

	freopen((s + ".out").c_str(), "w", stdout);

}

ll gcd(ll y,ll z){

	if(z == 0) return y;

	return gcd(z,y%z);

}

vector<int> dist(1e5);

bool comp(int a, int b) {

    return a > b;

}

void solve(){

	int n; cin >> n;

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

	vector<int> first(0);

	vector<int> second(0);

	vector<int> third(0);

	l--; r--;

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

		int x; cin >> x;

		first.push_back(x);

	}

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

		int x; cin >> x;

		second.push_back(x);

	}

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

		int x; cin >> x;

		third.push_back(x);

	}

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

	sort(second.begin(),second.end(),comp);

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

	int res1 = 0;

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

		if(i<first.size() && i < second.size()) res1 += min(first[i], second[i]);

		else if (i>=first.size()) res1+= second[i];

		else res1+= first[i];

	}

	int res2 = 0;

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

		if(i<third.size() && i < second.size()) res2 += min(third[i], second[i]);

		else if (i>=third.size()) res2 += second[i];

		else res2+= third[i];

	}

	//cout << res1 << "\n" << res2 << "\n";

	cout << min(res1,res2);

}

int main(){

    ios::sync_with_stdio(0);

    cin.tie(nullptr);

    cout << fixed << setprecision(8);

	int t = 1;

	cin >> t;

	while(t--) {

		solve();

		cout << "\n";

	}

}

#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int MAX_N = 1e5;

vector<vector<int>> adj(MAX_N,vector<int>());

vector<bool> processed(MAX_N,0);

void setIO(string s) {

	freopen((s + ".in").c_str(), "r", stdin);

	freopen((s + ".out").c_str(), "w", stdout);

}

ll gcd(ll y,ll z){

	if(z == 0) return y;

	return gcd(z,y%z);

}

vector<int> dist(1e5);

bool comp(int a, int b) {

    return a > b;

}

void solve(){

	int n; cin >> n;

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

	vector<int> first(0);

	vector<int> second(0);

	vector<int> third(0);

	l--; r--;

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

		int x; cin >> x;

		first.push_back(x);

	}

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

		int x; cin >> x;

		second.push_back(x);

	}

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

		int x; cin >> x;

		third.push_back(x);

	}

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

	sort(second.begin(),second.end(),comp);

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

	ll res1 = 0;

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

		if(i<first.size() && i < second.size()) res1 += min(first[i], second[i]);

		else if (i>=first.size()) res1+= second[i];

		else res1+= first[i];

	}

	ll res2 = 0;

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

		if(i<third.size() && i < second.size()) res2 += min(third[i], second[i]);

		else if (i>=third.size()) res2 += second[i];

		else res2+= third[i];

	}

	//cout << res1 << "\n" << res2 << "\n";

	cout << min(res1,res2);

}

int main(){

    ios::sync_with_stdio(0);

    cin.tie(nullptr);

    cout << fixed << setprecision(8);

	int t = 1;

	cin >> t;

	while(t--) {

		solve();

		cout << "\n";

	}

}

Answer: 2


#include<iostream>

#include<vector>

#include<algorithm>

#include<unordered_map>

#include<utility>

#include<string>

using namespace std;

typedef long long ll;

int t;

bool comp(int a, int b){

    return a > b;

}

void solve(){

    int n, l, r;

    int sum = 0;

    cin>>n>>l>>r;

    vector<int> a(n);

    l--; r--;

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

        cin >> a[i];

        if(i >= l && i <= r) sum += a[i];

    }

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

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

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

    int ptr1 = 0, ptr2 = l, ptr3 = r + 1;

    int sum1 = 0, sum2 = 0, sum3 = 0;

    int ans = sum;

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

        sum1 += a[ptr1];

        sum2 += a[ptr2];

        ans = min(ans, sum - sum2 + sum1);

        ptr1++; ptr2++;

    }

    ptr2 = l;

    sum2 = 0;

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

        sum3 += a[ptr3];

        sum2 += a[ptr2];

        ans = min(ans, sum - sum2 + sum3);

        ptr3++; ptr2++;

    }

    cout<<ans;

    cout<<endl;

}

int main(){

    cin >> t;

    while(t--){

        solve();

    }

}

#include<iostream>

#include<vector>

#include<algorithm>

#include<unordered_map>

#include<utility>

#include<string>

using namespace std;

typedef long long ll;

int t;

bool comp(int a, int b){

    return a > b;

}

void solve(){

    int n, l, r;

    ll sum = 0;

    cin>>n>>l>>r;

    vector<int> a(n);

    l--; r--;

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

        cin >> a[i];

        if(i >= l && i <= r) sum += a[i];

    }

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

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

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

    int ptr1 = 0, ptr2 = l, ptr3 = r + 1;

    ll sum1 = 0, sum2 = 0, sum3 = 0;

    ll ans = sum;

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

        sum1 += a[ptr1];

        sum2 += a[ptr2];

        ans = min(ans, sum - sum2 + sum1);

        ptr1++; ptr2++;

    }

    ptr2 = l;

    sum2 = 0;

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

        sum3 += a[ptr3];

        sum2 += a[ptr2];

        ans = min(ans, sum - sum2 + sum3);

        ptr3++; ptr2++;

    }

    cout<<ans;

    cout<<endl;

}

int main(){

    cin >> t;

    while(t--){

        solve();

    }

}

Answer: 2


#include<bits/stdc++.h>

using namespace std;
#define LL long long
 int  main()
{
     long long t;
     cin>>t;
    while(t--)
    {
         long long n,l,r;
        cin>>n>>l>>r;
        vector<long long >a(n);
        vector<long long >b;
        vector<long long >c;
        for( long long i=0;i<n;i++)
        {
            cin>>a[i];
        }
        l--,r--;
        for( long long i=0;i<=r;i++)
        {
            b.push_back(a[i]);
        }
        for( long long i=l;i<n;i++)
        {
            c.push_back(a[i]);
        }
        sort(b.begin(),b.end());
        sort(c.begin(),c.end());
         long long sum1 = accumulate(b.begin(),b.begin()+int(r-l+1),0LL);
         long long sum2 = accumulate(c.begin(),c.begin()+int(r-l+1),0LL);
        cout<<min(sum1,sum2)<<endl;

    }
}

#include<bits/stdc++.h>

using namespace std;
#define LL long long
int main()
{
    int t;cin>>t;
    while(t--)
    {
        int n,l,r;
        cin>>n>>l>>r;
        vector<int>a(n);
        vector<int>b;
        vector<int>c;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        l--,r--;
        for(int i=0;i<=r;i++)
        {
            b.push_back(a[i]);
        }
        for(int i=l;i<n;i++)
        {
            c.push_back(a[i]);
        }
        sort(b.begin(),b.end());
        sort(c.begin(),c.end());
        int sum1 = accumulate(b.begin(),b.begin()+int(r-l+1),0);
        int sum2 = accumulate(c.begin(),c.begin()+int(r-l+1),0);
        cout<<min(sum1,sum2)<<endl;

    }
}

Answer: 1


#include <bits/stdc++.h>
#define ll long long
#define endl '\n'

using namespace std;

ll n, l, r;
signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

    int t; cin >> t;

    while(t--) {
        cin >> n >> l >> r;

        priority_queue<ll> mid_l, mid_r;
        priority_queue<ll, vector<ll>, greater<ll>> lqu, rqu;
        ll ans = 0, la = 0, ra = 0;

        for (int i = 1; i < l; i++) {
            ll e; cin >> e;
            lqu.push(e);
        }

        for (int i = l; i <= r; i++) {
            ll e; cin >> e;
            mid_l.push(e); mid_r.push(e);

            ans += e;
        }

        for (int i = r + 1; i <= n; i++) {
            ll e; cin >> e;
            rqu.push(e);
        }

        while(!lqu.empty() && !mid_l.empty()) {
            if (lqu.top() <= mid_l.top()) la += lqu.top() - mid_l.top();
            else break;

            lqu.pop(); mid_l.pop();
        }

        while(!rqu.empty() && !mid_r.empty()) {
            if (rqu.top() <= mid_r.top()) ra += rqu.top() - mid_r.top();
            else break;

            rqu.pop(); mid_r.pop();
        }

        cout << ans + min(min(ra, la), 0LL) << endl;
    }

    return 0;
}

#include <bits/stdc++.h>

#define ll long long

#define endl '\n'

using namespace std;

ll n, l, r;

signed main() {

    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

    int t; cin >> t;

    while(t--) {

        cin >> n >> l >> r;

        priority_queue<int> mid_l, mid_r;

        priority_queue<int, vector<int>, greater<int>> lqu, rqu;

        int ans = 0, la = 0, ra = 0;

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

            int e; cin >> e;

            lqu.push(e);

        }

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

            int e; cin >> e;

            mid_l.push(e); mid_r.push(e);

            ans += e;

        }

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

            int e; cin >> e;

            rqu.push(e);

        }

        while(!lqu.empty() && !mid_l.empty()) {

            if (lqu.top() <= mid_l.top()) la += lqu.top() - mid_l.top();

            else break;

            lqu.pop(); mid_l.pop();

        }

        while(!rqu.empty() && !mid_r.empty()) {

            if (rqu.top() <= mid_r.top()) ra += rqu.top() - mid_r.top();

            else break;

            rqu.pop(); mid_r.pop();

        }

        cout << ans + min(0, min(ra, la)) << endl;

    }

    return 0;

}

Answer: 1


//Problem Link :

#include<bits/stdc++.h>

#define ll long long int

#define endl '\n'

#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;

int main()

{

    fast;

    ll t;

    cin>>t;

    while(t--)

    {

        ll n,l,r,x;

        cin>>n>>l>>r;

        vector<ll>b,c;

        for(ll i=1;i<=n;i++)

        {

            cin>>x;

            if(x>=l && x<=n)b.push_back(x);

            if(x<=r && x>=1)c.push_back(x);

        }

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

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

        ll sum1=0,sum2=0;

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

        {

            sum1+=b[i];

            sum2+=c[i];

        }

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

    }

    return 0;

}

//Problem Link :

#include<bits/stdc++.h>

#define ll long long int

#define endl '\n'

#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;

int main()

{

    fast;

    ll t;

    cin>>t;

    while(t--)

    {

        ll n,l,r,x;

        cin>>n>>l>>r;

        vector<ll>b,c;

        for(ll i=1;i<=n;i++)

        {

            cin>>x;

            if(i>=l && i<=n)b.push_back(x);

            if(i<=r && i>=1)c.push_back(x);

        }

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

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

        ll sum1=0,sum2=0;

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

        {

            sum1+=b[i];

            sum2+=c[i];

        }

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

    }

    return 0;

}

Answer: 2


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

void input(vector<ll>&a,ll n){

  a.resize(n);
  for(ll i=0;i<n;i++){
    cin>>a[i];
  }

}
void printvec(vector<int>&a){
  for(auto num :a){
    cout<<num<<' ';
  }cout<<endl;
}

int main(){
   int test;
   cin>>test;
   while(test--){
    ll n,l,r;
    cin>>n>>l>>r;
    vector<ll>v(n);
    input(v,n);
    vector<ll>b,c;
    r--;l--;
    for(ll i=l;i<n;i++){
       b.push_back(v[i]);
    }
for(ll i=0;i<=r;i++){
  c.push_back(v[i]);
}
sort(b.begin(),b.end());
sort(c.begin(),c.end());

ll sum1 = accumulate(b.begin(),b.begin()+(int)(r-l+1),0LL);
ll sum2 = accumulate(c.begin(),c.begin()+(int)(r-l+1),0LL);
cout<<min(sum1,sum2) <<endl;

}}

#include <bits/stdc++.h>

using namespace std;

#define ll long long

void input(vector<int>&a,int n){

  a.resize(n);

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

    cin>>a[i];

  }

}

void printvec(vector<int>&a){

  for(auto num :a){

    cout<<num<<' ';

  }cout<<endl;

}

int main(){

   int test;

   cin>>test;

   while(test--){

    int n,l,r;

    cin>>n>>l>>r;

    vector<int>v(n);

    input(v,n);

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

    int ans=r-l+1;

    int sum=0;

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

      sum+=v[i];

    }

    cout<<sum<<endl;

}}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define endl "\n"

//     priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq; for 3 values input
//cout << get<0>(pq.top()) << endl;
// priority_queue<tuple<int, int, int>> pq;

#define optimizer() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define fraction(a) cout.unsetf(ios::fixed); cout.precision(a); cout.setf(ios::fixed);

void anik() {
ll n,l,r;
cin>>n>>l>>r;
vector<ll> v(n);
for(int i=0;i<n;i++)
{
      cin>>v[i];
}
sort(v.begin(),v.end());
ll dif=r-l;
ll sum=0;
for(int i=0;i<=dif;i++)
{
sum+=v[i];
}

cout<<sum<<endl;

}

int main() {
    optimizer();

    int test;
    cin >> test;
    while (test--) {
        anik();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define endl "\n"

//     priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> pq; for 3 values input
//cout << get<0>(pq.top()) << endl;
// priority_queue<tuple<int, int, int>> pq;

#define optimizer() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define fraction(a) cout.unsetf(ios::fixed); cout.precision(a); cout.setf(ios::fixed);

void anik() {
ll n,l,r;
cin>>n>>l>>r;
l--;r--;

ll sum=0;
vector<ll> v(n),be,in,af;
for(int i=0;i<n;i++)
{
      cin>>v[i];
      if(i<l){
        be.push_back(v[i]);
      }
       if(i>=l and r>=i){
        in.push_back(v[i]);
        sum+=v[i];
      }

        else if(i>r){
            af.push_back(v[i]);
        }
}

// for(int i=0;i<be.size();i++){
//     cout<<be[i]<<" ";
// }
// cout<<endl;
// for(int i=0;i<in.size();i++){
//     cout<<in[i]<<" ";
// }
// cout<<endl;
// for(int i=0;i<af.size();i++){
//     cout<<af[i]<<" ";
// }
// cout<<endl;

sort(be.begin(),be.end());
sort(in.begin(),in.end(),greater<ll>());
sort(af.begin(),af.end());

 ll sum1=sum;

int j=0;
int x=0;
while(!be.empty() and j<in.size() and x<be.size()){
    if(be[x]<in[j]){
     sum-=in[j];
     sum+=be[j];
     x++;
    }
    j++;

}
j=0;
x=0;

while(!af.empty() and j<in.size() and x<af.size()){
    if(af[x]<in[j]){
     sum1-=in[j];
     sum1+=af[j];
     x++;
    }
    j++;

}

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

int main() {
    optimizer();

    int test;
    cin >> test;
    while (test--) {
        anik();
    }
    return 0;
}

Answer: 2


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

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

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

       int mid=(low+high)/2;
       int left=query(2*ind+1,low,mid,l,r);
       int right=query(2*ind+2,mid+1,high,l,r);

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

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

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

    vll left, mid, right;

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

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

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

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

    ll cur = sum;

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

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

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

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

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

    cin>>t;
    while(t--)
        solve_Func();

}

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

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

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

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

       int mid=(low+high)/2;
       int left=query(2*ind+1,low,mid,l,r);
       int right=query(2*ind+2,mid+1,high,l,r);

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

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

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

    vll left, mid, right;

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

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

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

    ans = sum;

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

    ll cur = sum;

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

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

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

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

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

    cin>>t;
    while(t--)
        solve_Func();

}

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

Answer: 2


#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define co cout
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
#define ci cin
#define ins insert
#define pb push_back
#define intl int
#define fi first
#define sc second
#define RODI ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define beg begin()
#define ed end()
using namespace std;
int a[1000009];
int b[1000009];
int ac[100];
const int mod = 1e9+7;
const int siz=1e5;
bool ch=false;
string s,s1,s2,s3,s4,s5;
int t=0,sum=0,i,w,m=0,n=0,n2,n3,n4,x,y=0,k=-1,st=0,cc=0,q,sb,sa,zero=0,rodi,idx,cost,l,d,mode=1,N=1e5+1,mx=-1,z,r,cnt,mid,r2,l2,mid2,sum2,mi,j=0,r1,l1,f,x1,x2,y2;
bool check;
int ar[4009][4009];
int inf=1e9;
string arr[1009];
int p[10000009];
set<int>Se;
vector<int>vec;
map<int,multiset<int>>sm;
vector<int>vec1,vec2,vec3;
stack<int>stk;
vector<int>dpi(2e5,0);
vector<int>adj[200009];
map<int,int>freq,fr1,fr2;
map<int,set<int> >ind;
ll power(ll x , ll y)
{

ll res = 1;
//x %= mod;
while(y > 0)
{
if (y%2)res = (res*x);//%mod;
y /= 2;
x = (x*x);//%mod;
}
return res;
}
deque<int>deq;
vector<int>vis(2e5+9,0);
vector<int>dis(2e5+9,0);
string solver(string s1,string s2)
{
string   s3="";
for(int i=0;i<s1.size();i++)
{
if(s1[i]==s2[i])
s3+='0';
else
s3+='1';
}
return s3;
}
map<int,set<int>>fse;
void solve()
{

    freq.clear();
    Se.clear();
    vec1.clear();
    vec2.clear();
    vec3.clear();
        ci>>n>>l>>r;
        sum=0;
        sum2=0;
        for(i=1;i<=n;i++)
        {
            ci>>a[i];
            if(i<l)
                vec1.pb(a[i]);
            else if(i>r)
                vec3.pb(a[i]);
            else{
                vec2.pb(a[i]);
                sum+=a[i];
                sum2+=a[i];
        }
        }
        sort(vec1.rbegin(),vec1.rbegin());
        sort(vec2.begin(),vec2.begin());
        sort(vec3.rbegin(),vec3.rbegin());
        for(i=vec2.size()-1;i>=0&&vec1.size();i--)
        {
                    if(vec2[i]<vec1.back())
                        break;
                        if(vec2[i]>vec1.back()){
                    sum-=vec2[i];
                    sum+=vec1.back();
                    vec1.pop_back();
                        }
        }

  for(i=vec2.size()-1;i>=0&&vec3.size();i--)
        {
                    if(vec2[i]<vec3.back())
                        break;
                        if(vec2[i]>vec3.back()){
                    sum2-=vec2[i];
                    sum2+=vec3.back();
                    vec3.pop_back();
                        }
        }
        co<<min(sum,sum2)<<endl;

}
/*

*/

int32_t main()
{
RODI;
t=1;
ci>>t;

while(t--){
solve();

}
return 0;
}

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define co cout
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
#define ci cin
#define ins insert
#define pb push_back
#define intl int
#define fi first
#define sc second
#define RODI ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define beg begin()
#define ed end()
using namespace std;
int a[1000009];
int b[1000009];
int ac[100];
const int mod = 1e9+7;
const int siz=1e5;
bool ch=false;
string s,s1,s2,s3,s4,s5;
int t=0,sum=0,i,w,m=0,n=0,n2,n3,n4,x,y=0,k=-1,st=0,cc=0,q,sb,sa,zero=0,rodi,idx,cost,l,d,mode=1,N=1e5+1,mx=-1,z,r,cnt,mid,r2,l2,mid2,sum2,mi,j=0,r1,l1,f,x1,x2,y2;
bool check;
int ar[4009][4009];
int inf=1e9;
string arr[1009];
int p[10000009];
set<int>Se;
vector<int>vec;
map<int,multiset<int>>sm;
vector<int>vec1,vec2,vec3;
stack<int>stk;
vector<int>dpi(2e5,0);
vector<int>adj[200009];
map<int,int>freq,fr1,fr2;
map<int,set<int> >ind;
ll power(ll x , ll y)
{

ll res = 1;
//x %= mod;
while(y > 0)
{
if (y%2)res = (res*x);//%mod;
y /= 2;
x = (x*x);//%mod;
}
return res;
}
deque<int>deq;
vector<int>vis(2e5+9,0);
vector<int>dis(2e5+9,0);
string solver(string s1,string s2)
{
string   s3="";
for(int i=0;i<s1.size();i++)
{
if(s1[i]==s2[i])
s3+='0';
else
s3+='1';
}
return s3;
}
map<int,set<int>>fse;
void solve()
{

    freq.clear();
    Se.clear();
    vec1.clear();
    vec2.clear();
    vec3.clear();
        ci>>n>>l>>r;
        sum=0;
        sum2=0;
        for(i=1;i<=n;i++)
        {
            ci>>a[i];
            if(i<l)
                vec1.pb(a[i]);
            else if(i>r)
                vec3.pb(a[i]);
            else{
                vec1.pb(a[i]);
                vec3.pb(a[i]);
        }
        }
        sort(vec1.rbegin(),vec1.rend());
        sort(vec3.rbegin(),vec3.rend());
        x=r-l+1;
        y=x;
       while(x--)
       {
           sum+=vec1.back();
           vec1.pop_back();
       }
       while(y--)
       {
           sum2+=vec3.back();
           vec3.pop_back();
       }
       co<<min(sum,sum2)<<endl;

}
/*

*/

int32_t main()
{
RODI;
t=1;
ci>>t;

while(t--){
solve();

}
return 0;
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

typedef long long ll;

void Func() {

    ll n, l, r;

    cin >> n >> l >> r;

    priority_queue<ll, vector<ll>, greater<> > q2,q1;

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

        ll x;

        cin >> x;

        if (i<=r) q1.push(x);

        if (i>=l) q2.push(x);

    }

    ll ans = 0,ans2 = 0;

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

        ans += q1.top();

        q1.pop();

        ans2 += q2.top();

        q2.pop();

    }

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

}

int main() {

#ifdef LOCAL

    freopen("Testlib.in", "r", stdin);

    freopen("Code.out", "w", stdout);

#endif

    int T;

    cin >> T;

    while (T--) {

        Func();

    }

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

typedef long long ll;

void Func() {

    ll n, l, r;

    cin >> n >> l >> r;

    priority_queue<ll, vector<ll>, greater<ll> > q2;

    priority_queue<ll> q1;

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

        ll x;

        cin >> x;

        if (l <= i && i <= r) {

            q1.push(x);

        } else {

            q2.push(x);

        }

    }

    while (!q2.empty() && !q1.empty() && q1.top() > q2.top()) {

        q1.pop();

        q1.push(q2.top());

        q2.pop();

    }

    ll ans = 0;

    while (!q1.empty()) {

        ans += q1.top();

        q1.pop();

    }

    cout << ans << endl;

}

int main() {

    int T;

    cin >> T;

    while (T--) {

        Func();

    }

    return 0;

}

Answer: 1


#include <bits/stdc++.h>

using namespace std;

#define int 			long long int

#define double			long double

#define F			first

#define S 			second

#define pb 			push_back

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

#define vi 			vector<int>

#define pii			pair<int, int>

#define rep(i, a, b) 		for(int i = a; i < b; i++)

#define CODE_START 		ios_base::sync_with_stdio();cin.tie(0);cout.tie(0);

void solve() {

	int n, l, r;

	cin >> n >> l >> r;

	vi a(n);

	rep(i, 0, n) cin >> a[i];

	int curr_sum = 0, sum = 0, len = 0, d = r-l+1;

	rep(i, l-1, r) curr_sum += a[i];

	int mn = curr_sum;

	vi prefix(d+1);

	rep(i, 1, d+1) prefix[i] = prefix[i-1] + a[l+i-2];

	rep(i, 0, r-1) {

		if(sum + a[i] < curr_sum) {

			sum += a[i];

			len++;

		}

		else {

			sum = a[i];

			len = 1;

		}

		mn = min(mn, curr_sum - prefix[len] + sum);

		if(len == d) {

			sum -= a[i-r+l];

			len--;

		}

	}

	vi suffix(d+1);

	for(int i = d-1; i >= 0; i--) suffix[i] = suffix[i+1] + a[r-d+i];

	sum = 0;

	len = 0;

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

		if(sum + a[i] < curr_sum) {

			sum += a[i];

			len++;

		}

		else {

			sum = a[i];

			len = 1;

		}

		mn = min(mn, curr_sum - suffix[d-len] + sum);

		if(len == d) {

			sum -= a[i+r-l];

			len--;

		}

	}

	cout << mn << "\n";

}

int32_t main() {

	#ifdef CODE_START

		CODE_START;

	#endif

	int t;

	cin >> t;

	while(t--) {

		solve();

	}

	return 0;

}

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

#define int 			long long int
#define double			long double
#define F			first
#define S 			second
#define pb 			push_back
#define all(x) 			x.begin(), x.end()
#define vi 			vector<int>
#define pii			pair<int, int>
#define rep(i, a, b) 		for(int i = a; i < b; i++)
#define CODE_START 		ios_base::sync_with_stdio();cin.tie(0);cout.tie(0);

void solve() {
	int n, l, r;
	cin >> n >> l >> r;
	vi a(n);
	rep(i, 0, n) cin >> a[i];
	vi b;
	rep(i, l-1, r) b.pb(a[i]);
	sort(b.rbegin(), b.rend());
	int d = r-l+1;
	vi prefix(d+1);
	rep(i, 0, d) prefix[i+1] = prefix[i] + b[i];

	vi c;
	rep(i, 0, l-1) c.pb(a[i]);
	sort(all(c));

	int sum = 0, mn = prefix[d], len = 0;
	rep(i, 0, (int)c.size()) {
		if(sum + c[i] < prefix[i+1]) {
			sum += c[i];
			mn = min(mn, prefix[d] - prefix[i+1] + sum);
		}
		else break;
		len++;
		if(len == d) break;
	}

	vi e;
	for(int i = n-1; i >= r; i--) e.pb(a[i]);
	sort(all(e));

	sum = 0;
	len = 0;
	rep(i, 0, (int)e.size()) {
		if(sum + e[i] < prefix[i+1]) {
			sum += e[i];
			mn = min(mn, prefix[d] - prefix[i+1] + sum);
		}
		else break;
		len++;
		if(len == d) break;
	}

	cout << mn << "\n";
}

int32_t main() {
	#ifdef CODE_START
		CODE_START;
	#endif

	int t;
	cin >> t;
	while(t--) {
		solve();
	}
	return 0;
}

Answer: 2


// B. Subsequence Update

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

const int N = 2e5 + 10;
int gA[N];

signed main() {
	ios::sync_with_stdio(false);

	int t;
	cin >> t;

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

		for (int i = 1; i <= n; i++)
			cin >> gA[i];

		priority_queue<int, vector<int>, greater<int> > lft, rht;
		priority_queue<int> mid;

		int ans = 0;
		for (int i = l; i <= r; i++)
			ans += gA[i];

		int tmpans = ans;
		for (int i = 1; i < l; i++) lft.push(gA[i]);
		for (int i = l; i <= r; i++) mid.push(gA[i]);
		for (int i = r + 1; i <= n; i++) rht.push(gA[i]);

		auto tmp = mid;
		while (lft.size() && mid.size() && lft.top() < mid.top()) ans -= mid.top() - lft.top(), mid.pop(), lft.pop();

		int mn = ans; ans = tmpans;
		while (rht.size() && mid.size() && rht.top() < mid.top()) ans -= mid.top() - rht.top(), mid.pop(), rht.pop();

		cout << min(mn, ans) << endl;
	}

	return 0;
}

// B. Subsequence Update

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 2e5 + 10;

int gA[N];

signed main() {

	ios::sync_with_stdio(false);

	int t;

	cin >> t;

	while (t--) {

		int n, l, r;

		cin >> n >> l >> r;

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

			cin >> gA[i];

		priority_queue<int, vector<int>, greater<int> > lft, rht;

		priority_queue<int> mid;

		int ans = 0;

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

			ans += gA[i];

		int tmpans = ans;

		for (int i = 1; i < l; i++) lft.push(gA[i]);

		for (int i = l; i <= r; i++) mid.push(gA[i]);

		for (int i = r + 1; i <= n; i++) rht.push(gA[i]);

		auto tmp = mid;

		while (lft.size() && mid.size() && lft.top() < mid.top()) ans -= mid.top() - lft.top(), mid.pop(), lft.pop();

		int mn = ans; ans = tmpans; mid = tmp;

		while (rht.size() && mid.size() && rht.top() < mid.top()) ans -= mid.top() - rht.top(), mid.pop(), rht.pop();

		cout << min(mn, ans) << endl;

	}

	return 0;

}

Answer: 2


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

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

    vector<int> v(n);
    for(auto &x : v) cin >> x;

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

    long long sum = 0;
    for(int i = 0; i < (r - l + 1); i++){
        sum += v[i];
    }

    cout << sum << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int t; cin >> t;
    while (t--) solve();
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define mod 1000000007

void solve(){

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

    vector<int> v(n);

    for(auto &x : v) cin >> x;

    if(l == 1 || r == n){

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

        long long sum = 0;

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

            sum += v[i];

        }

        cout << sum << "\n";

        return;

    }

    vector<int> left, right;

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

        if(i+1 < l){

            left.push_back(v[i]);

        }else if(i + 1 > r){

            right.push_back(v[i]);

        }else{

            left.push_back(v[i]);

            right.push_back(v[i]);

        }

    }

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

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

    long long suml = 0;

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

        suml += left[i];

    }

    long long sumr = 0;

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

        sumr += right[i];

    }

    cout << min(suml, sumr) << "\n";

}

int main() {

    ios::sync_with_stdio(0);

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

    int t; cin >> t;

    while (t--) solve();

    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);

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

            cin >> v[i];

        }

        l--;

        vector<int> u(v.begin() + l, v.end());

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

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

        long long sum_w = accumulate(v.begin(), v.begin() + r-l , 0);

        long long sum_u = accumulate(u.begin(), u.begin() + r - l, 0);

        cout << min(sum_w, sum_u) << endl;

    }

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

int main() {

    int t, n, l, r, i, a[200000];

    cin >> t;

    while (t--) {

        cin >> n >> l >> r;

        l--; // Convert to 0-based index

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

            cin >> a[i];

        }

        vector<int> c(a + l, a + n);

        sort(a, a + r);

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

        cout << min(accumulate(a, a + r - l, 0ll), accumulate(c.begin(), c.begin() + r - l, 0ll)) << ' ';

    }

}

Answer: 2


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=100005;

ll a[N],b[N],c[N];

void solve(){

    ll n,l,r;cin>>n>>l>>r;

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

        cin>>a[i];

        b[i]=a[i],c[i]=a[i];

    }

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

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

    ll ans1=0,ans2=0;

    for(int i=1;i<=r-l+1;i++) ans1+=b[i];

    for(int i=l;i<=r;i++) ans2+=c[i];

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

}

int main()

{

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

    int T;cin>>T;

    while(T--){

        solve();

    }

    return 0;

}

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N=100005;

ll a[N],b[N],c[N];

void solve(){

    ll n,l,r;cin>>n>>l>>r;

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

        cin>>a[i];

        b[i]=a[i],c[i]=a[i];

    }

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

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

    ll ans1=0,ans2=0;

    for(int i=1;i<=r-l+1;i++) ans1+=b[i];

    for(int i=l;i<=r;i++) ans2+=c[i];

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

}

int main()

{

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

    int T;cin>>T;

    while(T--){

        solve();

    }

    return 0;

}

Answer: 2


#include<bits/stdc++.h>

using namespace std;

#define int long long

const int N=1e5+7;

int q[N],ql[N],qr[N];

void slove(){

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

//	vector<int>q(n+5);

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

	cin>>q[i];

	ql[i]=q[i];

	qr[i]=q[i];

	}

	sort(ql,ql+r+1);

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

	int ans=0,num=r-l+1;

	int cntl=0,cntr=0;

	for(int i=1;i<=num;i++)

		cntl+=ql[i];

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

	cntr+=qr[i];

	ans=min(cntl,cntr);

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

    cout<<ans<<'\n';

}

signed main(){

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

    int t=1;

	cin>>t;

	while(t--){

		slove();

	}

}

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+7;

int q[N],ql[N],qr[N];

void slove(){
	int n,l,r;  cin>>n>>l>>r;
//	vector<int>q(n+5);

	for(int i=1;i<=n;i++){
	cin>>q[i];
	ql[i]=q[i];
	qr[i]=q[i];
	}

	sort(ql,ql+1+r);
	sort(qr+l,qr+1+n);

	int ans=0,num=r-l+1;
	int cntl=0,cntr=0;
	for(int i=1;i<=num;i++){
		cntl+=ql[i];
		cntr+=qr[i];
	}
	ans=min(cntl,cntr);

//	for(int i=1;i<=n;i++)
    cout<<ans<<'\n';
}

signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t=1;
	cin>>t;

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

}

Answer: 1


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,l,r,x;
		ll y=0;
		int a[100000];
		cin>>n>>l>>r;
		x=r-l+1;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		sort(a+1,a+n+1);
		for(int i=1;i<=x;i++)
		{
			y+=a[i];
		}
		cout<<y<<endl;
	}
	return 0;
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;
//int main()
//{
//	int t;
//	cin>>t;
//	while(t--)
//	{
//		int n,l,r,x;
//		ll y=0;
//		int a[100000];
//		cin>>n>>l>>r;
//		x=r-l+1;
//		for(int i=1;i<=n;i++)
//		{
//			cin>>a[i];
//		}
//		sort(a+1,a+n+1);
//		for(int i=1;i<=x;i++)
//		{
//			y+=a[i];
//		}
//		cout<<y<<endl;
//	}
//	return 0;
//}
//只能一次反转
void solve()
{
	int n,l,r;
	cin>>n>>l>>r;
	ll res=0;
	vector<int>a(n+1);
	for(int i=1;i<=n;i++)
		cin>>a[i];
	priority_queue<int> tmpl_ans,tmpr_ans;
	//tmpl_ans 和 tmpr_ans 是最大堆，用于存储区间 [l, r] 内的元素。
    priority_queue<int,vector<int>,greater<int>> lans,rans;
	//lans 和 rans 是最小堆，分别用于存储区间 [1, l-1] 和 [r+1, n] 的元素。
	for(int i=l;i<=r;i++)
	{
		res+=a[i];
		tmpl_ans.push(a[i]);
		tmpr_ans.push(a[i]);
	}
	for(int i=1;i<l;i++)
		lans.push(a[i]);
	for(int i=r+1;i<=n;i++)
		rans.push(a[i]);

	ll res1=res;

  while (!lans.empty()){
    int now1 = tmpl_ans.top(),now2 = lans.top();
    if (now1>now2){
      res -= now1-now2;
      tmpl_ans.pop();
      tmpl_ans.push(now2);
      lans.pop();
    }else{
      break;
    }
  }
  while (!rans.empty()){
    int now1 = tmpr_ans.top(),now2 = rans.top();//最大元素
    if (now1>now2){
      res1 -= now1-now2;
      tmpr_ans.pop();//移除堆顶的最大元素
      tmpr_ans.push(now2);//将新的较小元素 now2 插入堆中
      rans.pop();
    }else{
      break;
    }
  }
  cout<<min(res1,res)<<endl;
}

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

Answer: 2


import java.util.*;

public class B_Subsequence_Update{

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);

        int t = s.nextInt();

        while (t-->0){

            int n = s.nextInt();

            int l = s.nextInt();

            int r = s.nextInt();

            int[] arr = new int[n];

            for (int i=0;i<n;++i) arr[i] = s.nextInt();

            PriorityQueue<Integer> maxpq = new PriorityQueue<>((a,b)->b-a);

            PriorityQueue<Integer> minpql = new PriorityQueue<>();

            PriorityQueue<Integer> minpqr = new PriorityQueue<>();

            // ArrayList<Integer> raw = new ArrayList<>();

            int grr = 0;

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

                maxpq.add(arr[i]);

                grr += arr[i];

            }

            for (int i=0;i<l-1;++i) minpql.add(arr[i]);

            for (int i=r;i<n;++i) minpqr.add(arr[i]);

            int sw = 0, sol = 0, sor = 0, ans = grr;

            while (!maxpq.isEmpty() && !minpql.isEmpty()){

                sw += maxpq.poll();

                sol += minpql.poll();

                ans = Math.min(ans,grr-sw+sol);

                // raw.add(sol);

                if (sol >= sw) break;

            }

            maxpq.clear();

            sw = 0;

            for (int i=l-1;i<r;++i) maxpq.add(arr[i]);

            while (!maxpq.isEmpty() && !minpqr.isEmpty()){

                sw += maxpq.poll();

                sor += minpqr.poll();

                ans = Math.min(ans,grr-sw+sor);

                // raw.add(sor);

                if (sor >= sw) break;

            }

            System.out.println(ans);

        }

    }

}

import java.util.*;

public class B_Subsequence_Update{

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);

        int t = s.nextInt();

        while (t-->0){

            int n = s.nextInt();

            int l = s.nextInt();

            int r = s.nextInt();

            int[] arr = new int[n];

            for (int i=0;i<n;++i) arr[i] = s.nextInt();

            PriorityQueue<Integer> maxpq = new PriorityQueue<>((a,b)->b-a);

            PriorityQueue<Integer> minpql = new PriorityQueue<>();

            PriorityQueue<Integer> minpqr = new PriorityQueue<>();

            // ArrayList<Integer> raw = new ArrayList<>();

            long grr = 0;

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

                maxpq.add(arr[i]);

                grr += arr[i];

            }

            for (int i=0;i<l-1;++i) minpql.add(arr[i]);

            for (int i=r;i<n;++i) minpqr.add(arr[i]);

            long sw = 0, sol = 0, sor = 0;

            long ans = grr;

            while (!maxpq.isEmpty() && !minpql.isEmpty()){

                sw += maxpq.poll();

                sol += minpql.poll();

                ans = Math.min(ans,grr-sw+sol);

                // raw.add(sol);

                if (sol >= sw) break;

            }

            maxpq.clear();

            sw = 0;

            for (int i=l-1;i<r;++i) maxpq.add(arr[i]);

            while (!maxpq.isEmpty() && !minpqr.isEmpty()){

                sw += maxpq.poll();

                sor += minpqr.poll();

                ans = Math.min(ans,grr-sw+sor);

                // raw.add(sor);

                if (sor >= sw) break;

            }

            System.out.println(ans);

        }

    }

}

Answer: 2


#include <bits/stdc++.h>

using namespace std;
using vi = vector<int>;

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

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

    vi brr = arr;
    sort(brr.begin() + l, brr.end());

    vi crr = arr;
    sort(crr.begin(), crr.begin() + r, greater<int>());

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

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

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

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

#include <bits/stdc++.h>

using namespace std;
using vi = vector<int>;
using ll = long long;

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

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

    vi brr = arr;
    sort(brr.begin() + l, brr.end());

    vi crr = arr;
    sort(crr.begin(), crr.begin() + r, greater<int>());

    ll sum_brr = accumulate(brr.begin() + l, brr.begin() + r, 0LL);
    ll sum_crr = accumulate(crr.begin() + l, crr.begin() + r, 0LL);

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

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

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

Answer: 2


// Handle: akhilpranjal

// Date: 05-02-2025

// Time: 17:12

#include<bits/stdc++.h>

using namespace std;

#define rep(i,a,b) for (int i=a;i<b;i++)

#define vll vector<long long>

#define ll long long

#define ull unsigned long long

#define ld long double

#define F first

#define S second

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

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

// Operator Overloads:

template<typename T1, typename T2>     // cin >> pair<T1,T2>

istream& operator>>(istream &istream, pair<T1,T2> &p) { return (istream >> p.first >> p.second); }

template<typename T>     // cin >> vector<T> &v

istream& operator>>(istream &istream, vector<T> &v)

{

    for(auto &it : v)

    {

        cin >> it;

    }

    return istream;

}

template<typename T1, typename T2>     // cout << pair<T1,T2>

ostream& operator<<(ostream &ostream, const pair<T1,T2> &p) { return(ostream << p.first << " " << p.second); }

template<typename T>     // cout << vector<T>

ostream& operator<<(ostream &ostream, const vector<T> &v) { for (auto &it : v) cout << it << " "; return ostream; }

void solve()

{

    ll n,l,r,sum1=0,sum2=0;

    cin>>n>>l>>r;

    l--;r--;

    vll a(n),b;

    cin>>a;

    b=a;

    // ll x = r-l+1;

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

    rep(i,0,r-l+1)

    {

        sum1 += a[i];

    }

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

    rep(i,l,r+1)

    {

        sum2 += b[i];

    }

    cout<<min(sum1,sum2);

}

int main()

{

    ios::sync_with_stdio(0);

    cin.tie(0);

    ll t;

    cin >> t;

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

        solve();

        cout<<endl;

    }

    return 0;

}

// Handle: akhilpranjal

// Date: 05-02-2025

// Time: 17:12

#include<bits/stdc++.h>

using namespace std;

#define rep(i,a,b) for (int i=a;i<b;i++)

#define vll vector<long long>

#define ll long long

#define ull unsigned long long

#define ld long double

#define F first

#define S second

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

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

// Operator Overloads:

template<typename T1, typename T2>     // cin >> pair<T1,T2>

istream& operator>>(istream &istream, pair<T1,T2> &p) { return (istream >> p.first >> p.second); }

template<typename T>     // cin >> vector<T> &v

istream& operator>>(istream &istream, vector<T> &v)

{

    for(auto &it : v)

    {

        cin >> it;

    }

    return istream;

}

template<typename T1, typename T2>     // cout << pair<T1,T2>

ostream& operator<<(ostream &ostream, const pair<T1,T2> &p) { return(ostream << p.first << " " << p.second); }

template<typename T>     // cout << vector<T>

ostream& operator<<(ostream &ostream, const vector<T> &v) { for (auto &it : v) cout << it << " "; return ostream; }

void solve()

{

    ll n,l,r,sum=0;

    cin>>n>>l>>r;

    vll a(n);

    cin>>a;

    sort(all(a));

    rep(i,0,r-l+1)

    {

        sum +=a[i];

    }

    cout<<sum;

}

int main()

{

    ios::sync_with_stdio(0);

    cin.tie(0);

    ll t;

    cin >> t;

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

        solve();

        cout<<endl;

    }

    return 0;

}

Answer: 1


#include <bits/stdc++.h>

using namespace std;

void solve() {

    int n, l, r;

    cin >> n >> l >> r;

    l--;

    r--;

    vector<long long> a(n);

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

    int size = r - l + 1;

    vector<long long> first(a.begin(), a.begin() + r + 1);

    vector<long long> second(a.begin() + l, a.end());

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

    long long res =  LONG_LONG_MAX;

    long long sum = 0;

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

        sum += first[i];

    }

    res = min(sum, res);

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

    sum = 0;

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

        sum += second[i];

    }

    res = min(sum, res);

    cout << res << endl;

}

int main() {

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

    int t;

    cin >> t;

    while (t--) {

        solve();

    }

}

#include <bits/stdc++.h>

using namespace std;

void solve() {

    int n, l, r;

    cin >> n >> l >> r;

    l--;

    r--;

    vector<long long> a(n);

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

    int size = r - l + 1;

    vector first(a.begin(), a.begin() + r + 1);

    vector second(a.begin() + l, a.end());

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

    long long res =  INT_MAX;

    long long sum = 0;

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

        sum += first[i];

    }

    res = min(sum, res);

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

    sum = 0;

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

        sum += second[i];

    }

    res = min(sum, res);

    cout << res << endl;

}

int main() {

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

    int t;

    cin >> t;

    while (t--) {

        solve();

    }

}

Answer: 1


/******************************************************************************

                              Online C++ Compiler.

               Code, Compile, Run and Debug C++ program online.

Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

int main()

{

    int t;

    cin>>t;

    while(t--){

        int n,l,r;

        cin>>n>>l>>r;

        vector<int>v(n);

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

            cin >> v[i];

        }

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

        int total = r-l+1;

        int answer = 0;

        int i = 0;

        while(total--){

            answer += v[i++];

        }

        cout<<answer<<endl;

    }

    return 0;

}

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int t;
    cin>>t;

    while(t--){
        int n,l,r;
        cin>>n>>l>>r;
        vector<int>v(n);
        for(int i = 0;i<n;i++){
            cin >> v[i];
        }

        long long leftSum = 0;
        long long rightSum = 0;
        vector<int>left;
        vector<int>right;

        for(int i = 0;i<r;i++){
            left.push_back(v[i]);
        }
        for(int i = l-1;i<n;i++){
            right.push_back(v[i]);
        }

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

        for(int i = 0;i<r-l+1;i++){
            leftSum += left[i];
            rightSum += right[i];
        }

        cout<<min(leftSum,rightSum)<<endl;

    }

    return 0;
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
void solve(ll tc)
{
    ll n,l,r,i,s1=0,s2=0,a,p;
    cin>>n>>l>>r;
    p=r-l+1;
    vector<ll>v1,v2;
    for(i=0; i<n; i++)
    {
        cin>>a;
        if(i<r) v1.pb(a);
        if(i>=l-1) v2.pb(a);
    }
    sort(v1.begin(),v1.end());
    sort(v2.begin(),v2.end());
    for(i=0; i<p; i++) s1+=v1[i],s2+=v2[i];
    ll s=min(s1,s2);
    cout<<s<<endl;

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tc=1;
    cin>>tc;
    for(ll i=1; i<=tc; i++)
    {
        solve(i);
    }
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
void solve(ll tc)
{
ll n,l,r,i,s=0,p;
cin>>n>>l>>r;
p=r-l+1;
vector<ll>v(n);
for(i=0;i<n;i++) cin>>v[i];
sort(v.begin(),v.end());
for(i=0;i<p;i++) s+=v[i];
cout<<s<<endl;

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tc=1;
    cin>>tc;
    for(ll i=1; i<=tc; i++)
    {
        solve(i);
    }
}

Answer: 1


#include <iostream>

#include <algorithm>

#include <cstring>

#define ll long long

const int N = 1e5 + 8;

using namespace std;

int a[N];

void solve() {

    memset(a , sizeof a, 0);

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

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

    auto b = a;

    ll sum1 = 0, sum2 = 0;

    sort(a , a + r);

    for (int i = 0 ; i < r - l + 1; i ++) sum1 += a[i];

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

    for (int i = l - 1 ; i < l - 1 + r - l + 1; i ++) sum2 +=b[i];

    ll ans = min(sum1, sum2);

    cout << ans << endl;

}

int main(void) {

    ios::sync_with_stdio(false);cin.tie(nullptr);

    int t ; cin >> t;

    while (t--)solve();

}

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#define ll long long
const int N = 1e5 + 8;

using namespace std;

void solve() {

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

    vector <int> a(n);

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

    auto b = a;

    ll sum1 = 0, sum2 = 0;

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

    for (int i = 0 ; i < r - l + 1; i ++) sum1 += a[i];

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

    for (int i = l - 1 ; i < l - 1 + r - l + 1; i ++) sum2 +=b[i];

    ll ans = min(sum1, sum2);

    cout << ans << endl;

}

int main(void) {
    ios::sync_with_stdio(false);cin.tie(nullptr);
    int t ; cin >> t;
    while (t--)solve();

}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll w1[110000],w2[110000];
void solve(){
    ll n,l,r;
    cin>>n>>l>>r;
    for(int i=0;i<n;i++){
        cin>>w1[i];
        w2[i]=w1[i];
    }
    ll x1=0,x2=0;
    sort(w1,w1+r,greater<ll>());
    sort(w2+l-1,w2+n);
    for(int i=l-1;i<r;i++){
        x1+=w1[i];
        x2+=w2[i];
    }
    cout<<min(x1,x2)<<'\n';
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
    ll t=1;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
};

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll w[110000];
void solve(){
    ll n,l,r;
    cin>>n>>l>>r;
    for(int i=0;i<n;i++)  cin>>w[i];
    sort(w,w+r,greater<ll>());
    sort(w+l-1,w+n);
    ll x=0;
    for(int i=l-1;i<r;i++)  x+=w[i];
    cout<<x<<'\n';
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
    ll t=1;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
};

Answer: 1


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

#define fastio()                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;

#define pb push_back
#define f(i, s, e) for (long long int i = s; i < e; i++)
#define cf(i, s, e) for (long long int i = s; i <= e; i++)
#define rf(i, e, s) for (long long int i = e - 1; i >= s; i--)
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define MOD 1000000007
#define PI 3.1415926535897932384626433832795

#ifndef ONLINE_JUDGE
#define debug(x)                                                               \
  cerr << #x << " ";                                                           \
  _print(x);                                                                   \
  cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) { cerr << t; }
void _print(int t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(lld t) { cerr << t; }
void _print(double t) { cerr << t; }
void _print(ull t) { cerr << t; }

template <class T, class V> void _print(pair<T, V> p);
template <class T> void _print(vector<T> v);
template <class T> void _print(set<T> v);
template <class T, class V> void _print(map<T, V> v);
template <class T> void _print(multiset<T> v);
template <class T, class V> void _print(pair<T, V> p) {
  cerr << "{";
  _print(p.ff);
  cerr << ",";
  _print(p.ss);
  cerr << "}";
}
template <class T> void _print(vector<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T> void _print(set<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T> void _print(multiset<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T, class V> void _print(map<T, V> v) {
  cerr << "[ ";
  for (auto i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}

ll left_recur(vector<vector<char>> &arr, ll i, ll j, ll prevI, ll prevJ) {

  ll left, up, down;
  left = up = down = 0;
  debug(i) debug(j)
      debug(arr[i][j]) if (j + 1 < arr[0].size() and arr[i][j + 1] == 'B') {
    left = 1 + left_recur(arr, i, j + 1, i, j);
  }
  if (i - 1 >= 0 and arr[i - 1][j] == 'B' and i - 1 != prevI) {
    up = 1 + left_recur(arr, i - 1, j, i, j);
  }
  if (i + 1 < 2 and arr[i + 1][j] == 'B' and i + 1 != prevI) {
    down = 1 + left_recur(arr, i + 1, j, i, j);
  }
  ll temp = max(left, max(up, down));
  debug(temp) return max(left, max(up, down));
}
ll brute_recur(vector<vector<char>> &arr, ll i, ll j, ll prevI, ll prevJ) {

  ll left, up, down;
  left = up = down = 0;
  // if()
  debug(i) debug(j) debug(arr[i][j]) if (j - 1 >= 0 and arr[i][j - 1] == 'B') {
    left = 1 + brute_recur(arr, i, j - 1, i, j);
  }
  if (i - 1 >= 0 and arr[i - 1][j] == 'B' and i - 1 != prevI) {
    up = 1 + brute_recur(arr, i - 1, j, i, j);
  }
  if (i + 1 < 2 and arr[i + 1][j] == 'B' and i + 1 != prevI) {
    down = 1 + brute_recur(arr, i + 1, j, i, j);
  }
  ll temp = max(left, max(up, down));
  debug(temp) return temp;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  int t = 1;
  cin >> t;
  while (t--) {
    ll n, l, r;
    cin >> n >> l >> r;
    vector<ll> a(n);
    for (ll i = 0; i < n; i++) {
      cin >> a[i];
    }
    vector<ll> s;
    vector<ll> left;
    vector<ll> right;

    for (ll i = l - 1; i < r; i++)
      s.pb(a[i]);

    for (ll i = 0; i < l - 1; i++)
      left.pb(a[i]);
    for (ll i = r; i < n; i++)
      right.pb(a[i]);

    sort(all(s), greater<>());
    sort(all(left));
    sort(all(right));
    ll i = 0, j = 0, sum = 0;
    sum = accumulate(all(s), (ll)0);

    ll temp = sum;
    while (i < left.size()) {
      if (j < s.size() and left[i] < s[j]) {
        temp -= s[j];
        temp += left[i];
        i++;
        j++;
      } else {
        break;
      }
    }
    i = 0, j = 0;

    ll temp1 = sum;
    while (i < right.size()) {
      if (j < s.size() and right[i] < s[j]) {
        temp1 -= s[j];
        temp1 += right[i];
        i++;
        j++;
      } else {
        break;
      }
    }
    vector<ll> ans = {sum, temp1, temp};

    cout << *min_element(all(ans)) << endl;

    // debug(sum)
  }

  return 0;
}

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

#define fastio()                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;

#define pb push_back
#define f(i, s, e) for (long long int i = s; i < e; i++)
#define cf(i, s, e) for (long long int i = s; i <= e; i++)
#define rf(i, e, s) for (long long int i = e - 1; i >= s; i--)
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define MOD 1000000007
#define PI 3.1415926535897932384626433832795

#ifndef ONLINE_JUDGE
#define debug(x)                                                               \
  cerr << #x << " ";                                                           \
  _print(x);                                                                   \
  cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) { cerr << t; }
void _print(int t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(lld t) { cerr << t; }
void _print(double t) { cerr << t; }
void _print(ull t) { cerr << t; }

template <class T, class V> void _print(pair<T, V> p);
template <class T> void _print(vector<T> v);
template <class T> void _print(set<T> v);
template <class T, class V> void _print(map<T, V> v);
template <class T> void _print(multiset<T> v);
template <class T, class V> void _print(pair<T, V> p) {
  cerr << "{";
  _print(p.ff);
  cerr << ",";
  _print(p.ss);
  cerr << "}";
}
template <class T> void _print(vector<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T> void _print(set<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T> void _print(multiset<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T, class V> void _print(map<T, V> v) {
  cerr << "[ ";
  for (auto i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}

ll left_recur(vector<vector<char>> &arr, ll i, ll j, ll prevI, ll prevJ) {

  ll left, up, down;
  left = up = down = 0;
  debug(i) debug(j)
      debug(arr[i][j]) if (j + 1 < arr[0].size() and arr[i][j + 1] == 'B') {
    left = 1 + left_recur(arr, i, j + 1, i, j);
  }
  if (i - 1 >= 0 and arr[i - 1][j] == 'B' and i - 1 != prevI) {
    up = 1 + left_recur(arr, i - 1, j, i, j);
  }
  if (i + 1 < 2 and arr[i + 1][j] == 'B' and i + 1 != prevI) {
    down = 1 + left_recur(arr, i + 1, j, i, j);
  }
  ll temp = max(left, max(up, down));
  debug(temp) return max(left, max(up, down));
}
ll brute_recur(vector<vector<char>> &arr, ll i, ll j, ll prevI, ll prevJ) {

  ll left, up, down;
  left = up = down = 0;
  // if()
  debug(i) debug(j) debug(arr[i][j]) if (j - 1 >= 0 and arr[i][j - 1] == 'B') {
    left = 1 + brute_recur(arr, i, j - 1, i, j);
  }
  if (i - 1 >= 0 and arr[i - 1][j] == 'B' and i - 1 != prevI) {
    up = 1 + brute_recur(arr, i - 1, j, i, j);
  }
  if (i + 1 < 2 and arr[i + 1][j] == 'B' and i + 1 != prevI) {
    down = 1 + brute_recur(arr, i + 1, j, i, j);
  }
  ll temp = max(left, max(up, down));
  debug(temp) return temp;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  int t = 1;
  cin >> t;
  while (t--) {
    ll n, l, r;
    cin >> n >> l >> r;
    vector<ll> a(n);
    for (ll i = 0; i < n; i++) {
      cin >> a[i];
    }
    vector<ll> s;
    vector<ll> left;
    vector<ll> right;

    for (ll i = l - 1; i < r; i++)
      s.pb(a[i]);

    for (ll i = 0; i < l - 1; i++)
      left.pb(a[i]);
    for (ll i = r; i < n; i++)
      right.pb(a[i]);

    sort(all(s), greater<>());
    sort(all(left));
    sort(all(right));
    ll i = 0, j = 0, sum = 0;
    sum = accumulate(all(s), 0);

    ll temp = sum;
    while (i < left.size()) {
      if (j < s.size() and left[i] < s[j]) {
        temp -= s[j];
        temp += left[i];
        i++;
        j++;
      } else {
        break;
      }
    }
    i = 0, j = 0;

    ll temp1 = sum;
    while (i < right.size()) {
      if (j < s.size() and right[i] < s[j]) {
        temp1 -= s[j];
        temp1 += right[i];
        i++;
        j++;
      } else {
        break;
      }
    }
    vector<ll> ans = {sum, temp1, temp};

    cout << *min_element(all(ans)) << endl;

    // debug(sum)
  }

  return 0;
}

Answer: 1


#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42;
#define endl '\n'
#endif

#define int long long

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

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

    vector<int>b(a.begin(), a.begin() + r);
    vector<int>c(a.begin() + l - 1, a.end());
    sort(b.begin(), b.end());
    sort(c.begin(), c.end());

    // debug(l, r, a, b, c)
    return min(accumulate(b.begin(), b.begin() + (r - l + 1), 0), accumulate(c.begin(), c.begin() + (r - l + 1), 0));
}
signed main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    int _ = 1;
    cin >> _;
    while (_--) {
        cout << solve(_) << endl;
    }
}

#include<bits/stdc++.h>

using namespace std;

#ifdef LOCAL

#include "debug.h"

#else

#define debug(...) 42;

#define endl '\n'

#endif

#define int long long

int solve(int _) {

    int l, r, n;

    cin >> n >> l >> r;

    vector<int>a(n);

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

        cin >> a[i];

    }

    vector<int>b(a.begin(), a.begin() + r);

    vector<int>c(a.begin() + l - 1, a.end());

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

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

    // debug(l, r, a, b, c)

    return min(accumulate(b.begin(), b.begin() + (r - l + 1), 0ll), accumulate(c.begin(), c.begin() + (r - l + 1), 0ll));

}

signed main()

{

    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

    int _ = 1;

    cin >> _;

    while (_--) {

        cout << solve(_) << endl;

    }

}

Answer: 2


t=int(input())

for case in range(t):

    s=0

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

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

    a=seq[:l-1]+sorted(seq[l-1:])

    b=sorted(seq[:r])[::-1]+seq[r:]

    print(min(sum(a[l-1:r]),sum(b[l-1:r])))

t=int(input())

for case in range(t):

    s=0

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

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

    seg=list(seq[l-1:r])

    exseg=list(seq[:l-1])

    exseg.extend(list(seq[r:]))

    seg.append(0)

    s=sum(seg)

    if len(exseg)!=0:

        while min(exseg)<max(seg):

            a=min(exseg)

            b=max(seg)

            s=s-b+a

            seg.remove(b)

            exseg.remove(a)

            if len(exseg)==0:

                break

    print(s)

Answer: 1


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using pii = pair<ll, ll>;
using pll = pair<ll, ll>;
#define all(v) (v).begin(), (v).end()
#define sz(v) (ll)(v).size()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define f(i, a, b) for (ll i = (a); i < (b); i++)
#define fr(i, a, b) for (ll i = (a); i >= (b); i--)
void solve() {
    ll n,l,r;
    cin>>n>>l>>r;
    vll a(n);
    f(i,0,n){cin>>a[i];}
    vll fr;vll b;
    f(i,0,l-1)
    {
      fr.pb(a[i]);
    }
    vll crr;
    f(i,l-1,r)
    {
      crr.pb(a[i]);
    }
    f(i,r,n)
    {
      b.pb(a[i]);
    }
    sort(all(crr));
    sort(all(fr));sort(all(b));
    ll x1=0;
    if(fr.size()>0)
    {
      for(int i=crr.size()-1;i>=0;i--)
      {
        if(x1==fr.size()){break;}
        if(fr[x1]<crr[i])
        {
          crr[i]=fr[x1];x1++;continue;
        }
        if(fr[x1]>crr[i]){break;}
      }
    }
    sort(all(crr));x1=0;
    if(b.size()>0)
    {
      for(int i=crr.size()-1;i>=0;i--)
      {
        if(x1==b.size()){break;}
        if(b[x1]<crr[i])
        {
          crr[i]=b[x1];x1++;continue;
        }
        if(b[x1]>crr[i]){break;}
      }
    }
    ll ans=0;
    f(i,0,crr.size())
    {
      ans+=crr[i];
    }
    cout<<ans<<endl;

}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll t; cin >> t;
    while (t--) { solve(); }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using pii = pair<ll, ll>;
using pll = pair<ll, ll>;
#define all(v) (v).begin(), (v).end()
#define sz(v) (ll)(v).size()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define f(i, a, b) for (ll i = (a); i < (b); i++)
#define fr(i, a, b) for (ll i = (a); i >= (b); i--)
void solve() {
    ll n,l,r;
    cin>>n>>l>>r;
    vll a(n);
    f(i,0,n){cin>>a[i];}
    vll fr;vll b;
    f(i,0,l-1)
    {
      fr.pb(a[i]);
    }
    vll crr;
    f(i,l-1,r)
    {
      crr.pb(a[i]);
    }
    f(i,r,n)
    {
      b.pb(a[i]);
    }
    vll tou=crr;ll a1=0;ll a2=a1;
    sort(all(crr));
    sort(all(fr));sort(all(b));
    ll x1=0;
    if(fr.size()>0)
    {
      for(int i=crr.size()-1;i>=0;i--)
      {
        if(x1==fr.size()){break;}
        if(fr[x1]<crr[i])
        {
          crr[i]=fr[x1];x1++;continue;
        }
        if(fr[x1]>crr[i]){break;}
      }
    }
    f(i,0,crr.size())
    {
      a1+=crr[i];
    }
    crr=tou;
    sort(all(crr));x1=0;
    if(b.size()>0)
    {
      for(int i=crr.size()-1;i>=0;i--)
      {
        if(x1==b.size()){break;}
        if(b[x1]<crr[i])
        {
          crr[i]=b[x1];x1++;continue;
        }
        if(b[x1]>crr[i]){break;}
      }
    }

    f(i,0,crr.size())
    {
      a2+=crr[i];
    }
    cout<<min(a1,a2)<<endl;

}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll t; cin >> t;
    while (t--) { solve(); }
    return 0;
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t-->0){
        long long int n,l,r;
        cin>>n>>l>>r;
        vector<long long int>v(n);
        vector<long long int>a,b;
        long long int g=(r-l)+1;
        for(long long int i=0;i<n;i++){
            cin>>v[i];
        }
        for(long long int i=l-1;i<n;i++){
            a.push_back(v[i]);
        }
        for(long long int i=0;i<r;i++){
            b.push_back(v[i]);
        }
        sort(a.begin(),a.end());
        sort(b.begin(),b.end());
        long long int ans1=0;
        long long int ans2=0;
        long long int ans=0;
        for(int i=0;i<g;i++){
            ans1+=(a[i]);
        }
        for(int i=0;i<g;i++){
            ans2+=(b[i]);
        }
        cout<<min(ans1,ans2)<<endl;
    }
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t-->0){
        int n,l,r;
        cin>>n>>l>>r;
        vector<int>v(n);
        vector<int>a,b;
        int g=(r-l)+1;
        for(int i=0;i<n;i++){
            cin>>v[i];
        }
        for(int i=l-1;i<n;i++){
            a.push_back(v[i]);
        }
        for(int i=0;i<r;i++){
            b.push_back(v[i]);
        }
        sort(a.begin(),a.end());
        sort(b.begin(),b.end());
        int ans1=0;
        int ans2=0;
        int ans=0;
        for(int i=0;i<g;i++){
            ans1+=(a[i]);
        }
        for(int i=0;i<g;i++){
            ans2+=(b[i]);
        }
        cout<<min(ans1,ans2)<<endl;
    }
}

Answer: 1