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 a[250];
int cnt[2001];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int N = 250;
    for (int j = 0; j < N; j++) scanf("%d", a + j);
    double M = 0;
    for (int j = 0; j < N; j++) M += a[j];
    M /= N;
    double v = 0;
    for (int j = 0; j < N; j++) v += (a[j] - M) * (a[j] - M);
    v /= N;
    if (v < 3 * M) {
      printf("%d\n", (int)round(M));
    } else {
      printf("%d\n", (*max_element(a, a + N) + *min_element(a, a + N)) / 2);
    }
  }
}