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

Previous submission (verdict: Wrong answer on test 8):
#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;

}



Submission (verdict: Wrong answer on test 8):
#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--)
    {
        ll n, l, r;
        cin >> n >> l >> r;
        vector<ll> a(n);
        for (ll i = 0; i < n; i++)
        {
            cin >> a[i];
        }

        vector<ll> before, after, btw;
        for (ll 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));

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

        ll 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;
}