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 = 2001;
int X[N];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int mx = 0;
    memset(X, 0, sizeof(X));
    vector<int> v;
    bool poisson = false;
    for (int i = 0; i < 250; i++) {
      int x;
      scanf("%d", &x);
      mx = max(mx, x);
      if (x < N) X[x]++;
      v.push_back(x);
    }
    if (mx > 2000) {
      poisson = true;
    }
    int d = mx;
    for (int i = 0; i < mx; i++) {
      int cnt = 0;
      for (int j = i; j < mx; j++) {
        cnt += X[j];
        if (cnt >= 125) {
          d = min(d, j - i + 1);
          break;
        }
      }
    }
    if (100 * d < 33 * mx) {
      poisson = true;
    }
    if (poisson) {
      sort(v.begin(), v.end());
      printf("%d\n", v[124]);
    } else {
      printf("%d\n", mx / 2);
    }
  }
  return 0;
}