ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

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

void dfs(int node, int pp , vector<int>&vis, vector<int>graph[]){
    
    vis[node] = 1;
    for(auto ch : graph[node]){
        if(ch == pp)continue ;
        if(!vis[ch]){
            dfs(ch,node,vis,graph);
        }
    }
}

signed main(){
    int t;
    cin>>t;
    
    while(t--){
        
       int n ; cin>>n ;
       
     
       vector<int>indegree(n+1,0);
       
       priority_queue<int>pq;
       for(int i = 1 ; i <= (n-1); i++){
           int u,v ;
           cin>>u>>v;
           indegree[u]++;
           indegree[v]++;
       }
       
       for(int i =1 ; i<=n ; i++){
           pq.push(indegree[i]);
       }
       
       if(n == 2){
           cout<<0<<endl;
       }
       else{
           int a = pq.top();pq.pop();
           int b = pq.top();pq.pop();
        //   cout<<a<<" "<<b<<endl;
           int ans = a+b-1 ;
           if(b == 1){
               ans--;
           }
           cout<<ans<<endl;
       }
       
       
    }
}


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



signed main(){
    int t;
    cin>>t;
    
    while(t--){
        
       int n ; cin>>n ;
       vector<int>indegree(n+1,0);
       vector<pair<int,int>>edges;
       map<int,set<int>>mp;
       for(int i =1  ; i<= (n-1) ; i++){
           int u,v ;
           cin>>u>>v;
           edges.push_back({u,v});
           indegree[u]++;
           indegree[v]++;
           mp[u].insert(v);
           mp[v].insert(u);
       }
       
       int ans = 0 ;
       for(int i = 0 ;  i<edges.size() ; i++){
           ans = max(ans,indegree[edges[i].first] + indegree[edges[i].second] - 2) ;
       }
       
       vector<pair<int,int>>vec;
       for(int i = 1 ; i<=n ; i++){
           vec.push_back({indegree[i],i});
       }
       
       sort(vec.rbegin(),vec.rend());
       
       for(int i = 0 ; i<vec.size(); i++){
           for(int j = i+1 ; j< vec.size() ; j++){
               int u = vec[i].second ;
               int v = vec[j].second ;
               if(mp[u].find(v) == mp[u].end()){
                   ans = max(ans,indegree[v]+indegree[u]-1);
                   break;
               }
               
           }
       }
       
       cout<<ans<<endl;
      
       
    }
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define int long long

void dfs(int node, int pp , vector<int>&vis, vector<int>graph[]){
    
    vis[node] = 1;
    for(auto ch : graph[node]){
        if(ch == pp)continue ;
        if(!vis[ch]){
            dfs(ch,node,vis,graph);
        }
    }
}

signed main(){
    int t;
    cin>>t;
    
    while(t--){
        
       int n ; cin>>n ;
       
       vector<int>graph[n+1];
       vector<int>indegree(n+1,0);
       vector<pair<int,int>>edges;
       
       for(int i = 1 ; i <= (n-1); i++){
           int u,v ;
           cin>>u>>v;
           indegree[u]++;
           indegree[v]++;
           edges.push_back({u,v});
       }
       
       int maxnode = -1 ; int maxvalue = -1 ;
       
       for(int i =1 ; i<= n ; i++){
           if(maxvalue < indegree[i]){
               maxvalue = indegree[i];
               maxnode = i ;
           }
       }
       
       int node1 = maxnode ;
       
       for(auto v : graph[maxnode]){
           indegree[v]--;
       }
       
       maxnode = -1 ; maxvalue = - 1;
       
       for(int i = 1 ; i<= n ; i++){
           
           if(i == node1)continue ;
           if(maxvalue < indegree[i]){
               maxvalue = indegree[i];
               maxnode = i ;
           }
       }
       
       int node2 = maxnode ;
       
       for(auto y : edges){
           
           if(y.first == node1 || y.second == node1){
               continue ;
           }
           
           if(y.first == node2 || y.second == node2){
               continue ;
           }
           
           graph[y.first].push_back(y.second);
           graph[y.second].push_back(y.first);
       }
       
    //   cout<<node1<<" "<<node2<<endl;
       
       int cc = 0 ;
       vector<int>vis(n+1,0);
       
      for(int i = 1 ; i <= n ; i++){
          if(i == node1 || i == node2){
              continue ;
          }
          if(vis[i] == 0){
              dfs(i,i,vis,graph);
              cc++;
          }
      }
       
       cout<<cc<<endl;
       
       
    }
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

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

void dfs(int node, int pp , vector<int>&vis, vector<int>graph[]){
    
    vis[node] = 1;
    for(auto ch : graph[node]){
        if(ch == pp)continue ;
        if(!vis[ch]){
            dfs(ch,node,vis,graph);
        }
    }
}

signed main(){
    int t;
    cin>>t;
    
    while(t--){
        
       int n ; cin>>n ;
       
     
       vector<int>indegree(n+1,0);
       
       priority_queue<int>pq;
       for(int i = 1 ; i <= (n-1); i++){
           int u,v ;
           cin>>u>>v;
           indegree[u]++;
           indegree[v]++;
       }
       
       for(int i =1 ; i<=n ; i++){
           pq.push(indegree[i]);
       }
       
       if(n == 2){
           cout<<0<<endl;
       }
       else{
           int a = pq.top();pq.pop();
           int b = pq.top();pq.pop();
           int ans = a+b-1 ;
           if(indegree[b] == 1){
               ans--;
           }
           cout<<ans<<endl;
       }
       
       
    }
}


Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define int long long

void dfs(int node, int pp , vector<int>&vis, vector<int>graph[]){
    
    vis[node] = 1;
    for(auto ch : graph[node]){
        if(ch == pp)continue ;
        if(!vis[ch]){
            dfs(ch,node,vis,graph);
        }
    }
}

signed main(){
    int t;
    cin>>t;
    
    while(t--){
        
       int n ; cin>>n ;
       
     
       vector<int>indegree(n+1,0);
       
       priority_queue<int>pq;
       for(int i = 1 ; i <= (n-1); i++){
           int u,v ;
           cin>>u>>v;
           indegree[u]++;
           indegree[v]++;
       }
       
       for(int i =1 ; i<=n ; i++){
           pq.push(indegree[i]);
       }
       
       if(n == 2){
           cout<<0<<endl;
       }
       else{
           int a = pq.top();pq.pop();
           int b = pq.top();pq.pop();
        //   cout<<a<<" "<<b<<endl;
           int ans = a+b-1 ;
           if(b == 1){
               ans--;
           }
           cout<<ans<<endl;
       }
       
       
    }
}
Problem: 2063B - Subsequence Update
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define int long long

signed main(){
    int t;
    cin>>t;
    while(t--){
       int n,l,r ;
       cin>>n>>l>>r;
       priority_queue<int>pq;
       vector<int>arr(n,0);
       for(int i = 0 ; i<n ;i++){
           cin>>arr[i];
       }
       
       int sum1 = 0 ;
       for(int i = (l-1) ; i<= (r-1) ; i++){
           pq.push(arr[i]);
           sum1 += arr[i];
       }
       int sum2 = sum1 ;
       
       vector<int>right ;
       vector<int>left ;
       for(int i = r ; i< n ; i++){
           right.push_back(arr[i]);
       }
       
       for(int i = 0 ; i< l ; i++){
           left.push_back(arr[i]);
       }
       
       sort(left.begin(),left.end());
       sort(right.begin(),right.end());
       
       
       vector<int>temp ;
       int r_i = 0 ;
       while(!pq.empty()){
           int y = pq.top();
           if(r_i < right.size() && y > right[r_i] ){
               pq.pop();
               sum1 -= y ;
               sum1 += right[r_i];
               r_i++;
               temp.push_back(y);
           }
           else{
               break;
           }
       }
       
       for(auto x : temp){
           pq.push(x);
       }
       
       int l_i = 0 ;
       while(!pq.empty()){
           int y = pq.top();
           if(l_i < left.size() && y > left[l_i] ){
               pq.pop();
               sum2 -= y ;
               sum2 += left[l_i];
               l_i++;
              
           }
           else{
               break;
           }
       }
       
       int ans = min(sum1,sum2);
       
       cout<<ans<<endl;
       
       
    }
}
Problem: 2050D - Digital string maximization
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 8):
#include<bits/stdc++.h>
using namespace std;
#include<algorithm>
//#define int long long
char a[200008];
void slove(){
	cin>>a;
	int n=strlen(a);
	for(int j=1;j<=n;j++){
		int p=1;
		for(int i=n-1;i>=j-1;i--){
				if(a[i]==0||i==0)continue;
				if(a[i]-a[i-1]>1&&a[i-1]!=0&&i>=1){
					p=0;
					a[i]-=1;
					char b=a[i-1];
					a[i-1]=a[i];
					a[i]=b;
				}
			}
			if(p)break;
	}
	
	for(int i=0;i<=n-1;i++)cout<<a[i];
	cout<<'\n';
}

int main(){

  int t;
  cin>>t;
//ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//取消同步流

  while(t--){
  	slove();
  }
    
}

Submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
#include<algorithm>
//#define int long long
char a[200008];

void slove(){
     cin>>a;
	int n=strlen(a);
	for(int i=0;i<n;i++){
		int Max=a[i],Maxi=i;
		for(int j=i;j<min(i+9,n);j++){
			if(a[j]-(j-i)>Max){
				Max=a[j]-(j-i);
				Maxi=j;
			}
		}
		char b=Max;
		a[Maxi]=a[i];
		a[i]=Max;
	}
	
	for(int i=0;i<=n-1;i++)cout<<a[i];
	cout<<"#"<<'\n';
}

int main(){

  int t;
  cin>>t;
//ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//取消同步流

  while(t--){
  	slove();
  }

}
Problem: 2050D - Digital string maximization
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
#include<algorithm>
//#define int long long
char a[200008];

void slove(){
     cin>>a;
	int n=strlen(a);
	for(int i=0;i<n;i++){
		int Max=a[i],Maxi=i;
		for(int j=i;j<min(i+9,n);j++){
			if(a[j]-(j-i)>Max){
				Max=a[j]-(j-i);
				Maxi=j;
			}
		}
		char b=Max;
		a[Maxi]=a[i];
		a[i]=Max;
	}
	
	for(int i=0;i<=n-1;i++)cout<<a[i];
	cout<<"#"<<'\n';
}

int main(){

  int t;
  cin>>t;
//ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//取消同步流

  while(t--){
  	slove();
  }

}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
#include<algorithm>
//#define int long long
char a[200008];

void slove(){
     cin>>a;
	int n=strlen(a);
	for(int i=0;i<n;i++){
		int Max=a[i],Maxi=i;
		for(int j=i;j<min(i+9,n);j++){
			if(a[j]-(j-i)>Max){
				Max=a[j]-(j-i);
				Maxi=j;
			}
		}
        while(Maxi>i){
        	a[Maxi]-=1;
			swap(a[Maxi],a[Maxi-1]);
			Maxi--;
		}
	}
	
	for(int i=0;i<=n-1;i++)cout<<a[i];
	cout<<'\n';
}

int main(){

  int t;
  cin>>t;
//ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//取消同步流

  while(t--){
  	slove();
  }

}
Problem: 2050D - Digital string maximization
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
#include<algorithm>
//#define int long long
char a[200008];
void slove(){
	cin>>a;
	int n=strlen(a);
	for(int j=1;j<=n;j++){
		int p=1;
		for(int i=n-1;i>=j-1;i--){
				if(a[i]==0||i==0)continue;
				if(a[i]-a[i-1]>1&&a[i-1]!=0&&i>=1){
					p=0;
					a[i]-=1;
					char b=a[i-1];
					a[i-1]=a[i];
					a[i]=b;
				}
			}
			if(p)break;
	}
	
	for(int i=0;i<=n-1;i++)cout<<a[i];
	cout<<'\n';
}

int main(){

  int t;
  cin>>t;
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//取消同步流

  while(t--){
  	slove();
  }
    
}

Submission (verdict: Time limit exceeded on test 8):
#include<bits/stdc++.h>
using namespace std;
#include<algorithm>
//#define int long long
char a[200008];
void slove(){
	cin>>a;
	int n=strlen(a);
	for(int j=1;j<=n;j++){
		int p=1;
		for(int i=n-1;i>=j-1;i--){
				if(a[i]==0||i==0)continue;
				if(a[i]-a[i-1]>1&&a[i-1]!=0&&i>=1){
					p=0;
					a[i]-=1;
					char b=a[i-1];
					a[i-1]=a[i];
					a[i]=b;
				}
			}
			if(p)break;
	}
	
	for(int i=0;i<=n-1;i++)cout<<a[i];
	cout<<'\n';
}

int main(){

  int t;
  cin>>t;
//ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//取消同步流

  while(t--){
  	slove();
  }
    
}
Problem: 2050D - Digital string maximization
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
#include<algorithm>
//#define int long long
char a[200008];
void slove(){
	cin>>a;
	int n=strlen(a);
	for(int j=1;j<=n;j++){
		int p=1;
		for(int i=n-1;i>=j-1;i--){
				if(a[i]==0||i==0)continue;
				if(a[i]-a[i-1]>1&&a[i-1]!=0&&i>=1){
					p=0;
					a[i]-=1;
					char b=a[i-1];
					a[i-1]=a[i];
					a[i]=b;
				}
			}
			if(p)break;
	}
	
	for(int i=0;i<=n-1;i++)cout<<a[i];
	cout<<'\n';
}

int main(){

  int t;
  cin>>t;
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//取消同步流

  while(t--){
  	slove();
  }
    
}
Problem: 2063B - Subsequence Update
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+7;

int q[N];

void slove(){
	int n,l,r;  cin>>n>>l>>r;
//	vector<int>q(n+5);
	
	for(int i=1;i<=n;i++)
	cin>>q[i];
	sort(q+1,q+1+n);
	
	int ans=0,num=r-l+1;
	for(int i=1;i<=num;i++)
	ans+=q[i];
	
    cout<<ans<<'\n';
}

signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t=1; 
	cin>>t;
   
	while(t--){
		slove();
	}  
  
}

Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+7;

int q[N],ql[N],qr[N];

void slove(){
	int n,l,r;  cin>>n>>l>>r;
//	vector<int>q(n+5);
	
	for(int i=1;i<=n;i++){
	cin>>q[i];
	ql[i]=q[i];
	qr[i]=q[i];	
	}

	
	sort(ql,ql+1+r);
	sort(qr+l,qr+1+n);
		
	int ans=0,num=r-l+1;
	int cntl=0,cntr=0;
	for(int i=1;i<=num;i++){
		cntl+=ql[i];
		cntr+=qr[i];
	}
	ans=min(cntl,cntr);
	
//	for(int i=1;i<=n;i++)
    cout<<ans<<'\n';
}

signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t=1; 
	cin>>t;
   
	while(t--){
		slove();
	}  
  
}
Problem: 2063B - Subsequence Update
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+7;

int q[N];

void slove(){
	int n,l,r;  cin>>n>>l>>r;
//	vector<int>q(n+5);
	
	for(int i=1;i<=n;i++)
	cin>>q[i];
	sort(q+1,q+1+n);
	
	int ans=0,num=r-l+1;
	for(int i=1;i<=num;i++)
	ans+=q[i];
	
    cout<<ans<<'\n';
}

signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t=1; 
	cin>>t;
   
	while(t--){
		slove();
	}  
  
}
Problem: 2063B - Subsequence Update
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+7;

int q[N],ql[N],qr[N];

void slove(){
	int n,l,r;  cin>>n>>l>>r;
//	vector<int>q(n+5);
	
	for(int i=1;i<=n;i++){
	cin>>q[i];
	ql[i]=q[i];
	qr[i]=q[i];	
	}

	
	sort(ql,ql+1+r);
	sort(qr+l,qr+1+n);
		
	int ans=0,num=r-l+1;
	int cntl=0,cntr=0;
	for(int i=1;i<=num;i++){
		cntl+=ql[i];
		cntr+=qr[i];
	}
	ans=min(cntl,cntr);
	
//	for(int i=1;i<=n;i++)
    cout<<ans<<'\n';
}

signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t=1; 
	cin>>t;
   
	while(t--){
		slove();
	}  
  
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>

using namespace std;

#define int long long

const int N=1e5+7;



int q[N],ql[N],qr[N];



void slove(){

	int n,l,r;  cin>>n>>l>>r;

//	vector<int>q(n+5);

	

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

	cin>>q[i];

	ql[i]=q[i];

	qr[i]=q[i];	

	}



	

	sort(ql,ql+r+1);

	sort(qr+l,qr+n+1);

		

	int ans=0,num=r-l+1;

	int cntl=0,cntr=0;

	for(int i=1;i<=num;i++)

		cntl+=ql[i];

	

	for(int i=l;i<=r;i++)

	cntr+=qr[i];

	

	ans=min(cntl,cntr);

	

//	for(int i=1;i<=n;i++)

    cout<<ans<<'\n';

}



signed main(){

	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

    int t=1; 

	cin>>t;

   

	while(t--){

		slove();

	}  

  

}
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

using namespace std;



void solve() {

int n;

cin >> n;

vector<int> arr(n);

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

cin >> arr[i];

}

if(n < 3){

if(arr[0] < arr[1]){

cout << "YES" << '\n';

return;

}

else{

cout << "NO" << '\n';

return;

}

}

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

if(arr[i] > arr[i + 1] && arr[i + 1] > arr[i + 2]){

cout << "NO" << '\n';

return;

}

}

cout << "YES" << '\n';

}



int main() {

ios::sync_with_stdio(false);

cin.tie(nullptr);



int t;

cin >> t;



while (t--) {

solve();

}



return 0;

}

Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

using namespace std;



void solve() {

int n;

cin >> n;

vector<int> arr(n);

for (int &x : arr) cin >> x;



int j = 0;





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

if (arr[i] > arr[i + 1]) {

j = i;

}

}





if (j == 0) {

cout << "YES\n";

return;

}



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

int min_val = min(arr[i], arr[i + 1]);

arr[i] -= min_val;

arr[i + 1] -= min_val;

}



cout << (is_sorted(arr.begin(), arr.end()) ? "YES" : "NO") << '\n';

}



int main() {

ios::sync_with_stdio(false);

cin.tie(nullptr);



int t;

cin >> t;

while (t--) solve();



return 0;

}
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

using namespace std;



void solve() {

int n;

cin >> n;

vector<int> arr(n);

for (int &x : arr) cin >> x;



int j = 0;





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

if (arr[i] > arr[i + 1]) {

j = i;

}

}





if (j == 0) {

cout << "YES\n";

return;

}



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

int min_val = min(arr[i], arr[i + 1]);

arr[i] -= min_val;

arr[i + 1] -= min_val;

}



cout << (is_sorted(arr.begin(), arr.end()) ? "YES" : "NO") << '\n';

}



int main() {

ios::sync_with_stdio(false);

cin.tie(nullptr);



int t;

cin >> t;

while (t--) solve();



return 0;

}



Submission (verdict: Accepted):
#include <bits/stdc++.h>

using namespace std;



void solve() {

int n;

cin >> n;

vector<int> arr(n);

for (int &x : arr) cin >> x;

int j = -1;

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

if (arr[i] > arr[i + 1]) {

j = i;

}

}



if (j == -1) {

cout << "YES\n";

return;

}



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

int min_val = min(arr[i], arr[i + 1]);

arr[i] -= min_val;

arr[i + 1] -= min_val;

}



if (is_sorted(arr.begin(), arr.end())) {

cout << "YES\n";

} else {

cout << "NO\n";

}

}



int main() {

ios::sync_with_stdio(false);

cin.tie(nullptr);



int t;

cin >> t;

while (t--) solve();



return 0;

}
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

using namespace std;



void solve() {

int n;

cin >> n;

vector<int> arr(n);

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

cin >> arr[i];

}

if(n < 3){

if(arr[0] < arr[1]){

cout << "YES" << '\n';

return;

}

else{

cout << "NO" << '\n';

return;

}

}

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

if(arr[i] > arr[i + 1] && arr[i + 1] > arr[i + 2]){

cout << "NO" << '\n';

return;

}

}

cout << "YES" << '\n';

}



int main() {

ios::sync_with_stdio(false);

cin.tie(nullptr);



int t;

cin >> t;



while (t--) {

solve();

}



return 0;

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


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,l,r;
        cin>>n>>l>>r;
        vector<int>a(n);
        for(auto &x:a)
            cin>>x;
        long long sum=0;
        for(int i=l-1;i<=r-1;i++)
        {
            sum+=a[i];
        }
        if(r<n && l>1)
        {
            sort(a.begin()+l-1,a.begin()+r-1);
            sort(a.begin(),a.begin()+l-2);
            sort(a.begin()+r,a.end());
            long long lsum=sum,rsum=sum;
            int xl=r,yl=r-1;
            while(a[xl]<a[yl] && xl<n && yl>=0)
            {
                lsum=lsum-a[yl]+a[xl];
                xl++;
                yl--;
            }
            int xr=0,yr=r-1;
            while(a[xr]<a[yr] && xr<l-1 && yr>=l-1)
            {
                rsum=rsum-a[yr]+a[xr];
                xr++;
                yr--;
            }
            sum=min(lsum,rsum);
            cout<<sum;
        }
        else if(l==1 && r<n)
        {
            sort(a.begin()+l-1,a.begin()+r-1);
            sort(a.begin()+r,a.end());
            int x=r,y=r-1;
            while(a[x]<a[y] && x<n && y>=0)
            {
                sum=sum-a[y]+a[x];
                x++;
                y--;
            }
            cout<<sum;
        }
        else if(r==n && l>1)
        {
            sort(a.begin()+l-1,a.begin()+r-1);
            sort(a.begin(),a.begin()+l-2);
            // for(auto &o:a)
            //     cout<<o<<" ";
            // cout<<"\n";
            // cout<<sum;
            int x=0,y=r-1;
            while(a[x]<a[y] && x<l-1 &&y >=l-1)
            {
                sum=sum-a[y]+a[x];
                x++;
                y--;
            }
            cout<<sum;
        }
        else
        {
            cout<<sum;
        }
        cout<<"\n";
    }
}