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, typename U>
inline void chkmax(T& x, U y) {
  if (x < y) x = y;
}
template <typename T, typename U>
inline void chkmin(T& x, U y) {
  if (y < x) x = y;
}
int a[255];
int main() {
  int ncase;
  for (scanf("%d", &ncase); ncase--;) {
    double s2 = 0, s1 = 0;
    for (int i = 0, x; i < 250; i++) {
      scanf("%d", &a[i]);
      x = a[i];
      s1 += x, s2 += 1.0 * x * x;
    }
    s1 /= 250, s2 /= 250;
    s2 -= s1 * s1;
    int ans = 10;
    sort(a, a + 250);
    if (s2 > 2 * s1) {
      ans = (a[0] + a[249]) / 2;
    } else {
      double mind = 1e100;
      for (int pro = 10; pro <= 1000; pro++) {
        double dis = 0;
        for (int i = 0; i < 250; i++) dis += 1.0 * (a[i] - pro) * (a[i] - pro);
        dis /= 250;
        if (fabs(dis - pro) < mind) {
          mind = fabs(dis - pro);
          ans = pro;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}