Problem: 2063B - Subsequence Update
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 7):
#include <bits/stdc++.h>

using namespace std; 

 

//KEVAL MORI//

 

#define Jay ios_base::sync_with_stdio(false);

#define Shree cin.tie(NULL);

#define Ram cout.tie(NULL);

#define read(n) long long int n;cin>>n;

#define tread(a,b) long long int a,b;cin>>a>>b;

#define thread(a,b,c) long long int a,b,c;cin>>a>>b>>c;

#define fread(a,b,c,d) long long int a,b,c,d; cin>>a>>b>>c>>d;

#define vread(v) for(int i=0;i<v.size();i++) cin>>v[i];

#define vvread(v) for(int i=0;i<v.size();i++) {for(int j=0;j<v[i].size();j++) cin>>v[i][j];}

#define lli long long int

#define ll long long

#define MOD 1000000007

#define ld long double

#define f(i,x,n) for(ll i = x; i < n; i++)

#define rf(i,x,n) for(ll i = x; i >= n; i--)

#define fr(i,m) for(auto &i:m)

#define vl vector<long long int>

#define vvl vector<vector<long long int>>

#define vs vector<string>

#define vpl vector<pair<long long int,long long int>>

#define pb push_back

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

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

#define rll(v) (v).begin(), (v).end(),greater<ll>()

#define sl set<long long int>

#define ss set<string>

#define mll map<long long int,long long int>

#define mss map<string,string>

#define msl map<string,long long int>

#define mls map<long long int,string>

#define pl pair<long long int,long long int>

#define fi first

#define se second

#define PI (3.141592653589)

#define maxar *max_element

#define minar *min_element

#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))

#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))

#define UNIQUE(x) (all(x)), x.erase(unique(all(x)), x.end())

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

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

#define nope cout<<-1<<endl

#define pv(v) for (int i = 0; i < v.size(); i++) cout<<v[i]<<" "; 

#define pvv(v) for(int i=0;i<v.size();i++){ for(int j=0;j<v[i].size();j++) {cout<<v[i][j]<<" ";}cout<<endl;}

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

bool isPerfectSquare(ll x){if(x>=0){ll sr=sqrt(x);return (sr*sr==x);}}

bool isInteger(double n){return floor(n)==n;}



void solve()

{

    thread(n,l,r);

    vl a(n),m,s(r-l+1),q;

    

    vread(a);



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

        s[i]=a[r-i-1];

    }

    sort(rall(s));

    q=s;

    lli ans1=0,ans2=0;

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

    if(l==1) break;

        if(*min_element(a.begin(),a.begin()+l-1)<s[i])

        {

           swap(*min_element(a.begin(),a.begin()+l-1),s[i]);

        }

        else break;

    }

    ans1=accumulate(all(s),0LL);

    s=q;



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

        if(a.begin()+r>=a.end()) break;





        if(*min_element(a.begin()+r,a.end())<s[i])

        {

           swap(*min_element(a.begin()+r,a.end()),s[i]);

        }

        else break;

    }

    ans2=accumulate(all(s),0LL);

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



    



    // fr(i,sequence) cout<<i<<" ";



    



   

}

int main()

{

    Jay Shree Ram



    int t;

    cin >> t;



    while (t--)

    {

        solve();

    }



    return 0;

}

Submission (verdict: Accepted):
#include <bits/stdc++.h>

using namespace std; 

 

//KEVAL MORI//

 

#define Jay ios_base::sync_with_stdio(false);

#define Shree cin.tie(NULL);

#define Ram cout.tie(NULL);

#define read(n) long long int n;cin>>n;

#define tread(a,b) long long int a,b;cin>>a>>b;

#define thread(a,b,c) long long int a,b,c;cin>>a>>b>>c;

#define fread(a,b,c,d) long long int a,b,c,d; cin>>a>>b>>c>>d;

#define vread(v) for(int i=0;i<v.size();i++) cin>>v[i];

#define vvread(v) for(int i=0;i<v.size();i++) {for(int j=0;j<v[i].size();j++) cin>>v[i][j];}

#define lli long long int

#define ll long long

#define MOD 1000000007

#define ld long double

#define f(i,x,n) for(ll i = x; i < n; i++)

#define rf(i,x,n) for(ll i = x; i >= n; i--)

#define fr(i,m) for(auto &i:m)

#define vl vector<long long int>

#define vvl vector<vector<long long int>>

#define vs vector<string>

#define vpl vector<pair<long long int,long long int>>

#define pb push_back

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

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

#define rll(v) (v).begin(), (v).end(),greater<ll>()

#define sl set<long long int>

#define ss set<string>

#define mll map<long long int,long long int>

#define mss map<string,string>

#define msl map<string,long long int>

#define mls map<long long int,string>

#define pl pair<long long int,long long int>

#define fi first

#define se second

#define PI (3.141592653589)

#define maxar *max_element

#define minar *min_element

#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))

#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))

#define UNIQUE(x) (all(x)), x.erase(unique(all(x)), x.end())

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

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

#define nope cout<<-1<<endl

#define pv(v) for (int i = 0; i < v.size(); i++) cout<<v[i]<<" "; 

#define pvv(v) for(int i=0;i<v.size();i++){ for(int j=0;j<v[i].size();j++) {cout<<v[i][j]<<" ";}cout<<endl;}

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

bool isPerfectSquare(ll x){if(x>=0){ll sr=sqrt(x);return (sr*sr==x);}}

bool isInteger(double n){return floor(n)==n;}



void solve()

{

    thread(n,l,r);

    vl a(n),m,s(r-l+1),q;

    

    vread(a);



    lli ans1=0,ans2=0;



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

        s[i]=a[r-i-1];

    }

    sort(rall(s));

    q=s;



    if(l!=1) {

        vl t1(a.begin(), a.begin()+l-1);

        sort(all(t1));

    for (int i = 0; i <s.size()&&i<t1.size(); i++) {



        if(t1[i]<s[i])

        {

           swap(t1[i],s[i]);

        }

        else break;

    }

}



    ans1=accumulate(all(s),0LL);

    s=q;

    if(a.begin()+r<a.end()) {

    vl t2(a.begin()+r, a.end());

    sort(all(t2));



    for (int i = 0; i <s.size()&&i<t2.size(); i++) {

        if(t2[i]<s[i])

        {

           swap(t2[i],s[i]);

        }

        else break;

    }

}

    ans2=accumulate(all(s),0LL);

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



}

int main()

{

    Jay Shree Ram



    int t;

    cin >> t;



    while (t--)

    {

        solve();

    }



    return 0;

}