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 double eps = 1e-10;
const int MOD = 1000000007;
int i, j, k, m, n, l;
long long ans, s1, s2;
int a[1000001], b[1000001];
long long d[2500][2500];
int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
void what() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  for (int i = 0; i < n + 2; i++)
    for (int j = 0; j < n + 2; j++) d[i][j] = 1e18;
  ans = 1e18;
  for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= k; j++) {
      if (j == 1) {
        d[i][j] = a[i] + b[i];
      }
      d[i][j] = min(d[i][j], d[i + 1][j - 1] + a[i] + b[i]);
      d[i][j] = min(d[i][j], d[i + 1][j] + a[i] - a[i + 1]);
      if (j == k) ans = min(ans, d[i][j]);
    }
  }
  cout << ans << endl;
}
vector<int> GetPois(int P) {
  vector<double> ps;
  double s = -P;
  for (int k = 0; k < 2000; k++) {
    if (k > 1) s -= log(1.0 * k);
    double p = exp(s);
    ps.push_back(p);
    s += log(1.0 * P);
  }
  vector<int> ret;
  for (int i = 0; i < 250; i++) {
    double dp = (rand() * 1.0 + 0.5) / (1LL * RAND_MAX + 1);
    for (int k = 0; k < 2000; k++) {
      dp -= ps[k];
      if (dp < 0) {
        ret.push_back(k);
        break;
      }
    }
  }
  return ret;
}
vector<int> GetUni(int P) {
  vector<int> ret;
  for (int i = 0; i < 250; i++) {
    ret.push_back(rand() % (2 * P + 1));
  }
  return ret;
}
double sumfuck[5000];
void prep() {
  sumfuck[0] = 0.0;
  for (int i = 1; i <= 2000; i++) sumfuck[i] = sumfuck[i - 1] + log(1.0 * i);
}
double Est(vector<int>& v, int P) {
  double ret = 0;
  double logp = log(P);
  for (int x : v) {
    if (x > 2000) return -1e10;
    ret += (-P - sumfuck[x]) + x * logp;
  }
  return ret;
}
void solve(vector<int> v) {
  double best = -1e50;
  int bestP = -1;
  for (int P = 10; P <= 1000; P++) {
    double p = Est(v, P);
    if (p > best) {
      best = p;
      bestP = P;
    }
  }
  int U = 0;
  for (int x : v) U = max(U, x);
  double P = (U + 1) / 2;
  double pp = 0.0;
  for (int x : v) pp += log(1.0 / (2 * P + 1));
  if (best > pp) {
    cout << bestP << endl;
  } else {
    cout << P << endl;
  }
}
int main() {
  prep();
  if (1) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      vector<int> v;
      for (int fuck = 0; fuck < 250; fuck++) {
        cin >> j;
        v.push_back(j);
      }
      solve(v);
    }
    return 0;
  }
  for (int P = 10; P <= 1000; P += 40) solve(GetPois(P));
  return 0;
}