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;
template <typename T>
using pq_gt = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using pq_lt = priority_queue<T, vector<T>, less<T>>;
int cnt[100001];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    memset(cnt, 0, sizeof(cnt));
    int n = 250;
    bool pois = 0;
    long long int sum = 0;
    int maxi = 0;
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      maxi = max(maxi, x);
      sum += x;
      if (x > 2000) {
        pois = 1;
      } else {
        cnt[x]++;
      }
    }
    sum = round((double)sum / (double)n);
    maxi *= 1.005;
    long long int s1 = 0;
    long long int s2 = 0;
    int dev = sqrt(sum);
    for (int i = max(sum - dev, 0LL); i <= sum + dev; i++) {
      s1 += cnt[i];
    }
    s2 = long long int(2 * dev + 1) * long long int(n) /
             (long double)(2 * sum + 1) +
         20;
    if (s1 > s2) {
      pois = 1;
    } else {
      pois = 0;
    }
    if (pois)
      cout << sum << endl;
    else
      cout << maxi / 2 << endl;
  }
}