Problem: 2063B - Subsequence Update
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>



using namespace std;



int main()

{

    int t;

    cin>>t;

    while(t--)

    {

        int n,l,r;

        cin>>n>>l>>r;

        vector<long long>a(n);

        for(auto &x:a)

            cin>>x;

        long long sum=0;

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

        {

            sum+=a[i];

        }

        if(r<n && l>1)

        {

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

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

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

            long long lsum=sum,rsum=sum;

            int xl=r,yl=r-1;

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

            {

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

                xl++;

                yl--;

            }

            int xr=0,yr=r-1;

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

            {

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

                xr++;

                yr--;

            }

            sum=min(lsum,rsum);

            cout<<sum;

        }

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

        {

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

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

            int x=r,y=r-1;

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

            {

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

                x++;

                y--;

            }

            cout<<sum;

        }

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

        {

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

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

            int x=0,y=r-1;

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

            {

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

                x++;

                y--;

            }

            cout<<sum;

        }

        else

        {

            cout<<sum;

        }

        cout<<"\n";

    }

}

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



using namespace std;



int main()

{

    int t;

    cin>>t;

    while(t--)

    {

        int n,l,r;

        cin>>n>>l>>r;

        vector<long long>a(n);

        for(auto &x:a)

            cin>>x;

        long long sum=0;

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

        {

            sum+=a[i];

        }

        if(r<n && l>1)

        {

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

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

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

            long long lsum=sum,rsum=sum;

            int xl=r,yl=r-1;

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

            {

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

                xl++;

                yl--;

            }

            int xr=0,yr=r-1;

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

            {

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

                xr++;

                yr--;

            }

            sum=min(lsum,rsum);

            cout<<sum;

        }

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

        {

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

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

            int x=r,y=r-1;

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

            {

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

                x++;

                y--;

            }

            cout<<sum;

        }

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

        {

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

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

            int x=0,y=r-1;

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

            {

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

                x++;

                y--;

            }

            cout<<sum;

        }

        else

        {

            cout<<sum;

        }

        cout<<"\n";

    }

}