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;
const int maxn = 252;
const int inf = 0x3f3f3f3f;
const double eps = 1e-2;
int a[maxn];
int main() {
  int n;
  while (~scanf("%d", &n)) {
    for (int i = 0; i < n; i++) {
      int sum = 0;
      int mmax = -inf;
      for (int k = 0; k < 250; k++) {
        scanf("%d", &a[k]);
        mmax = max(mmax, a[k]);
        sum += a[k];
      }
      double ave = sum * 1.0 / 250;
      double s = 0;
      for (int k = 0; k < 250; k++) {
        s += (a[k] - ave) * (a[k] - ave);
      }
      s /= 250.0;
      double p = (s - ave) / ave;
      double t = ave * ave / 3;
      double u = (s - t) / t;
      p = abs(p);
      u = abs(u);
      if (p < u) {
        printf("%d\n", (int)ave);
      } else {
        double res = 251.0 / 250.0 * mmax - 1;
        res /= 2;
        printf("%d\n", (int)res);
      }
    }
  }
}