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 n, m, k, x, y, sum, cnt, ans, q, a[500], f[10000];
int main() {
  scanf("%d", &q);
  while (q--) {
    memset(f, 0, sizeof f);
    bool ok = false;
    cnt = 0;
    for (int i = 0; i < 250; i++) {
      scanf("%d", &a[i]);
      f[a[i]]++;
      if (a[i] < 50) cnt++;
    }
    sort(a, a + 250);
    int med = a[125];
    if (abs(f[med + 3] + f[med - 2] + f[med - 1] + f[med] + f[med + 1] +
            f[med + 2] + f[med + 3] + f[med + 4] - f[0] - f[1] - f[2] - f[3] -
            f[4] - f[5] - f[6] - f[7]) >= 20)
      ok = true;
    if (med < 100 && f[0] + f[1] > 3)
      ok = false;
    else if (med >= 100 && cnt == 0)
      ok = true;
    if (ok)
      printf("%d\n", med);
    else {
      sum = 0;
      for (int i = 0; i < 250; i++) {
        sum += a[i];
      }
      if (a[249] <= 5000)
        printf("%d\n", ans = a[249] / 2);
      else
        printf("%d\n", ans = (int)round(1.0 * sum / 251));
      int lo = (int)floor(0.95 * y), hi = (int)ceil(1.05 * y);
    }
  }
  return 0;
}