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 n = 250, M = 8000, N = 105;
int T, a[n + 5], m, mn, s[M + 5];
double d, avg;
int main() {
  cin >> T;
  while (T--) {
    memset(s, 0, sizeof(s));
    m = mn = d = avg = 0;
    for (int i = 1; i <= n; i++) {
      int x;
      scanf("%d", &x);
      avg += x;
      m = max(x, m);
      a[i] = x;
      mn = min(x, mn);
      s[x]++;
    }
    s[mn]--, s[m]--;
    avg /= n;
    for (int i = 1; i <= n; i++) d += (a[i] - avg) * (a[i] - avg);
    for (int i = 0; i <= M; i++)
      if (s[i]) m = max(m, i), mn = min(mn, i);
    if (d > 2 * n * avg)
      printf("%d\n", (m + mn + 1) / 2);
    else {
      printf("%d\n", (int)(avg + 0.5));
    }
  }
  return 0;
}