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 = 4 * 100 * 1000;
int tab[MAXN + 3];
int t;
int main() {
  scanf("%d", &t);
  while (t--) {
    int n = 250;
    double avg = 0.0, var = 0.0;
    bool poisson = false;
    for (int i = 0; i < n; i++) {
      scanf("%d", &tab[i]);
      if (tab[i] > 2000) poisson = true;
      avg += tab[i];
    }
    avg /= 250.0;
    for (int i = 0; i < n && !poisson; i++) {
      var += ((double)tab[i] - avg) * ((double)tab[i] - avg);
    }
    var /= 250.0;
    if (fabs(var - avg) < fabs(var - (avg * avg + avg) / 3.0)) poisson = true;
    if (poisson)
      printf("%d\n", (int)(avg + 0.5));
    else {
      int x_min = 60000, x_max = 0;
      for (int i = 0; i < n; i++)
        x_min = min(x_min, tab[i]), x_max = max(x_max, tab[i]);
      printf("%d\n", (int)((x_min + x_max) / 2.0 + 0.5));
    }
  }
  return 0;
}