#include <iostream>
#include <vector>
using namespace std;
int upperpow(int X){
	int pow=1;
	int c=0;
	while(pow<X){
		pow=pow*2;
		c++;
	}
	return c;
}
int pow(int X){
	
	int pow=1;
	for(int i=0; i<X; i++){
		pow=pow*2;
	}
	return pow;
}

long long bho(int N, vector<int>V){
	long long ans=0;
	int N0=pow(upperpow(N));
	int S[N0];
	for(int i=0;i<N; i++){
		S[i]=V[i];
	}	
	for(int i=N; i<N0; i++){
		S[i]=9999999;
	}
	int pow=1;
	
	while(pow<N0){
		//cout<<"pow: "<<pow<<endl;
	//	for(int i=0; i<N0; i++){
		//	cout<<S[i]<<" ";
	//	}
	//	cout<<endl;
		int X[pow*2];
		
		for(int c=0; c<N0/(2*pow); c++){
			int r=0;
			int l=0;
			int p=0;
			int L=c*2*pow;
			int R=L+pow;
		//	cout<<L<<" "<<R+pow-1<<":"<<endl;
			while(p<pow*2){
				if(l<pow&&r<pow){
				//	cout<<"caso 1"<<endl;
					if(S[L+l]<=S[R+r]){
						X[p]=S[L+l];
						l++;
						p++;
					}else{
						X[p]=S[R+r];
						r++;
						p++;
						ans=ans+pow-l;
					//	cout<<" qua!"<<endl;
					//	cout<<pow<<" "<<R<<" "<<r<<" "<<L<<" "<<l<<endl;
					}	
				}else{
				//	cout<<"caso 2"<<endl;
					if(l==pow){
						X[p]=S[R+r];
						r++;
						p++;
					}else{
						X[p]=S[L+l];
						l++;
						p++;
					}
				}
			//	cout<<l<<" "<<r<<" "<<p<<endl;
				
			}
		//	cout<<"risultato: "<<endl;
			for(int i=0; i<2*pow; i++){
				S[i+L]=X[i];
			//	cout<<X[i]<<" ";
			}
		//	cout<<endl;
			
			
			
		}
		/*for(int i=0; i<N0; i++){
			cout<<S[i]<<" ";
		}
		cout<<endl;*/
		pow=pow*2;
	}
	
	
	return ans;
}
