Problem: Good job! Now that Heidi is able to distinguish between Poisson and uniform distributions, she is in a good position to actually estimate the populations.

Can you help Heidi estimate each village's population?

Input

Same as the easy version.

Output

Output one line per village, in the same order as provided in the input, containing your (integer) population estimate.

Your answer is considered correct if it is an integer that falls into the interval <image>, where P is the real population of the village, used to create the distribution (either Poisson or uniform) from which the marmots drew their answers.

Solution (in CPP):

Solution:
#include <iostream>
#include <map>
#include <algorithm>    
#include <vector>       
#include <set>
#include <utility>  
#include <cstring>
#include <stdio.h>
#include <queue>
#include <math.h>   
#include<conio.h>
#include <CMATH>
#include <iostream>
#include <cstdlib>
#include <ctime>



using namespace std;

struct orc{
	int num;
	int x;
	int y;
};

bool myfunction (orc i,orc j) { return (i.x<j.x); }

struct Comp1
{
	bool operator()(const orc& str1, const orc& str2)
	{
		return str1.y < str2.y;
	}
};

//while(r - l != 0)
//    {
//        int64 mid =  MID(r,l);
//        if(mid * (mid + 1) / 2 >= n) r = mid;
//        else l = mid + 1;
//    }



int gcd(int a, int b) { return a ? gcd(b%a, a) : b; }


double q[2001];

int main(){
	
	
	
	int t;
	cin>>t;
	while(t--){
		vector<int> k;
		
		 int iSecret, iGuess;

	  	/* initialize random seed: */
		  srand (time(NULL));

		
		for(int i =0; i<=2000; i++)
			q[i]=0;
		
		for(int i =0; i<250; i++){
			int a;
			cin>>a;
			k.push_back(a);
			
		}
		
		for(int i = 0; i<1000; i++){
			iSecret = rand() % 250  ;
			k.push_back(k[iSecret]);
		}
		
		for(int i=0; i<k.size(); i++){
			q[k[i]]++;
		}
		
		double a=0;
		double b=0;
		
		for(int  i =0; i<k.size(); i++){
			a += q[i] * i ;
			b += q[i] * i * i ; 
	 	}
	 	
	 	a=a/k.size();
	 	b=b/k.size();
	 	
	 	double e = a;
	 	double eq = b-a*a;
	 	int hghgh=0;
	 	double a1 = (eq - e*e/6);
	 	if(a1<0) a1=-a1;
		 
		double b1 = (eq-e);
		
		if(b1<0) b1= -b1;
		
		if(a1>b1) {
			cout<<round(a)<<"\n";
		}
		else {
			 cout << *max_element(k.begin(), k.end()) / 2 << "\n";
		}
	}
}