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 N = 250;
int a[N + 5], ans;
double fac[20005];
double POI(int P) {
  double ans = 0;
  double v = log(P);
  for (int i = (int)(1); i <= (int)(N); i++)
    if (a[i] < 0)
      return -1e300;
    else
      ans += v * a[i] - P - fac[a[i]];
  return ans;
}
double UNI(int P) {
  double ans = 0;
  double v = log(2 * P + 1);
  for (int i = (int)(1); i <= (int)(N); i++)
    if (a[i] < 0 || a[i] > 2 * P)
      return -1e300;
    else
      ans -= v;
  return ans;
}
void solve() {
  double pos = -1e300;
  for (int i = (int)(1); i <= (int)(N); i++) scanf("%d", &a[i]);
  for (int i = (int)(1); i <= (int)(1000); i++) {
    double v1 = POI(i), v2 = UNI(i);
    if (v1 > pos) pos = v1, ans = i;
    if (v2 > pos) pos = v2, ans = i;
  }
  printf("%d\n", ans);
}
int main() {
  for (int i = (int)(1); i <= (int)(10000); i++) fac[i] = fac[i - 1] + log(i);
  int T;
  scanf("%d", &T);
  while (T--) solve();
}