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

Previous submission (verdict: Wrong answer on test 2):
#pragma GCC optimize("O3,unroll-loops")



#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>



using namespace std;

using namespace chrono;

using namespace __gnu_pbds;



#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

#define MOD 1000000007

#define MOD1 998244353

#define INF 1e18

#define nline "\n"

#define pb push_back

#define ppb pop_back

#define mp make_pair

#define ff first

#define ss second

#define PI 3.141592653589793238462

#define set_bits __builtin_popcountll

#define sz(x) ((int)(x).size())

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



#ifdef RushabhMehta2005

    #define debug(x) cerr << #x << " "; _print(x); cerr << endl;

#else

    #define debug(x);

#endif



typedef long long ll;

typedef unsigned long long ull;

typedef long double lld;

typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update> pbds;



void _print(ll t) { cerr << t; }

void _print(int t) { cerr << t; }

void _print(string t) { cerr << t; }

void _print(char t) { cerr << t; }

void _print(lld t) { cerr << t; }

void _print(double t) { cerr << t; }

void _print(ull t) { cerr << t; }



template <class T, class V> void _print(pair<T, V> p);

template <class T> void _print(vector<T> v);

template <class T> void _print(set<T> v);

template <class T, class V> void _print(map<T, V> v);

template <class T> void _print(multiset<T> v);

void _print(pbds v);



template <class T, class V> void _print(pair<T, V> p) {

    cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";

}



template <class T> void _print(vector<T> v) {

    cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]";

}



template <class T> void _print(set<T> v) {

    cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]";

}



template <class T> void _print(multiset<T> v) {

    cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]";

}



template <class T, class V> void _print(map<T, V> v) {

    cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]";

}



void _print(pbds v) {

    cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]";

}



mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());



/* ---------------- Number Theory Functions ---------------- */

ll gcd(ll a, ll b) {

    if (b > a) return gcd(b, a);

    if (b == 0) return a;

    return gcd(b, a % b);

}



ll lcm(ll a, ll b) {

    return a / gcd(a, b) * b;

}



ll expo(ll a, ll b, ll mod) {

    ll res = 1;

    while (b > 0) {

        if (b & 1) res = (res * a) % mod;

        a = (a * a) % mod;

        b >>= 1;

    }

    return res;

}



void extendgcd(ll a, ll b, ll *v) {

    if (b == 0) {

        v[0] = 1; v[1] = 0; v[2] = a;

        return;

    }

    extendgcd(b, a % b, v);

    ll x = v[1];

    v[1] = v[0] - v[1] * (a / b);

    v[0] = x;

}



ll mminv(ll a, ll b) {

    ll arr[3];

    extendgcd(a, b, arr);

    return arr[0];

}



ll mminvprime(ll a, ll b) {

    return expo(a, b - 2, b);

}



vector<ll> sieve(int n) {

    vector<ll> primes;

    vector<int> is_prime(n + 1, 1);

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

        if (is_prime[i]) {

            primes.pb(i);

            for (int j = 2 * i; j <= n; j += i) {

                is_prime[j] = 0;

            }

        }

    }

    return primes;

}



ll mod_add(ll a, ll b, ll m) { return ((a % m + b % m) % m + m) % m; }

ll mod_mul(ll a, ll b, ll m) { return ((a % m * b % m) % m + m) % m; }

ll mod_sub(ll a, ll b, ll m) { return ((a % m - b % m) % m + m) % m; }

ll mod_div(ll a, ll b, ll m) { return mod_mul(a, mminvprime(b, m), m); }



ll phin(ll n) { // euler's totient function

    ll result = n;

    for (ll i = 2; i * i <= n; i++) {

        if (n % i == 0) {

            while (n % i == 0) n /= i;

            result -= result / i;

        }

    }

    if (n > 1) result -= result / n;

    return result;

}



ll getRandomNumber(ll l, ll r) {

    return uniform_int_distribution<ll>(l, r)(rng);

}



/* -------------------------------------------------------------- */

void solve() {

    int n, l, r;

    cin >> n >> l >> r;

    vector<ll> a(n);

    vector<ll> b(n);

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

        cin >> a[i];

        b[i] = a[i];

    }

    l--; r--;

    ll k = r - l + 1;

    ll ans1 = 0, ans2 = 0;

    if (l == 0 || r == n - 1) {

        sort(all(a));

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

            ans1 += a[i];

        }

        cout << ans1 << nline;

        return;

    }

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

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

    

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

        ans1 += a[i];

    }

    for (int i = n - 1; i >= n - k; i--) {

        ans2 += b[i];

    }

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

}



int main() {

    #ifdef RushabhMehta2005

        freopen("Error.txt", "w", stderr);

    #endif

    

    fastio();

    

    bool testcases = true; // change this if no TCs

    int t = 1;

    if (testcases) {

        cin >> t;

    }



    auto start = high_resolution_clock::now();

    

    while (t--) {

        solve();

    }



    auto stop = high_resolution_clock::now();

    auto duration = duration_cast<microseconds>(stop - start);



    #ifdef RushabhMehta2005

        cerr << fixed << setprecision(6);

        cerr << "Total Time: " << duration.count() / 1000.0 << " ms" << endl;

    #endif

    

    return 0;

}





Submission (verdict: Accepted):
#pragma GCC optimize("O3,unroll-loops")



#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>



using namespace std;

using namespace chrono;

using namespace __gnu_pbds;



#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

#define MOD 1000000007

#define MOD1 998244353

#define INF 1e18

#define nline "\n"

#define pb push_back

#define ppb pop_back

#define mp make_pair

#define ff first

#define ss second

#define PI 3.141592653589793238462

#define set_bits __builtin_popcountll

#define sz(x) ((int)(x).size())

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



#ifdef RushabhMehta2005

    #define debug(x) cerr << #x << " "; _print(x); cerr << endl;

#else

    #define debug(x);

#endif



typedef long long ll;

typedef unsigned long long ull;

typedef long double lld;

typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update> pbds;



void _print(ll t) { cerr << t; }

void _print(int t) { cerr << t; }

void _print(string t) { cerr << t; }

void _print(char t) { cerr << t; }

void _print(lld t) { cerr << t; }

void _print(double t) { cerr << t; }

void _print(ull t) { cerr << t; }



template <class T, class V> void _print(pair<T, V> p);

template <class T> void _print(vector<T> v);

template <class T> void _print(set<T> v);

template <class T, class V> void _print(map<T, V> v);

template <class T> void _print(multiset<T> v);

void _print(pbds v);



template <class T, class V> void _print(pair<T, V> p) {

    cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";

}



template <class T> void _print(vector<T> v) {

    cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]";

}



template <class T> void _print(set<T> v) {

    cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]";

}



template <class T> void _print(multiset<T> v) {

    cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]";

}



template <class T, class V> void _print(map<T, V> v) {

    cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]";

}



void _print(pbds v) {

    cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]";

}



mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());



/* ---------------- Number Theory Functions ---------------- */

ll gcd(ll a, ll b) {

    if (b > a) return gcd(b, a);

    if (b == 0) return a;

    return gcd(b, a % b);

}



ll lcm(ll a, ll b) {

    return a / gcd(a, b) * b;

}



ll expo(ll a, ll b, ll mod) {

    ll res = 1;

    while (b > 0) {

        if (b & 1) res = (res * a) % mod;

        a = (a * a) % mod;

        b >>= 1;

    }

    return res;

}



void extendgcd(ll a, ll b, ll *v) {

    if (b == 0) {

        v[0] = 1; v[1] = 0; v[2] = a;

        return;

    }

    extendgcd(b, a % b, v);

    ll x = v[1];

    v[1] = v[0] - v[1] * (a / b);

    v[0] = x;

}



ll mminv(ll a, ll b) {

    ll arr[3];

    extendgcd(a, b, arr);

    return arr[0];

}



ll mminvprime(ll a, ll b) {

    return expo(a, b - 2, b);

}



vector<ll> sieve(int n) {

    vector<ll> primes;

    vector<int> is_prime(n + 1, 1);

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

        if (is_prime[i]) {

            primes.pb(i);

            for (int j = 2 * i; j <= n; j += i) {

                is_prime[j] = 0;

            }

        }

    }

    return primes;

}



ll mod_add(ll a, ll b, ll m) { return ((a % m + b % m) % m + m) % m; }

ll mod_mul(ll a, ll b, ll m) { return ((a % m * b % m) % m + m) % m; }

ll mod_sub(ll a, ll b, ll m) { return ((a % m - b % m) % m + m) % m; }

ll mod_div(ll a, ll b, ll m) { return mod_mul(a, mminvprime(b, m), m); }



ll phin(ll n) { // euler's totient function

    ll result = n;

    for (ll i = 2; i * i <= n; i++) {

        if (n % i == 0) {

            while (n % i == 0) n /= i;

            result -= result / i;

        }

    }

    if (n > 1) result -= result / n;

    return result;

}



ll getRandomNumber(ll l, ll r) {

    return uniform_int_distribution<ll>(l, r)(rng);

}



/* -------------------------------------------------------------- */

void solve() {

    int n, l, r;

    cin >> n >> l >> r;

    vector<ll> a(n);

    vector<ll> b(n);

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

        cin >> a[i];

        b[i] = a[i];

    }

    l--; r--;

    ll k = r - l + 1;

    ll ans1 = 0, ans2 = 0;

    if (l == 0 || r == n - 1) {

        sort(all(a));

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

            ans1 += a[i];

        }

        cout << ans1 << nline;

        return;

    }

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

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

    

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

        ans1 += a[i];

    }

    for (int i = l; i < k + l; i++) {

        ans2 += b[i];

    }

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

}



int main() {

    #ifdef RushabhMehta2005

        freopen("Error.txt", "w", stderr);

    #endif

    

    fastio();

    

    bool testcases = true; // change this if no TCs

    int t = 1;

    if (testcases) {

        cin >> t;

    }



    auto start = high_resolution_clock::now();

    

    while (t--) {

        solve();

    }



    auto stop = high_resolution_clock::now();

    auto duration = duration_cast<microseconds>(stop - start);



    #ifdef RushabhMehta2005

        cerr << fixed << setprecision(6);

        cerr << "Total Time: " << duration.count() / 1000.0 << " ms" << endl;

    #endif

    

    return 0;

}