ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2057B - Gorilla and the Exam
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n, k;
        cin >> n >> k;
        int a[n];
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        sort(a, a + n);
        int y = a[0];
        int countt = 1;
        int b[n] = {0};
        for (int i = 1; i < n; i++)
        {
            if(y == a[i])
            {
                countt++;
            }
            else
            {
                b[countt - 1]++;
                y = a[i];
                countt = 1;
            }
        }
        b[countt - 1]++;
        int ans = 0;
        for (int i = 0; i < int(sizeof(b) / sizeof(int)); i++)
        {
            ans += b[i];
        }
        // cout << ans << endl;
        for (int i = 0; i < int(sizeof(b) / sizeof(int)); i++)
        {
            if(k == 0)
            {
                break;
            }
            if(k < b[i]*(i+1))
            {
                ans -= k/(i+1);
            }
            else
            {
                k -= b[i]*(i+1);
                ans -= b[i];
            }
        }
        if(ans == 0)
        {
            ans = 1;
        }
        cout << ans << endl;
    }
    return 0;
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n, k;
        cin >> n >> k;
        int a[n];
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        sort(a, a + n);
        int y = a[0];
        int countt = 1;
        int b[n] = {0};
        for (int i = 1; i < n; i++)
        {
            if(y == a[i])
            {
                countt++;
            }
            else
            {
                b[countt - 1]++;
                y = a[i];
                countt = 1;
            }
        }
        b[countt - 1]++;
        int ans = 0;
        for (int i = 0; i < int(sizeof(b) / sizeof(int)); i++)
        {
            ans += b[i];
        }
        // cout << ans << endl;
        for (int i = 0; i < int(sizeof(b) / sizeof(int)); i++)
        {
            if(k == 0)
            {
                break;
            }
            if(k < b[i]*(i+1))
            {
                ans -= k/(i+1);
                k = 0;
            }
            else
            {
                k -= b[i]*(i+1);
                ans -= b[i];
            }
        }
        if(ans == 0)
        {
            ans = 1;
        }
        cout << ans << endl;
    }
    return 0;
}
Problem: 2061D - Kevin and Numbers
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 3):
#include<bits/stdc++.h>
 
#define ll long long int
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
 
using namespace std;

void solve() {
    int n, m; cin >> n >> m;

    if(m > n) {
        cout << "NO" << endl;
        return;
    }

    vector<int> a, b;
    map<int, int> freq_a, freq_b;

    for(int i=0;i<n;i++) {
        int x;
        cin >> x;
        freq_a[x]++;
    }

    for(int i=0;i<m;i++) {
        int x;
        cin >> x;
        freq_b[x]++;
    }

    for(auto& [k, v]:freq_b) {
        while(v && freq_a[k]) {
            v--;
            freq_a[k]--;
        }
    }

    for(auto [k, v]:freq_b) {
        while(v--) b.push_back(k); 
    }

    for(auto [k, v]:freq_a) {
        while(v--) a.push_back(k); 
    }

    sort(all(a)), sort(all(b));

    //for(auto x:a) cout << x << " ";
    //cout << endl;

    //for(auto x:b) cout << x << " ";
    //cout << endl;

    int i = 0, j = 0;
    bool ans = true; 

    while(i < a.size() && j < b.size()) {
        while(i< n-1 && b[j] > a[i] && a[i+1] <= a[i]+1) {
            a[i+1] += a[i];
            i++;
        }

        if(b[j] != a[i]) {
            ans = false;
            break;
        }
        i++, j++;
    }
    //cout << "i: " << i << " j: " << j << endl;
    //cout << "ans: " << ans << endl;
    if(i < a.size()|| j < b.size()) ans = false;

    if(ans) cout << "YES" << endl;
    else cout << "NO" << endl;

    return;
}

int main()
{
    fast;
 
    ll t = 1;
    cin >> t;
    while(t--) {
        solve();
    }    
 
}
Problem: 2061D - Kevin and Numbers
Language: C++17 (GCC 7-32)

Previous submission (verdict: Compilation error):
#include<bits/stdc++.h>
 
#define ll long long int
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
 
using namespace std;
using int = ll;

void solve() {
    int n, m; cin >> n >> m;

    if(m > n) {
        cout << "NO" << endl;
        return;
    }

    vector<int> a, b;
    map<int, int> freq_a, freq_b;

    for(int i=0;i<n;i++) {
        int x;
        cin >> x;
        freq_a[x]++;
    }

    for(int i=0;i<m;i++) {
        int x;
        cin >> x;
        freq_b[x]++;
    }

    for(auto& [k, v]:freq_b) {
        while(v && freq_a[k]) {
            v--;
            freq_a[k]--;
        }
    }

    for(auto [k, v]:freq_b) {
        while(v--) b.push_back(k); 
    }

    for(auto [k, v]:freq_a) {
        while(v--) a.push_back(k); 
    }

    sort(all(a)), sort(all(b));

    //for(auto x:a) cout << x << " ";
    //cout << endl;

    //for(auto x:b) cout << x << " ";
    //cout << endl;

    int i = 0, j = 0;
    bool ans = true; 

    while(i < a.size() && j < b.size()) {
        while(i< n-1 && b[j] > a[i] && a[i+1] <= a[i]+1) {
            a[i+1] += a[i];
            i++;
        }

        if(b[j] != a[i]) {
            ans = false;
            break;
        }
        i++, j++;
    }
    //cout << "i: " << i << " j: " << j << endl;
    //cout << "ans: " << ans << endl;
    if(i < a.size()|| j < b.size()) ans = false;

    if(ans) cout << "YES" << endl;
    else cout << "NO" << endl;

    return;
}

int main()
{
    fast;
 
    ll t = 1;
    cin >> t;
    while(t--) {
        solve();
    }    
 
}


Submission (verdict: Accepted):
#include<bits/stdc++.h>
 
#define ll long long int
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
 
using namespace std;

void solve() {
    int n, m; cin >> n >> m;

    if(m > n) {
        cout << "NO" << endl;
        return;
    }

    vector<ll> a, b;
    map<ll, ll> freq_a, freq_b;

    for(int i=0;i<n;i++) {
        int x;
        cin >> x;
        freq_a[x]++;
    }

    for(int i=0;i<m;i++) {
        int x;
        cin >> x;
        freq_b[x]++;
    }
    stack<int> st;
    for(auto& [k, v]:freq_b) {
        while(v) {
            v--;
            st.push(k);
        }
    }
    
    bool ans = true;
    while(!st.empty()) {
        auto x = st.top();
        st.pop();

        if(freq_a.count(x)) {
            freq_a[x]--;
            if(freq_a[x] == 0)
                freq_a.erase(x);
        } else {
            if(x == 1) {
                ans = false;
                break;
            }
            
            st.push(x/2);
            st.push((x+1)/2);
        }
    }

    if(freq_a.size())
        ans = false;

    if(ans) cout << "YES" << endl;
    else cout << "NO" << endl;

    return;
}

int main()
{
    fast;
 
    ll t = 1;
    cin >> t;
    while(t--) {
        solve();
    }    
 
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
 
#define ll long long int
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
 
using namespace std;

void solve() {
    int n; cin >> n;
    map<int, set<int>> tree;

    for(int i=0;i<n-1;i++) {
        int u, v;
        cin >> u >> v;

        tree[u].insert(v);
        tree[v].insert(u);
    }

    int max_indegree = 0;
    int max_ind_node = -1;
    int ans = 0;
    for(auto& [k, v]:tree) {
        if(v.size()>max_indegree) {
            max_indegree = v.size();
            max_ind_node = k;
        }
    }
    ans += tree[max_ind_node].size();    
    for(auto nei:tree[max_ind_node]) {
        tree[nei].erase(max_ind_node);
    }
    tree.erase(max_ind_node);

    max_indegree = 0;
    max_ind_node = -1;
    for(auto& [k, v]:tree) {
        if(v.size()>max_indegree) {
            max_indegree = v.size();
            max_ind_node = k;
        }
    }
    ans += tree[max_ind_node].size() - 1; 
    cout << ans << endl;
}

int main()
{
    fast;
 
    ll t = 1;
    cin >> t;
    while(t--) {
        solve();
    }    
 
}


Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
 
#define ll long long int
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
 
using namespace std;

void solve() {
    int n; cin >> n;
    map<int, set<int>> tree;

    for(int i=0;i<n-1;i++) {
        int u, v;
        cin >> u >> v;

        tree[u].insert(v);
        tree[v].insert(u);
    }

    int max_indegree = 0;
    int max_ind_node = -1;
    for(auto& [k, v]:tree) {
        if(v.size()>max_indegree) {
            max_indegree = v.size();
            max_ind_node = k;
        }
    }
    int ans = max_indegree; 
    for(auto nei:tree[max_ind_node]) {
        tree[nei].erase(max_ind_node);
    }
    tree.erase(max_ind_node);

    assert(tree.find(max_ind_node) == tree.end());
    
    int last = max_ind_node;
    max_indegree = 0;
    max_ind_node = -1;
    for(auto& [k, v]:tree) {
        assert(v.find(last) == v.end());
        if(v.size()>max_indegree) {
            max_indegree = v.size();
            max_ind_node = k;
        }
    }
    ans += max_indegree - 1;
    cout << ans << endl;
}

int main()
{
    fast;
 
    ll t = 1;
    cin >> t;
    while(t--) {
        solve();
    }    
 
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
 
#define ll long long int
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
 
using namespace std;

void solve() {
    int n; cin >> n;
    map<int, set<int>> tree;

    for(int i=0;i<n-1;i++) {
        int u, v;
        cin >> u >> v;

        tree[u].insert(v);
        tree[v].insert(u);
    }

    int max_indegree = 0;
    int max_ind_node = -1;
    for(auto& [k, v]:tree) {
        if(v.size()>max_indegree) {
            max_indegree = v.size();
            max_ind_node = k;
        }
    }
    int ans = max_indegree; 
    for(auto nei:tree[max_ind_node]) {
        tree[nei].erase(max_ind_node);
    }
    tree.erase(max_ind_node);

    assert(tree.find(max_ind_node) == tree.end());
    
    int last = max_ind_node;
    max_indegree = 0;
    max_ind_node = -1;
    for(auto& [k, v]:tree) {
        assert(v.find(last) == v.end());
        if(v.size()>max_indegree) {
            max_indegree = v.size();
            max_ind_node = k;
        }
    }
    ans += max_indegree - 1;
    cout << ans << endl;
}

int main()
{
    fast;
 
    ll t = 1;
    cin >> t;
    while(t--) {
        solve();
    }    
 
}


Submission (verdict: Accepted):
#include<bits/stdc++.h>
 
#define ll long long int
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
 
using namespace std;

void solve() {
    int n; cin >> n;
    map<int, set<int>> tree;

    for(int i=0;i<n-1;i++) {
        int u, v;
        cin >> u >> v;

        tree[u].insert(v);
        tree[v].insert(u);
    }

    int max_indegree = 0;
    int max_ind_node = -1;
    for(auto& [k, v]:tree) {
        if(v.size()>max_indegree) {
            max_indegree = v.size();
            max_ind_node = k;
        }
    }
    int ans = max_indegree; 
    int count = 0;
    for(auto nei:tree[max_ind_node]) {
        if(tree[nei].size() == max_indegree) count++;
        tree[nei].erase(max_ind_node);
    }
    tree.erase(max_ind_node);

    if(count >= 2) {
        cout << max_indegree*2 - 1 << endl;
        return;
    }

    int last = max_ind_node;
    max_indegree = 0;
    max_ind_node = -1;
    for(auto& [k, v]:tree) {
        if(v.size()>max_indegree) {
            max_indegree = v.size();
            max_ind_node = k;
        }
    }
    ans += max_indegree - 1;
    cout << ans << endl;
}

int main()
{
    fast;
 
    ll t = 1;
    cin >> t;
    while(t--) {
        solve();
    }    
 
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
 
#define ll long long int
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
 
using namespace std;

void solve() {
    int n; cin >> n;
    map<int, set<int>> tree;

    for(int i=0;i<n-1;i++) {
        int u, v;
        cin >> u >> v;

        tree[u].insert(v);
        tree[v].insert(u);
    }

    int max_indegree = 0;
    int max_ind_node = -1;
    int ans = 0;
    for(auto& [k, v]:tree) {
        if(v.size()>max_indegree) {
            max_indegree = v.size();
            max_ind_node = k;
        }
    }
    ans += tree[max_ind_node].size();    
    for(auto nei:tree[max_ind_node]) {
        tree[nei].erase(max_ind_node);
    }
    tree.erase(max_ind_node);

    max_indegree = 0;
    max_ind_node = -1;
    for(auto& [k, v]:tree) {
        if(v.size()>max_indegree) {
            max_indegree = v.size();
            max_ind_node = k;
        }
    }
    ans += tree[max_ind_node].size() - 1; 
    cout << ans << endl;
}

int main()
{
    fast;
 
    ll t = 1;
    cin >> t;
    while(t--) {
        solve();
    }    
 
}
Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Compilation error):
/**
 *    author:  tiansheng
 *    created: 2024.12.30 15:53:00 IST
 **/
#include <bits/stdc++.h>
#include "debug.h"
using namespace std;
#ifdef LOCAL
#include "debug.h"
#endif

void solve()
{
    string s;
    cin >> s;
    int i,j;
    for (int k = 0; k < s.length() + 1; k++)
    {
        if((s[k+1] - s[k]) > 1){
            i = k;j = k+1;
            do
            {
                s[j]--;
                // cout<<"s[j] "<<s[j]<<endl;
                swap(s[i],s[j]);
                i--;j--;
                // cout<<"cod "<<( i > 0 && j > 0 && ((s[j] - s[i]) > 1))<<endl;
            } while ( i >= 0 && j >= 0 && ((s[j] - s[i]) > 1));
            
        }
    }
    cout<<s<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int tt;
    cin >> tt;
    while (tt--)
    {
        solve();
    }
    return 0;
}

Submission (verdict: Wrong answer on test 2):
/**
 *    author:  tiansheng
 *    created: 2024.12.30 15:53:00 IST
 **/
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#endif

void solve()
{
    string s;
    cin >> s;
    int i,j;
    for (int k = 0; k < s.length() + 1; k++)
    {
        if((s[k+1] - s[k]) > 1){
            i = k;j = k+1;
            do
            {
                s[j]--;
                // cout<<"s[j] "<<s[j]<<endl;
                swap(s[i],s[j]);
                i--;j--;
                // cout<<"cod "<<( i > 0 && j > 0 && ((s[j] - s[i]) > 1))<<endl;
            } while ( i >= 0 && j >= 0 && ((s[j] - s[i]) > 1));
            
        }
    }
    cout<<s<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int tt;
    cin >> tt;
    while (tt--)
    {
        solve();
    }
    return 0;
}
Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
/**
 *    author:  tiansheng
 *    created: 2024.12.30 15:53:00 IST
 **/
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#endif

void solve()
{
    string s;
    cin >> s;
    int i,j;
    for (int k = 0; k < s.length() + 1; k++)
    {
        if((s[k+1] - s[k]) > 1){
            i = k;j = k+1;
            do
            {
                s[j]--;
                // cout<<"s[j] "<<s[j]<<endl;
                swap(s[i],s[j]);
                i--;j--;
                // cout<<"cod "<<( i > 0 && j > 0 && ((s[j] - s[i]) > 1))<<endl;
            } while ( i >= 0 && j >= 0 && ((s[j] - s[i]) > 1));
            
        }
    }
    cout<<s<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int tt;
    cin >> tt;
    while (tt--)
    {
        solve();
    }
    return 0;
}

Submission (verdict: Accepted):
/**
 *    author:  tiansheng
 *    created: 2024.12.30 15:53:00 IST
 **/
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#endif

void solve()
{
    string s;
    cin >> s;
    int i, j;
    for (int k = 0; (k + 1) < s.length(); k++)
    {
        if ((s[k + 1] - s[k]) > 1)
        {
            i = k;
            j = k + 1;
            do
            {
                swap(s[i], --s[j]);
                i--; j--;
            } while (i >= 0 && j >= 0 && ((s[j] - s[i]) > 1));
        }
    }
    cout << s << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int tt;
    cin >> tt;
    while (tt--)
    {
        solve();
    }
    return 0;
}
Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Compilation error):
/**
 *    author:  tiansheng
 *    created: 2024.12.30 15:53:00 IST
 **/
#include <bits/stdc++.h>
#include "debug.h"
using namespace std;
#ifdef LOCAL
#include "debug.h"
#endif

void solve()
{
    string s;
    cin >> s;
    int i,j;
    for (int k = 0; k < s.length() + 1; k++)
    {
        if((s[k+1] - s[k]) > 1){
            i = k;j = k+1;
            do
            {
                s[j]--;
                // cout<<"s[j] "<<s[j]<<endl;
                swap(s[i],s[j]);
                i--;j--;
                // cout<<"cod "<<( i > 0 && j > 0 && ((s[j] - s[i]) > 1))<<endl;
            } while ( i >= 0 && j >= 0 && ((s[j] - s[i]) > 1));
            
        }
    }
    cout<<s<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int tt;
    cin >> tt;
    while (tt--)
    {
        solve();
    }
    return 0;
}
Problem: 2063B - Subsequence Update
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 2):
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    //t test case
    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];
    }
    sort(a.begin(),a.begin()+l-1);
    sort(a.begin()+l-1,a.begin()+r);
    sort(a.end()-n+r,a.end());
    int sum1=0;
    int sum2=0;
    int i=0;
    int j=r-1;
    // for(int i=0;i<n;i++){
    //     cout<<a[i]<<" ";
    // }
    while(a[j]>a[i] && i<l-1){
        sum1+=a[j]-a[i];
        i++;
        j--;
    }
    i=r-1;
    j=r;
    while(a[i]>a[j] && j<n){
        sum2+=a[i]-a[j];
        i--;
        j++;
    }
    int sumf=0;
    for(int i=l-1;i<r;i++){
        sumf+=a[i];
    }
    // cout<<sum1<<" "<<sum2<<endl;
    cout<<sumf-max(sum1,sum2)<<endl;
    }
}
Problem: 2063B - Subsequence Update
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    //t test case
    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];
    }
    sort(a.begin(),a.begin()+l-1);
    sort(a.begin()+l-1,a.begin()+r);
    sort(a.end()-n+r,a.end());
    int sum1=0;
    int sum2=0;
    int i=0;
    int j=r-1;
    // for(int i=0;i<n;i++){
    //     cout<<a[i]<<" ";
    // }
    while(a[j]>a[i] && i<l-1){
        sum1+=a[j]-a[i];
        i++;
        j--;
    }
    i=r-1;
    j=r;
    while(a[i]>a[j] && j<n){
        sum2+=a[i]-a[j];
        i--;
        j++;
    }
    int sumf=0;
    for(int i=l-1;i<r;i++){
        sumf+=a[i];
    }
    // cout<<sum1<<" "<<sum2<<endl;
    cout<<sumf-max(sum1,sum2)<<endl;
    }
}

Submission (verdict: Wrong answer on test 8):
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    //t test case
    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];
    }
    sort(a.begin(),a.begin()+l-1);
    sort(a.begin()+l-1,a.begin()+r);
    sort(a.end()-n+r,a.end());
    int sum1=0;
    int sum2=0;
    int i=0;
    int j=r-1;
    // for(int i=0;i<n;i++){
    //     cout<<a[i]<<" ";
    // }
    while(a[j]>a[i] && i<l-1 && j>=l-1){
        sum1+=a[j]-a[i];
        i++;
        j--;
    }
    i=r-1;
    j=r;
    while(a[i]>a[j] && j<n && i>=l-1){
        sum2+=a[i]-a[j];
        i--;
        j++;
    }
    int sumf=0;
    for(int i=l-1;i<r;i++){
        sumf+=a[i];
    }
    // cout<<sum1<<" "<<sum2<<endl;
    cout<<sumf-max(sum1,sum2)<<endl;
    }
}
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;

int main(){
    //t test case
    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];
    }
    sort(a.begin(),a.begin()+l-1);
    sort(a.begin()+l-1,a.begin()+r);
    sort(a.end()-n+r,a.end());
    int sum1=0;
    int sum2=0;
    int i=0;
    int j=r-1;
    // for(int i=0;i<n;i++){
    //     cout<<a[i]<<" ";
    // }
    while(a[j]>a[i] && i<l-1 && j>=l-1){
        sum1+=a[j]-a[i];
        i++;
        j--;
    }
    i=r-1;
    j=r;
    while(a[i]>a[j] && j<n && i>=l-1){
        sum2+=a[i]-a[j];
        i--;
        j++;
    }
    int sumf=0;
    for(int i=l-1;i<r;i++){
        sumf+=a[i];
    }
    // cout<<sum1<<" "<<sum2<<endl;
    cout<<sumf-max(sum1,sum2)<<endl;
    }
}

Submission (verdict: Accepted):
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    //t test case
    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];
    }
    sort(a.begin(),a.begin()+l-1);
    sort(a.begin()+l-1,a.begin()+r);
    sort(a.end()-n+r,a.end());
    long long int sum1=0;
    long long int sum2=0;
    int i=0;
    int j=r-1;
    // for(int i=0;i<n;i++){
    //     cout<<a[i]<<" ";
    // }
    while(a[j]>a[i] && i<l-1 && j>=l-1){
        sum1+=a[j]-a[i];
        i++;
        j--;
    }
    i=r-1;
    j=r;
    while(a[i]>a[j] && j<n && i>=l-1){
        sum2+=a[i]-a[j];
        i--;
        j++;
    }
    long long int sumf=0;
    for(int i=l-1;i<r;i++){
        sumf+=a[i];
    }
    // cout<<sum1<<" "<<sum2<<endl;
    cout<<sumf-max(sum1,sum2)<<endl;
    }
}