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

Previous submission (verdict: Wrong answer on test 8):
#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

    //1-partition array into 3 arrays from ranges : a: <l ,b: l<= <=r ,c: >r

    //3-sort the 3 subarrays

    //2- take arrays a and b and use two pointers to minimze elements picked

    //3- repeat with the b and c and answer is the minimum of steps 2 and 3

    

int findMinSum(vector<int>& a,vector<int>& b){

    int ptrA = 0,ptrB=b.size()-1;

    long long sum =0;

    while (ptrA<a.size() && ptrB >= 0 && a[ptrA]< b[ptrB]){

        sum+=a[ptrA];

        ptrA++;

        ptrB--;

    }

    while (ptrB>=0){

        sum+=b[ptrB--];

    }

    return sum;

}



int main()

{

    int tc;

    cin >> tc;

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

        int n,l,r,element;

        cin >> n;

        cin >> l;

        cin >> r;

        vector<int> a,b,c;

        a.reserve(l);

        b.reserve(r-l+1);

        c.reserve(n-r);

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

            cin >> element;

            if (i<l){

                a.push_back(element);

            }else if (i>=l && i<=r){

                b.push_back(element);

            }else{

                c.push_back(element);

            }

        }

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

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

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

        

        cout << min(findMinSum(a,b),findMinSum(c,b)) <<endl;

    }

    return 0;

}

Submission (verdict: Wrong answer on test 2):
#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

    //1-partition array into 3 arrays from ranges : a: <l ,b: l<= <=r ,c: >r

    //3-sort the 3 subarrays

    //2- take arrays a and b and use two pointers to minimze elements picked

    //3- repeat with the b and c and answer is the minimum of steps 2 and 3

    

int findMinSum(vector<int>& a,vector<int>& b,bool mustSwitch){

    int ptrA = 0,ptrB=b.size()-1;

    long long sum =0;

    while (ptrA<a.size() && ptrB >= 0 && (a[ptrA]< b[ptrB] || mustSwitch)){

        sum+=a[ptrA];

        ptrA++;

        ptrB--;

        mustSwitch = false;

    }

    while (ptrB>=0){

        sum+=b[ptrB--];

    }

    return sum;

}



int main()

{

    int tc;

    cin >> tc;

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

        int n,l,r,element;

        cin >> n;

        cin >> l;

        cin >> r;

        vector<int> a,b,c;

        a.reserve(l);

        b.reserve(r-l+1);

        c.reserve(n-r);

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

            cin >> element;

            if (i<l){

                a.push_back(element);

            }else if (i>=l && i<=r){

                b.push_back(element);

            }else{

                c.push_back(element);

            }

        }

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

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

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

        bool mustSwitch = (a.size()==0 && c.size()!=0 ||c.size()==0 && a.size()!=0);

        if (!a.empty() && !c.empty()){

            cout << min(findMinSum(a,b,mustSwitch),findMinSum(c,b,mustSwitch)) <<endl;

        }else if (!a.empty()){

            cout<< findMinSum(a,b,mustSwitch) << endl;

        }else{

            cout <<findMinSum(c,b,mustSwitch) <<endl;

        }

    }

    return 0;

}