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 MOD = 1e9 + 7;
const int SIZE = 1e6 + 10;
int a[250];
bool is_poisson() {
  int sum = 0;
  for (int j = 0; j < (250); ++j) sum += a[j];
  double P = sum / 250.;
  double dd = 0;
  for (int j = 0; j < (250); ++j) {
    dd += (P - a[j]) * (P - a[j]);
  }
  dd /= 250.;
  if (dd < P * 2) return true;
  return false;
}
int main() {
  int(V);
  scanf("%d", &V);
  for (int i = 0; i < (V); ++i) {
    int sum = 0;
    for (int j = 0; j < (250); ++j) {
      scanf("%d", &(a[j]));
      sum += a[j];
    }
    double P = sum / 250.;
    if (is_poisson()) {
      sort(a, a + 250);
      sum = 0;
      int cnt = 0;
      double dd = 0;
      for (int j = 0; j < (250); ++j) {
        dd += (P - a[j]) * (P - a[j]);
      }
      dd /= 250.;
      for (int j = 0; j < (250); ++j) {
        if ((P - a[j]) <= dd && P - a[j] >= -dd) {
          sum += a[j];
          cnt++;
        }
      }
      P = sum / (double)cnt;
      printf("%.0f\n", P);
    } else {
      int x = *min_element(a, a + 250);
      int y = *max_element(a, a + 250);
      P = (x + y) / 2.;
      printf("%.0f\n", P);
    }
  }
  return 0;
}