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 t;
int vis[10001];
double Calc_Uniform(int l, int r) {
  memset(vis, 0, sizeof(vis));
  double sum = 0;
  for (int i = 1; i <= 250; i++) {
    int k = rand() % (r - l + 1) + l;
    vis[k]++;
    sum += k;
  }
  sum /= 250;
  double ans = 0;
  for (int i = l; i <= r; i++) ans += (sum - i) * (sum - i) * vis[i];
  return ans;
}
int main() {
  srand(2333);
  scanf("%d", &t);
  while (t--) {
    memset(vis, 0, sizeof(vis));
    int max0 = 0, min0 = 1e9;
    double xx = 0;
    for (int i = 1; i <= 250; i++) {
      int x;
      scanf("%d", &x);
      vis[x]++;
      max0 = max(max0, x);
      min0 = min(min0, x);
      xx += x;
    }
    xx /= 250;
    double ans1 = 0;
    for (int i = min0; i <= max0; i++) ans1 += (xx - i) * (xx - i) * vis[i];
    double ans2 = 0;
    for (int i = 1; i <= 500; i++) {
      double now = Calc_Uniform(min0, max0);
      ans2 += now;
    }
    ans2 /= 500;
    if (ans1 > ans2 * 0.7) {
      printf("%d\n", (int)(xx * 0.2 + max0 * 0.5 * 0.8 + 0.5));
      continue;
    }
    int kx = 1;
    for (int i = 2; i <= max0; i++)
      if (vis[i] > vis[kx]) kx = i;
    printf("%d\n", (int)(xx + 0.5));
  }
  return 0;
}