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

Previous submission (verdict: Wrong answer on test 8):
#include <bits/stdc++.h>
#include <numeric>
using namespace std;

#define fastio()                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;

#define pb push_back
#define f(i, s, e) for (long long int i = s; i < e; i++)
#define cf(i, s, e) for (long long int i = s; i <= e; i++)
#define rf(i, e, s) for (long long int i = e - 1; i >= s; i--)
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define MOD 1000000007
#define PI 3.1415926535897932384626433832795

#ifndef ONLINE_JUDGE
#define debug(x)                                                               \
  cerr << #x << " ";                                                           \
  _print(x);                                                                   \
  cerr << endl;
#else
#define debug(x)
#endif

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);
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 << "]";
}

ll left_recur(vector<vector<char>> &arr, ll i, ll j, ll prevI, ll prevJ) {

  ll left, up, down;
  left = up = down = 0;
  debug(i) debug(j)
      debug(arr[i][j]) if (j + 1 < arr[0].size() and arr[i][j + 1] == 'B') {
    left = 1 + left_recur(arr, i, j + 1, i, j);
  }
  if (i - 1 >= 0 and arr[i - 1][j] == 'B' and i - 1 != prevI) {
    up = 1 + left_recur(arr, i - 1, j, i, j);
  }
  if (i + 1 < 2 and arr[i + 1][j] == 'B' and i + 1 != prevI) {
    down = 1 + left_recur(arr, i + 1, j, i, j);
  }
  ll temp = max(left, max(up, down));
  debug(temp) return max(left, max(up, down));
}
ll brute_recur(vector<vector<char>> &arr, ll i, ll j, ll prevI, ll prevJ) {

  ll left, up, down;
  left = up = down = 0;
  // if()
  debug(i) debug(j) debug(arr[i][j]) if (j - 1 >= 0 and arr[i][j - 1] == 'B') {
    left = 1 + brute_recur(arr, i, j - 1, i, j);
  }
  if (i - 1 >= 0 and arr[i - 1][j] == 'B' and i - 1 != prevI) {
    up = 1 + brute_recur(arr, i - 1, j, i, j);
  }
  if (i + 1 < 2 and arr[i + 1][j] == 'B' and i + 1 != prevI) {
    down = 1 + brute_recur(arr, i + 1, j, i, j);
  }
  ll temp = max(left, max(up, down));
  debug(temp) return temp;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  int t = 1;
  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> s;
    vector<ll> left;
    vector<ll> right;

    for (ll i = l - 1; i < r; i++)
      s.pb(a[i]);

    for (ll i = 0; i < l - 1; i++)
      left.pb(a[i]);
    for (ll i = r; i < n; i++)
      right.pb(a[i]);

    sort(all(s), greater<>());
    sort(all(left));
    sort(all(right));
    ll i = 0, j = 0, sum = 0;
    sum = accumulate(all(s), 0);

    ll temp = sum;
    while (i < left.size()) {
      if (j < s.size() and left[i] < s[j]) {
        temp -= s[j];
        temp += left[i];
        i++;
        j++;
      } else {
        break;
      }
    }
    i = 0, j = 0;

    ll temp1 = sum;
    while (i < right.size()) {
      if (j < s.size() and right[i] < s[j]) {
        temp1 -= s[j];
        temp1 += right[i];
        i++;
        j++;
      } else {
        break;
      }
    }
    vector<ll> ans = {sum, temp1, temp};

    cout << *min_element(all(ans)) << endl;

    // debug(sum)
  }

  return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
#include <numeric>
using namespace std;

#define fastio()                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;

#define pb push_back
#define f(i, s, e) for (long long int i = s; i < e; i++)
#define cf(i, s, e) for (long long int i = s; i <= e; i++)
#define rf(i, e, s) for (long long int i = e - 1; i >= s; i--)
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define MOD 1000000007
#define PI 3.1415926535897932384626433832795

#ifndef ONLINE_JUDGE
#define debug(x)                                                               \
  cerr << #x << " ";                                                           \
  _print(x);                                                                   \
  cerr << endl;
#else
#define debug(x)
#endif

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);
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 << "]";
}

ll left_recur(vector<vector<char>> &arr, ll i, ll j, ll prevI, ll prevJ) {

  ll left, up, down;
  left = up = down = 0;
  debug(i) debug(j)
      debug(arr[i][j]) if (j + 1 < arr[0].size() and arr[i][j + 1] == 'B') {
    left = 1 + left_recur(arr, i, j + 1, i, j);
  }
  if (i - 1 >= 0 and arr[i - 1][j] == 'B' and i - 1 != prevI) {
    up = 1 + left_recur(arr, i - 1, j, i, j);
  }
  if (i + 1 < 2 and arr[i + 1][j] == 'B' and i + 1 != prevI) {
    down = 1 + left_recur(arr, i + 1, j, i, j);
  }
  ll temp = max(left, max(up, down));
  debug(temp) return max(left, max(up, down));
}
ll brute_recur(vector<vector<char>> &arr, ll i, ll j, ll prevI, ll prevJ) {

  ll left, up, down;
  left = up = down = 0;
  // if()
  debug(i) debug(j) debug(arr[i][j]) if (j - 1 >= 0 and arr[i][j - 1] == 'B') {
    left = 1 + brute_recur(arr, i, j - 1, i, j);
  }
  if (i - 1 >= 0 and arr[i - 1][j] == 'B' and i - 1 != prevI) {
    up = 1 + brute_recur(arr, i - 1, j, i, j);
  }
  if (i + 1 < 2 and arr[i + 1][j] == 'B' and i + 1 != prevI) {
    down = 1 + brute_recur(arr, i + 1, j, i, j);
  }
  ll temp = max(left, max(up, down));
  debug(temp) return temp;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  int t = 1;
  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> s;
    vector<ll> left;
    vector<ll> right;

    for (ll i = l - 1; i < r; i++)
      s.pb(a[i]);

    for (ll i = 0; i < l - 1; i++)
      left.pb(a[i]);
    for (ll i = r; i < n; i++)
      right.pb(a[i]);

    sort(all(s), greater<>());
    sort(all(left));
    sort(all(right));
    ll i = 0, j = 0, sum = 0;
    sum = accumulate(all(s), (ll)0);

    ll temp = sum;
    while (i < left.size()) {
      if (j < s.size() and left[i] < s[j]) {
        temp -= s[j];
        temp += left[i];
        i++;
        j++;
      } else {
        break;
      }
    }
    i = 0, j = 0;

    ll temp1 = sum;
    while (i < right.size()) {
      if (j < s.size() and right[i] < s[j]) {
        temp1 -= s[j];
        temp1 += right[i];
        i++;
        j++;
      } else {
        break;
      }
    }
    vector<ll> ans = {sum, temp1, temp};

    cout << *min_element(all(ans)) << endl;

    // debug(sum)
  }

  return 0;
}