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 a[250];
inline double sqr(double x) { return x * x; }
inline int rand1(int P) {
  double sum = 0;
  double x = (double)rand() / RAND_MAX;
  double frac = 1;
  for (int k = 0; k <= 2 * P; k++) {
    if (k) frac *= k;
    sum += (pow(P, k) * exp(-P)) / frac;
    if (x <= sum) return k;
  }
  return 2 * P;
}
inline int rand2(int P) {
  double x = (double)rand() / RAND_MAX;
  cout << P;
  return (int)floor(x * (2 * P + 1));
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    memset(a, 0, sizeof a);
    double sum = 0;
    int Mx = 0;
    for (int i = 0; i < 250; i++) {
      scanf("%d", a + i);
      sum += a[i];
      Mx = max(a[i], Mx);
    }
    sum /= (double)250;
    double fangcha = 0;
    for (int i = 0; i < 250; i++) fangcha += sqr(a[i] - sum);
    fangcha /= 250;
    if (fangcha < 2 * sum)
      cout << int(sum + 0.5) << endl;
    else
      cout << int((double)Mx / 2.0 + 0.5) << endl;
  }
  return 0;
}