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 INF = numeric_limits<int>::max();
const long long LLINF = numeric_limits<long long>::max();
const unsigned long long ULLINF = numeric_limits<unsigned long long>::max();
const double PI = acos(-1.0);
int n;
long long a[255];
double sqr(double a) { return a * a; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    double mean = 0;
    double var = 0;
    long long mx = 0, mn = LLINF;
    for (int j = 0; j < 250; j++) {
      cin >> a[j];
      mean += a[j];
      mx = max(mx, a[j]);
      mn = min(mn, a[j]);
    }
    mean /= 250.0;
    for (int j = 0; j < 250; j++) var += sqr(a[j] - mean);
    var /= 249.0;
    if (abs(mean - var) / min(mean, var) < 0.9) {
      long long ans = (long long)(mean + 0.5);
      if (ans <= 10)
        cout << 10 << '\n';
      else if (ans >= 1000)
        cout << 1000 << '\n';
      else
        cout << ans << '\n';
    } else {
      long long ans =
          max((long long)((mx + mn) / 2.0 + 0.5), (long long)(mx / 2.0 + 0.5));
      if (ans <= 10)
        cout << 10 << '\n';
      else if (ans >= 1000)
        cout << 1000 << '\n';
      else
        cout << ans << '\n';
    }
  }
  return 0;
}