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 a[260];
double logg[10010];
double logsum[10010];
double uniform_prob(int P, int k) {
  if (k >= 0 && k <= 2 * P) return -logg[2 * P + 1];
  return -1.0E+18;
}
double poisson_prob(int P, int k) {
  if (k < 0) return -1.0E+18;
  return logg[P] * k - P - logsum[k];
}
double get_uniform(int P) {
  int i;
  double ans = 0.0;
  for ((i) = 0; (i) < (int)(250); (i)++) ans += uniform_prob(P, a[i]);
  return ans;
}
double get_poisson(int P) {
  int i;
  double ans = 0.0;
  for ((i) = 0; (i) < (int)(250); (i)++) ans += poisson_prob(P, a[i]);
  return ans;
}
void main2(void) {
  char type = '?';
  int P = -1;
  double best = -1.0E+18;
  int i;
  for (i = 10; i <= 1000; i++) {
    double tmp = get_uniform(i);
    if (tmp > best) {
      best = tmp;
      type = 'u';
      P = i;
    }
    tmp = get_poisson(i);
    if (tmp > best) {
      best = tmp;
      type = 'p';
      P = i;
    }
  }
  cout << P << endl;
}
int main(void) {
  int TC, tc, i;
  for ((i) = 0; (i) < (int)(10000); (i)++) logg[i] = log(i);
  for ((i) = 0; (i) < (int)(10000); (i)++)
    logsum[i + 1] = logsum[i] + log(i + 1);
  cin >> TC;
  for ((tc) = 0; (tc) < (int)(TC); (tc)++) {
    for ((i) = 0; (i) < (int)(250); (i)++) scanf("%d", &a[i]);
    main2();
  }
  return 0;
}