#include "rotate.h"
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define speedIO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//#define mod 1000000007
#define f first
#define s second
#define pii pair<int,int>
#define pb push_back
const int M=50000;
vector<int> final;
void energy(int n, vector<int> v){
	vector<pii> w(n);
	for(int i=0;i<n;i++){
		w[i]={v[i],i};
	}
	sort(w.begin(),w.end());
	set<pair<pii,int>> a;
	for(int i=0;i<n;i++){
		a.insert({{w[i].f,i},w[i].s});
	}
	//will work with w, w[i].s is the og index.
	// a.f.f is value, a.f.s is the sorted index and a.s is the og index.
	int idx=0;
	vector<bool> marked(n,false);
	while(idx<n){
		if(marked[idx]){
			idx++;
			continue;
		}
		marked[idx]=true;
		if(w[idx].f<M/2){
			auto it=a.upper_bound({{w[idx].f+M/2,1e9},1e9});
			it--; // j st w[j]<=w[i]+M/2<w[j+1]
			auto element=*it;
			if(element.f.s-idx>=n/2){
				rotate({w[idx].s},w[element.f.s].f+M/2-w[idx].f);
				a.erase({{w[idx].f,idx},w[idx].s});
				w[idx].f=(w[element.f.s].f+M/2)%M;
				a.insert({{w[idx].f,idx},w[idx].s});
				marked[element.f.s]=true;
			}else{
				rotate({w[idx].s},w[(element.f.s+1)%n].f+M/2-w[idx].f);
				a.erase({{w[idx].f,idx},w[idx].s});
				w[idx].f=(w[(element.f.s+1)%n].f+M/2)%M;
				a.insert({{w[idx].f,idx},w[idx].s});
				marked[(element.f.s+1)%n]=true;
			}
		}else{
			auto it=a.lower_bound({{w[idx].f-M/2,0},0});
			//it--; // j st w[j-1]<w[i]+M/2<=w[j]
			auto element=*it;
			if(idx-element.f.s>=n/2){
				rotate({w[idx].s},w[element.f.s].f+M/2-w[idx].f);
				a.erase({{w[idx].f,idx},w[idx].s});
				w[idx].f=(w[element.f.s].f+M/2)%M;
				a.insert({{w[idx].f,idx},w[idx].s});
				marked[element.f.s]=true;
			}else{
				rotate({w[idx].s},w[(element.f.s-1)%n].f+M/2-w[idx].f);
				a.erase({{w[idx].f,idx},w[idx].s});
				w[idx].f=(w[(element.f.s-1)%n].f+M/2)%M;
				a.insert({{w[idx].f,idx},w[idx].s});
				marked[(element.f.s-1)%n]=true;
			}
		}
		idx++;
	
	}
	
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |