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>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
template <class T, class U>
inline void Max(T &a, U b) {
  if (a < b) a = b;
}
template <class T, class U>
inline void Min(T &a, U b) {
  if (a > b) a = b;
}
inline void add(int &a, int b) {
  a += b;
  while (a >= 1000000007) a -= 1000000007;
}
int pow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = ans * (long long)a % 1000000007;
    a = (long long)a * a % 1000000007;
    b >>= 1;
  }
  return ans;
}
int a[300];
double logsum[10010], INF = 1e18, logg[10010];
double poisson_prob(int p, int k) {
  if (k < 0) return -INF;
  return -p + k * logg[p] - logsum[k];
}
double uniform_prob(int p, int k) {
  if (k < 0 || k > 2 * p) return -INF;
  return -logg[2 * p + 1];
}
double poisson(int p) {
  double ans = 0;
  for (int i = 0; i < 250; i++) {
    ans += poisson_prob(p, a[i]);
  }
  return ans;
}
double uniform(int p) {
  double ans = 0;
  for (int i = 0; i < 250; i++) {
    ans += uniform_prob(p, a[i]);
  }
  return ans;
}
double sqr(double first) { return first * first; }
int main() {
  int T, i = 0, j, k, ca = 0, m = 250, K, n;
  logsum[1] = 0;
  for (int i = 2; i < 10010; i++)
    logg[i] = log(i), logsum[i] = logsum[i - 1] + logg[i];
  scanf("%d", &T);
  while (T--) {
    for (int i = 0; i < m; i++) scanf("%d", &a[i]);
    double ans = -INF;
    int P;
    for (int p = 10; p < 1001; p++) {
      double a = poisson(p);
      if (ans < a) ans = a, K = 1, P = p;
      a = uniform(p);
      if (ans < a) ans = a, K = 0, P = p;
    }
    if (K) {
      double av = 0;
      for (int i = 0; i < m; i++) av += a[i];
      av /= m;
      printf("%d\n", (int)(av + 0.5));
    } else {
      P = *max_element(a, a + m);
      printf("%d\n", (P + 1) / 2);
    }
  }
  return 0;
}