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 P = 1000;
const int M = 10 * P;
const int N = 250;
const long double inf = 1e18;
int a[N];
long double lg[M];
long double ls[M];
long double pois(int k, int p) { return k == 0 ? -p : (k * lg[p] - p - ls[k]); }
long double unif(int k, int p) { return k > 2 * p ? -inf : -lg[2 * p + 1]; }
long double sum_pois(int p) {
  long double res = 0;
  for (int i = 0; i < N; ++i) res += pois(a[i], p);
  return res;
}
long double sum_unif(int p) {
  long double res = 0;
  for (int i = 0; i < N; ++i) res += unif(a[i], p);
  return res;
}
int main() {
  cin.sync_with_stdio(0);
  cout.sync_with_stdio(0);
  ls[1] = lg[1] = 0;
  for (int i = 2; i < M; ++i) {
    lg[i] = logl(i);
    ls[i] = ls[i - 1] + lg[i];
  }
  int t;
  cin >> t;
  while (t--) {
    bool pois = false;
    for (int i = 0; i < N; ++i) {
      cin >> a[i];
    }
    long double p_unif = -inf, p_pois = -inf, tmp;
    int pp = 10, pu = 10;
    for (int p = 10; p <= 1000; ++p) {
      tmp = sum_pois(p);
      if (p_pois < tmp) {
        p_pois = tmp;
        pp = p;
      }
      tmp = sum_unif(p);
      if (p_unif < tmp) {
        p_unif = tmp;
        pu = p;
      }
    }
    if (p_pois > p_unif)
      cout << pp << '\n';
    else
      cout << pu << '\n';
  }
  return 0;
}