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>
const int n = 250;
const int maxn = 300;
int a[maxn];
double b[maxn];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    bool bs = true;
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    bool flag = false;
    for (int i = 0; i < n; i++) {
      if (a[i] > 2000) {
        flag = true;
        break;
      }
    }
    int sum = 0;
    double avg;
    if (!flag) {
      sum = 0;
      for (int i = 0; i < n; i++) sum += a[i];
      avg = (double)sum / n;
      double s2 = 0.0;
      for (int i = 0; i < n; i++) s2 += (a[i] - avg) * (a[i] - avg);
      s2 /= n;
      if (fabs(s2 - avg) > fabs(avg * (avg + 1) / 3 - s2)) bs = false;
    }
    if (bs)
      printf("%d\n", (int)avg);
    else {
      int ma = 0;
      for (int i = 0; i < n; i++)
        if (a[i] > ma) ma = a[i];
      printf("%d\n", ma / 2);
    }
  }
  return 0;
}