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 = 250;
int a[maxn + 5], cnt[maxn * 10], mx, T, sum, flag, r, mn;
double S;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    mx = 0;
    flag = 0;
    mn = 2000;
    S = 0;
    memset(cnt, 0, sizeof cnt);
    for (int i = 1; i <= maxn; i++) {
      scanf("%d", &a[i]);
      mx = max(mx, a[i]);
      mn = min(mn, a[i]);
      if (mx > 2000) break;
      cnt[a[i]]++;
      S += a[i];
    }
    if (mx > 2000) {
      printf("%.0lf\n", S / maxn);
      continue;
    }
    r = 0;
    sum = cnt[0];
    for (int l = 0; l <= mx / 2; l++) {
      while (r < mx && r < l + mx / 2) sum += cnt[r++];
      if (sum > 0.7 * maxn) {
        flag = 1;
        break;
      }
      sum -= cnt[l];
    }
    if (flag == 1)
      printf("%.0lf\n", S / maxn);
    else
      printf("%.0lf\n", (mn + mx) / 2.0);
  }
  return 0;
}