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 <bits/stdc++.h>
using namespace std;
int freq[2500];
mt19937 mt(time(NULL));
void getAvg() {
  memset(freq, 0, sizeof freq);
  int sum = 0;
  int minim = 100000, maxim = -10;
  for (int i = 1; i <= 250; ++i) {
    int sample;
    cin >> sample;
    ++freq[sample];
    sum += sample;
    minim = min(minim, sample);
    maxim = max(maxim, sample);
  }
  int P = sum / 250;
  int dev = 0;
  for (int i = 0; i <= 2000; ++i) dev += freq[i] * (i - P) * (i - P);
  dev /= 250;
  int v1 = P;
  int v2 = P * P / 3;
  if (abs(v1 - dev) < abs(v2 - dev))
    cout << min(max(P, 10), 1000) << '\n';
  else {
    P = (maxim + minim) / 2;
    cout << P << '\n';
  }
}
int main() {
  srand(time(NULL));
  int T = 0;
  cin >> T;
  while (T--) getAvg();
  return 0;
}