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 = 1e4 + 233;
int a[maxn], cnt[maxn];
int main() {
  int T;
  cin >> T;
  while (T--) {
    memset(cnt, 0, sizeof(cnt));
    int n = 250, maxi = 0;
    double tot = 0, ave;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      maxi = max(maxi, a[i]);
      ave += a[i];
      cnt[a[i]]++;
    }
    ave = n;
    ave /= maxi;
    for (int i = 0; i <= maxi; i++) {
      double x = abs(cnt[i] * 1.0 - ave);
      tot += x * x;
    }
    tot /= maxi;
    tot /= ave;
    if (tot >= 2.5) {
      int sum = 0;
      for (int i = 1; i <= n; i++) sum += a[i];
      cout << int(sum / n + 0.5) << endl;
    } else {
      cout << int(maxi / 2 + 0.5) << endl;
    }
  }
  return 0;
}