ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 92 ms):
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int main() {
	int t;
	cin>>t;
	while(t--) {
		string s;
		cin>>s;
		int l=s.size();
		for(int i=0;i<l;i++) {
			int pos=0,mxi=s[i]-'0';
			for(int j=1;j<=8;j++) {
				if(i+j>=l) break;
				if(s[i+j]-j-'0'>mxi) {
					pos = i+j;
					mxi = s[i+j]-'0'-j;
				}
			}
			if(pos) {
				for(int k=pos;k>i;k--) {
					s[k] = s[k-1];
				}
				s[i] = mxi+'0';
			}
		}
		cout<<s<<"\n";
	} 
	return 0;
}


Solution 2 (runtime: 46 ms):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define bhavya ios::sync_with_stdio(0);cin.tie(0); 

void sieveOfEratosthenes(int n) 
{
    vector<bool> prime(n + 1, true);
    prime[0] = prime[1] = false;

    for (int p = 2; p * p <= n; ++p) {
        if (prime[p]) {
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }

    cout << "Prime numbers up to " << n << " are:\n";
    for (int p = 2; p <= n; ++p) {
        if (prime[p])
            cout << p << " ";
    }
    cout << endl;
}

int gcd(int a, int b) 
{
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int lcm(int a, int b) 
{
    return (a * b) / gcd(a, b);
}

bool isPrime(int n) 
{
    if (n <= 1) return false;
    if (n <= 3) return true;
    if (n % 2 == 0 || n % 3 == 0) return false;
    for (int i = 5; i * i <= n; i += 6) {
        if (n % i == 0 || n % (i + 2) == 0) {
            return false;
        }
    }
    return true;
}



void solve()
{
   string s; 
   cin >> s;
    for (int i = 0; i < s.size(); i++) 
    {
        int best = s[i] - '0', pos = i;
        for (int j = i; j < min(i + 10, (int) s.size()); j++) 
        {
            if (s[j] - '0' - (j - i) > best) {
                best = s[j] - '0' - (j - i);
                pos = j;
            }
        }
        while (pos > i) {
            swap(s[pos], s[pos - 1]);
            pos--;
        }
        s[i] = char(best + '0');
    }
    cout << s << endl;
    
    

}

int main() 
{
    bhavya;
    int t;
    //t=1;
    cin >> t;
    while(t--) 
    {
    
        solve();
    }
    return 0;
}
Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 249 ms):
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

typedef long long LL;

const int N = 300010;

vector<int> e[N];

int n, ans, f1, f2;
void dfs(int u, int fa) {
    if (fa == -1 || e[fa].size() != f1) {
        ans = max(ans, (int)e[u].size() + f1 - 1);
    } else {
        ans = max(ans, (int)e[u].size() + f2 - 1);
        ans = max(ans, (int)e[u].size() + f1 - 2);
    }
    if (e[u].size() > f1) f2 = f1, f1 = e[u].size();
    else if (e[u].size() > f2) f2 = e[u].size();
    for (int j : e[u]) {
        if (j == fa) continue;
        dfs(j, u);
    }
}

int main() {
    int T;
    scanf("%d", &T);
    while (T --) {
        scanf("%d", &n);
        for (int i = 1; i <= n; i ++) e[i].clear();
        for (int i = 1; i < n; i ++) {
            int a, b;
            scanf("%d%d", &a, &b);
            e[a].push_back(b);
            e[b].push_back(a);
        }
        ans = f1 = f2 = 0;
        dfs(1, -1);
        printf("%d\n", ans);
    }
	return 0;
} 

Solution 2 (runtime: 531 ms):
#include <bits/stdc++.h>

#define pb push_back
#define sz(v) ((int)(v).size())

using namespace std;

class Task {
public:
	void solveOne() {
		int l, x, y, result;
		cin >> l;
		map<int, vector<int>> conn;
		for (int i = 0; i < l-1; i++) {
			cin >> x >> y;
			conn[x].pb(y);
			conn[y].pb(x);
		}
		if (l <= 4) {
			cout << l-2 << "\n";
			return;
		}
		map<int, vector<int>> deg;
		for (int i = 1; i <= l; i++) {
			x = sz(conn[i]);
			deg[x-1].pb(i);
		}
		auto ptr = --deg.end();
		bool oneMore = false;
		if (sz((*ptr).second) >= 2) {
			// check if 2 in this group are not adjacent
			result = (*ptr).first * 2;
			vector<int> gp_v = (*ptr).second;
			if (sz(gp_v) > 2) {
				oneMore = true;
			}
			else { // == 2
				x = gp_v[0];
				y = gp_v[1];
				oneMore = true;
				for (int node : conn[x]) {
					if (node == y) {
						oneMore = false;
					}
				}
			}
		}
		else {
			// check the max one has a non-adjacent in the group of second most
			auto ptr2 = ptr;
			--ptr2;
			result = (*ptr).first + (*ptr2).first;
			x = (*ptr).second[0];
			vector<int> gp_v = (*ptr2).second;
			set<int> gp;
			for (int i : gp_v) {
				gp.insert(i);
			}
			set<int> touched;
			for (int i : conn[x]) {
				if (gp.find(i) != gp.end()) {
					touched.insert(i);
				}
 			}
			if (sz(touched) < sz(gp_v)) {
				oneMore = true;
			}
		}
		cout << result + oneMore << "\n";
	}
	void solve() {
		int t;
		cin >> t;
		for (int i = 1; i <= t; i++) {
			solveOne();
		}
	}
};
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	Task solver;
	solver.solve();
	return 0;
}
Problem: 2050E - Three Strings
Language: c++17 

Solution 1 (runtime: 109 ms):
#include <bits/stdc++.h>
using namespace std;
#define int long long 
int dp[2005][2005];
void solve(){
    string a , b ,c ;
    cin >> a >> b >> c;
    int l1 , l2 , l3;
    l1 = a.length() , l2 = b.length();
    l3 = l1 + l2;
    for(int i = 0 ; i <= l1 ; i++){
        for(int j = 0 ; j <= l2 ; j++){
            dp[i][j] = 1e9;
        }
    }
    dp[0][0] = 0;
    for(int i = 1 ; i <= l3 ; i++){
        for(int j = 1 ; j <= min(i , l1) ; j++){//a
            if(i - j >= 0)
                dp[j][i - j] = min(dp[j - 1][i - j] + (c[i - 1] != a[j - 1]) , dp[j][i - j]);
            else break;
        }
        for(int j = 1 ; j <= min(i , l2) ; j++){
            if(i - j >= 0)
                dp[i - j][j] = min(dp[i - j][j - 1] + (c[i - 1] != b[j - 1]) , dp[i - j][j]);
            else break;
        }
    }
    cout << dp[l1][l2] <<"\n";
    return ;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    cin>>t;
    while(t--){
        solve();
    }
    system("pause");
    return 0;
}

Solution 2 (runtime: 62 ms):
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
const int MX=1e9+7;
int dp[1050][1050];//当前这一位用了i个a，j个b 
int main() {
	int t;
	cin>>t;
	while(t--) {
		string a,b,c;
		cin>>a>>b>>c;
		int la=a.size(),lb=b.size();
		
		for(int i=0;i<=la;i++) for(int j=0;j<=lb;j++) dp[i][j] = MX;
		
		int lc=la+lb;
		dp[0][0] = 0;
		for(int i=0;i<=la;i++) {
			for(int j=0;j<=lb;j++) {//多少个 
				if(i+j-1>=0) {
					if(j>=1 && c[i+j-1] == b[j-1]) {
						dp[i][j] = min(dp[i][j],dp[i][j-1]);
					} else if(j>=1){
						dp[i][j] = min(dp[i][j],dp[i][j-1]+1);
					}
					if(i>=1 && c[i+j-1] == a[i-1]) {
						dp[i][j] = min(dp[i][j],dp[i-1][j]);
					} else if(i>=1){
						dp[i][j] = min(dp[i][j],dp[i-1][j]+1);
					}
				}
			}
		} 
		printf("%d\n",dp[la][lb]);
	} 
	return 0;
}
Problem: 2057C - Trip to the Olympiad
Language: c++17 

Solution 1 (runtime: 61 ms):
#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

#define dbg(x) cerr << #x << ": " << x << '\n';
#define dbgv(v) cerr << #v << ": "; for(auto &el : v) cerr << el << " "; cerr << '\n';

using namespace std;
using ll = long long;

const ll MOD = 1e9 + 7;
const ll INF = 1e18 + 5;

void solve(){
    int l, r;
    cin >> l >> r;
    int log = 31 - __builtin_clz(l ^ r);    
    int a = l | ((1 << log) - 1), b = a + 1, c = (a == l ? r : l);
    cout << a << " " << b << " " << c << "\n";
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t;
    cin >> t;
    while(t--){
        solve();
    }
}


Solution 2 (runtime: 77 ms):
#include<bits/stdc++.h>
using namespace std;
#define int long long
int t,l,r;
void work()
{
    for(int w = (1ll << 30);w >= 1;w >>= 1)
        if((l & w) != (r & w)) {
            int x = r / w * w;
            cout<<x<<" "<<x - 1<<" "<<(r == x ? l : r)<<endl;
            return;
        }
}
signed main()
{
    ios::sync_with_stdio(false);
    
    cin>>t;
    while(t--) cin>>l>>r,work();
    
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Solution 1 (runtime: 108 ms):
#include <bits/stdc++.h>

using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--){
        int n, m;
        cin >> n >> m;
        vector<int>a(n);
        for (int i=0;i<n;i++)
            cin>>a[i];
        map<int, int>mp;
        for (int i:a)
            mp[i]++;
        vector<int>v;
        for (auto it:mp)
            v.push_back(it.second);
        sort(v.begin(), v.end());
        int red=0;
        for (int i=0;i<v.size();i++){
            if (m>=v[i]){
                m-=v[i];
                red++;
            }
        }
        cout << max(1, int(mp.size()-red)) << "\n";
    }
}

Solution 2 (runtime: 217 ms):
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        vector<int> arr(n);
        for(int i=0;i<n;i++){
            cin>>arr[i];   
        }
        map<int,int> freq;
        for(int num: arr){
            freq[num]++;
        }
        vector<int> counts;
        for(auto &entry: freq){
            counts.push_back(entry.second);
        }
        sort(counts.begin(),counts.end());
        long long sum=0;
        int i=0;
        for(int count: counts){
            if(sum+count<=k){
                i++;
                sum+=count;
            }
            else{
                break;
            }
        }
        int ans=counts.size()-i;
        if(ans>0){
            cout<<ans<<endl;
        }
        else{
            cout<<1<<endl;
        }
    }
    return 0;
}
Problem: 2063B - Subsequence Update
Language: c++17 

Solution 1 (runtime: 77 ms):
#pragma GCC optimize("O3,unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define watch(x) cerr << endl << (#x) << " is " << (x) << endl
#define mod  1000000007
#define INF 0x3F3F3F3F
#define ll long long
#define noo { cout<<"NO"<<endl; return; }
#define pprint(a,b) {cout<<"("<<a<<","<<b<<")"<<endl;}
#define pb push_back
#define MAXN 3005
#define ff first
#define ss second
typedef  pair<int,int>  pii;
int dx[8] = {1,0,-1,0,1,1,-1,-1};
int dy[8] = {0,1,0,-1,1,-1,1,-1};
const int N = (int)5e5+5;


void solve(){
    int n, l, r;
    cin>>n>>l>>r;
    vector<int>arr(n);
    for(int i=0;i<n;i++){
        cin>>arr[i];
        // cout<<arr[i]<<" ";
    }
    l--, r--;
    vector<int>arr1, arr2;
    for(int i=0;i<=r;i++){
        // cout<<arr[i]<<" ";
        arr1.push_back(arr[i]);
    }
    for(int i=l;i<n;i++){
        arr2.push_back(arr[i]);
    }
    sort(arr1.begin(), arr1.end());
    sort(arr2.begin(), arr2.end());
    ll ans1 = 0;
    for(int i=0;i<r-l+1;i++){
        // cout<<arr1[i]<<" ";
        ans1 += arr1[i]; 
    }
    // cout<<"\n";
    ll ans2 = 0;
    for(int i=0;i<r-l+1;i++){
        // cout<<arr2[i]<<" ";
        ans2 += arr2[i];
    }
    // cout<<"\n";
    ll ans = 0;
    if(ans1!=0 && ans2!=0) ans = min(ans1,ans2);
    else if(ans1==0) ans = ans2;
    else ans = ans1;
    cout<<ans<<"\n";


}

int main(){
    fastio
    int t;
    cin>>t;
    while(t--)solve();

    return 0;
}

Solution 2 (runtime: 186 ms):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;



void solve(){

    int n, l, r;

    cin>>n>>l>>r;

    vector<ll> prefix(r), suffix(n - l + 1);

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

    	ll input;

    	cin>>input;

    	if(ri < r)

    		prefix[ri] = input;

    	if(ri > l - 2)

    		suffix[ri - l + 1] = input;

    }

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

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

    ll prefixSum = 0, suffixSum = 0;

    for(int ri = 0; ri < r - l + 1; ri++){

    	prefixSum += prefix[ri];

    }

    for(int ri = 0; ri < r - l + 1; ri++){

    	suffixSum += suffix[ri];

    }

    cout<<min(prefixSum, suffixSum)<<endl;

}



int main()

{

    int t;

    scanf("%d",&t);

    while(t--){

        solve();

    }

    return 0;

}
Problem: 2062C - Cirno and Operations
Language: c++17 

Solution 1 (runtime: 62 ms):
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod =1e18;
ll a[55];
int main()
{
    int tc;
    cin>>tc;
    while(tc--)
    {
        int n;
        cin>>n;
        ll tong=0;
        for (int i=1;i<=n;i++)
        {
            cin>>a[i];
            tong+=a[i];
        }
        ll res=tong;
        while(n>1)
        {
           ll sum1=0,sum2=0;
           for (int i=1;i<=n-1;i++)
           {
               sum1+=(a[i]-a[i+1]);
               sum2+=(a[i+1]-a[i]);
           }
           res=max({res,sum1,sum2});
           if (sum1>=sum2)
           {
               reverse(a+1,a+1+n);
               for (int i=1;i<=n-1;i++)
               {
                   a[i]=a[i+1]-a[i];
               }
               n--;
           } else
           {
               for (int i=1;i<=n-1;i++)
               {
                   a[i]=a[i+1]-a[i];
               }
               n--;
           }


        }
        cout<<res<<endl;
    }
}


Solution 2 (runtime: 46 ms):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int

void solve()
{
    ll n;
    cin >> n;
    vector<ll> v;
    ll maxi = 0;
    for (int i = 0; i < n; i++)
    {
        ll x;
        cin >> x;
        v.push_back(x);
        maxi += x;
    }
    if (n == 1)
    {
        cout << maxi << endl;
        return;
    }
    vector<ll> d;
    while (n > 0)
    {
        for (int i = 1; i < n; i++)
        {
            d.push_back(v[i] - v[i - 1]);
        }
        ll p = 0;
        for (int i = 0; i < d.size(); i++)
        {
            p += d[i];
        }
        // cout << abs(p) << " ";
        maxi = max(maxi, abs(p));
        n--;
        v = d;
        d.clear();
    }
    cout << maxi << endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}
Problem: 2057C - Trip to the Olympiad
Language: c++17 

Solution 1 (runtime: 62 ms):
#include <bits/stdc++.h>
using namespace std;

int highestBitPos(unsigned x) {
    return 31 - __builtin_clz(x);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while(t--){
        long long l, r;
        cin >> l >> r;

        long long diff = l ^ r;
        int k = highestBitPos((unsigned)diff);

        long long power = 1LL << k;
        long long x = (l + power - 1) & ~(power - 1);
        if (x > r) {
            x -= power;
        }

        long long y = x - 1;
        if (y < l) {
            x = (r / power) * power; 
            y = x - 1;
        }

        long long z;
        if (l != x && l != y) {
            z = l;
        } else if (r != x && r != y) {
            z = r;
        } else {
            z = l + 1;
        }

        cout << x << " " << y << " " << z << "\n";
    }

    return 0;
}


Solution 2 (runtime: 61 ms):

// KISS <3
 
#include <bits/stdc++.h>
#include<stdio.h>
using namespace std;
 
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
 
// typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> ordered_set;
 
typedef long long ll;

const int N = 200010;
const int M = 1e9 + 7;
const int MOD = 998244353;
// const int INF = 1000000007;
 
struct arr {
	ll val;
	ll idx;
};
 
long long int power(int base, int exp) {
	if (exp == 0)
		return 1;
	else if (exp == 1)
		return base;
	else{
		long long int calc;
		if (exp % 2 == 0){
			calc = power(base, exp/2);
			calc *= calc;
		} else {
			calc = base*power(base, exp-1);
		}
		return calc;
	}
}


int main () {
 
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r", stdin);
	freopen("output.txt","w", stdout);
	#endif
	

	
	ll tc;cin>>tc;
	while(tc--){
		
		ll l, r;cin>>l>>r;
		ll ans = 0;
		ll a = 0, b = 0;
		for(int i=30;i>=0;i--){
			ll b1 = 0, b2 = 0;
			
			if (l & (1ll<<i)) {
				b1 = 1;
			}

			if (r & (1ll<<i)) {
				b2 = 1;
			}
			
			if(b1 == b2){
				ans += b1 * (1ll<< i);
			} else {
				a = ans + (1ll << i);
				b = a - 1;
				break;
			}

		}

		ll c = -1;
		for(int i=l;i<=r;i++){
			if(i != a and i != b){
				c = i;
				break; 
			}
		}

		cout<<a<<' '<<b<<' '<<c<<endl;
	}
	
}
Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 1250 ms):
#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<vector<int>> adj(n);
        vector<int> in(n,0);
        for(int i=0;i<n-1;i++){
            int u,v;
            cin>>u>>v;
            adj[u-1].push_back(v-1);
            adj[v-1].push_back(u-1);
            in[u-1]++;
            in[v-1]++;
        }
        set<pair<int,int>> ins;
        for(int i=0;i<n;i++){
            ins.insert({in[i],i});
        }
        int mx = (*(ins.rbegin())).first;
        int ans = 0;
        for(int i=0;i<n;i++){
            if(in[i]==mx){
                ins.erase({in[i],i});
                for(auto g:adj[i]){
                    ins.erase({in[g],g});
                    in[g]--;
                    ins.insert({in[g],g});
                }
                ans = max(ans,mx+(*(ins.rbegin())).first-1);
                for(auto g:adj[i]){
                    ins.erase({in[g],g});
                    in[g]++;
                    ins.insert({in[g],g});
                }
                ins.insert({in[i],i});
            }
        }
        cout<<min(n-2,ans)<<endl;
    }
}


Solution 2 (runtime: 296 ms):
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int M = 1000000007;


int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    

    int t;
    cin>>t;

    while(t--){
        int n;
        cin>>n;

        vector<vector<int>> v(n);

        for(int i=0; i<n-1; ++i){
            int a, b;
            cin>>a>>b;
            v[a-1].push_back(b);
            v[b-1].push_back(a);
        }
        vector<pair<int, int>> mm(n);
        for(int i=0; i<n; ++i){
            int m=0;
            int mi=-1;
            for(int j=0; j<v[i].size(); ++j){
                m++;
                
            }
            mm[i] = {m , i};
        }

        sort(mm.begin(), mm.end());
        int m1=mm[n-1].second+1;
        int d1= mm[n-1].first;
        int m3=mm[n-2].second+1;
        int d3=mm[n-2].first;
        vector<int> itr;
        itr.push_back(m1);
        if(d1==d3) itr.push_back(m3);
        
        int ans=0;
        for(auto k: itr){
            vector<pair<int, int>> mm1(n);
            for(int i=0; i<n; ++i){
                int m=0;
                int mi=-1;
                if(k==i+1) {mm1[i]={-1,i};continue;}
                for(int j=0; j<v[i].size(); ++j){
                    if(v[i][j]==k) continue;
                    m++;
                    
                }
                mm1[i] = {m , i};
            }
        
        sort(mm1.begin(), mm1.end());
        int m2= mm1[n-1].second+1;
        int d2=max(mm1[n-1].first,0);
        //cout<<"d1 "<<d1<<" d2 "<<d2<<"\n";
        ans = max(d1-1+d2,ans);

        }
        
        

        cout<<ans<<"\n";


    }

    

    

    return 0;

}
Problem: 2063B - Subsequence Update
Language: c++17 

Solution 1 (runtime: 218 ms):
/*********************************************
 * Author  : Gansukh Lkhagva
 * Created : 2025-02-17 20:19
 * Problem : 2063B - Subsequence Update
 * Source  : Codeforces Round #1000 (Div. 2)
 * Time    : 
 * Status  : 
 *********************************************/

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void solve() {
    int n, l , r;
    cin >> n >> l >> r;
    vector<long long> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];    // subarray-iin urtiig oloh
    int t = r - l + 1;
    vector<long long> rsubarray(a.begin(), a.begin() + r);
    vector<long long> lsubarray(a.begin() + l - 1, a.end());
    sort(rsubarray.begin(), rsubarray.end());
    sort(lsubarray.begin(), lsubarray.end());
    long long lsum = 0, rsum = 0;
    for(int i = 0; i < t; i++) {
        lsum += lsubarray[i];
        rsum += rsubarray[i];
    }
    cout << min(lsum, rsum) << endl;
}

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



Solution 2 (runtime: 77 ms):
// mode: short code

/*

practicing..

*/

#include<bits/stdc++.h>



void solve() {

    int n, l, r;

    std::cin >> n >> l >> r;



    std::vector<int> a(n);

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

        std::cin >> a[i];

    }



    std::vector<int> b(a);



    sort(a.begin() + l - 1, a.end());

    long long suf = accumulate(a.begin() + l - 1, a.begin() + r, 0ll);



    sort(b.begin(), b.begin() + r, std::greater<int>());

    long long pre = accumulate(b.begin() + l - 1, b.begin() + r, 0ll);



    std::cout << std::min(pre, suf) << '\n';

    return;

}



int main() {

    std::ios::sync_with_stdio(false);

    std::cin.tie(0);



    int t;

    std::cin >> t;



    while (t--) {

        solve();

    }



    return 0;

}
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 108 ms):
#include <bits/stdc++.h>
using namespace std;
int n,a[200005],i,ans,t,r,q;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    while (t--) {
        cin>>n;
        q = 0;
        for ( i = 1; i<=n;i++) {
            cin>>a[i];
            if (a[i] < a[i-1]) {
                q = 1;
            }
             r = min(a[i-1], a[i]);
            a[i-1]-= r;
            a[i]-=r;
        }
        if (q == 0) {
            cout<<"YES"<<endl;
        } else {
            cout<<"NO"<<endl;
        }
    }

    return 0;
}


Solution 2 (runtime: 343 ms):
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<int> a(n);
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        for(int i=0;i<n-1;i++){
            int x=min(a[i],a[i+1]);
            if(i==0||a[i]-x>=a[i-1]){
                a[i]=a[i]-x;
                a[i+1]=a[i+1]-x;
            }
        }
        bool flag=true;
        for(int i=0;i<n-1;i++)
        if(a[i]>a[i+1]){
            flag=false;
            break;
        }
        if(flag)
        cout<<"YES"<<endl;
        else
        cout<<"NO"<<endl;
    }
}