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

Previous submission (verdict: Compilation error):
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>

using namespace std;
#define 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> first(a.begin(), a.begin() + l - 1);
    // cout << "first size: " << first.size() << endl;
    vector<ll> second(a.begin() + l - 1, a.begin() + r);
    // cout << "second size: " << second.size() << endl;
    vector<ll> last(a.begin() + r, a.end());
    // cout << "last size: " << last.size() << endl;

    sort(first.begin(), first.end());
    sort(second.begin(), second.end(), greater<ll>());
    sort(last.begin(), last.end());

    // cout << "first: ";
    // for (int x : first) cout << x << " ";
    // cout << endl;

    // cout << "second: ";
    // for (int x : second) cout << x << " ";
    // cout << endl;

    // cout << "last: ";
    // for (int x : last) cout << x << " ";
    // cout << endl;

    ll initial_sum = 0;
    for (ll x: second) initial_sum += x;


    ll left_delta = 0;
    for (ll i = 0; i < min(first.size(), second.size()); i++){
        if (first[i] > second[i]){
            break;
        }
        else{
            left_delta +=  first[i] - second[i] ;
        }
    }

    ll right_delta = 0;
    for (ll i = 0; i < min(last.size(), second.size()); i++){
        if (last[i] > second[i]){
            break;
        }
        else{
            right_delta -= second[i] - last[i];
        }
    }

    ll res = initial_sum + min(left_delta, right_delta, 0);
    // cout << "left delta: " << left_delta <<  " right delta: " << right_delta << endl;

    cout << res << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

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

Submission (verdict: Accepted):
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>

using namespace std;
#define 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> first(a.begin(), a.begin() + l - 1);
    // cout << "first size: " << first.size() << endl;
    vector<ll> second(a.begin() + l - 1, a.begin() + r);
    // cout << "second size: " << second.size() << endl;
    vector<ll> last(a.begin() + r, a.end());
    // cout << "last size: " << last.size() << endl;

    sort(first.begin(), first.end());
    sort(second.begin(), second.end(), greater<ll>());
    sort(last.begin(), last.end());

    // cout << "first: ";
    // for (int x : first) cout << x << " ";
    // cout << endl;

    // cout << "second: ";
    // for (int x : second) cout << x << " ";
    // cout << endl;

    // cout << "last: ";
    // for (int x : last) cout << x << " ";
    // cout << endl;

    ll initial_sum = 0;
    for (ll x: second) initial_sum += x;


    ll left_delta = 0;
    for (ll i = 0; i < min(first.size(), second.size()); i++){
        if (first[i] > second[i]){
            break;
        }
        else{
            left_delta +=  first[i] - second[i] ;
        }
    }

    ll right_delta = 0;
    for (ll i = 0; i < min(last.size(), second.size()); i++){
        if (last[i] > second[i]){
            break;
        }
        else{
            right_delta -= second[i] - last[i];
        }
    }

    ll delta = min(left_delta, right_delta);
    delta = min(delta, 0ll);
    ll res = initial_sum + delta;
    // cout << "left delta: " << left_delta <<  " right delta: " << right_delta << endl;

    cout << res << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

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