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;
string determine_type(vector<int> a) {
  double P = accumulate(a.begin(), a.end(), 0) / 250.;
  sort(a.begin(), a.end());
  vector<double> Fn(2000), Fp(2000), Fu(2000);
  int cur = 0;
  double supu = 0.0, supp = 0.0;
  for (int i = 0; i < 2000; ++i) {
    Fn[i] = i == 0 ? 0 : Fn[i - 1];
    while (cur < 250 && a[cur] < i) {
      Fn[i] += 1 / 250.;
      ++cur;
    }
    Fp[i] = (i == 0 ? 0 : Fp[i - 1]) + exp(i * log(P) - P - lgamma(i + 1));
    Fu[i] = (i == 0 ? 0 : Fu[i - 1]) + (i > 2 * P ? 0 : 1. / (2 * P + 1));
    supu = max(supu, abs(Fu[i] - Fn[i]));
    supp = max(supp, abs(Fp[i] - Fn[i]));
  }
  return supu < supp ? "uniform" : "poisson";
}
int determine_P(vector<int> a) {
  double P;
  if (determine_type(a) == "poisson") {
    P = accumulate(a.begin(), a.end(), 0) / 250.;
  } else {
    sort(a.begin(), a.end());
    P = (a.front() + a.back()) / 2.0;
  }
  return int(round(P));
}
void check() {
  uniform_int_distribution<> Pdistr(10, 1000);
  default_random_engine engine;
  vector<int> arg(250);
  int perror = 0, uerror = 0;
  int pPerror = 0, uPerror = 0;
  for (int test = 0; test < 10000; ++test) {
    int P = Pdistr(engine);
    poisson_distribution<> pdistr(P);
    for (int i = 0; i < 250; ++i) {
      arg[i] = pdistr(engine);
    }
    if (determine_type(arg) != "poisson") {
      ++perror;
    }
    if (abs(determine_P(arg) / double(P) - 1) > 0.05) {
      ++pPerror;
    }
    uniform_int_distribution<> udistr(0, 2 * P);
    for (int i = 0; i < 250; ++i) {
      arg[i] = udistr(engine);
    }
    if (determine_type(arg) != "uniform") {
      ++uerror;
    }
    if (abs(determine_P(arg) / double(P) - 1) > 0.05) {
      cout << "real=" << P << " "
           << " predicted=" << determine_P(arg) << endl;
      ++uPerror;
    }
  }
  cout << perror << " " << uerror << endl;
  cout << pPerror << " " << uPerror << endl;
}
int main() {
  int v;
  cin >> v;
  for (int test = 0; test < v; ++test) {
    vector<int> a(250);
    for (int i = 0; i < 250; ++i) {
      cin >> a[i];
    }
    cout << determine_P(a) << endl;
  }
}