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;
int nums[300];
int cnts[2200];
int main() {
  int V;
  scanf("%d", &V);
  for (int cas = 1; cas <= V; cas++) {
    priority_queue<int, vector<int>, less<int> > h1;
    priority_queue<int, vector<int>, greater<int> > h2;
    int mx = 0, mn = 1200, sum = 0;
    memset(cnts, 0, sizeof cnts);
    for (int j = 1; j <= 250; j++) {
      int t;
      scanf("%d", &t);
      h1.push(t);
      h2.push(t);
      if (h1.size() > 3) h1.pop();
      if (h2.size() > 3) h2.pop();
      mx = max(mx, t);
      mn = min(mn, t);
      nums[j] = t;
      cnts[t]++;
      sum += t;
    }
    int a = max(h1.top(), h2.top());
    int b = min(h1.top(), h2.top());
    double result = 0.0;
    if (a - b <= 1.7 * (mx + mn) / 2) {
      for (int i = mn; i <= mx; i++) cnts[i] += cnts[i - 1];
      int len = max(0.24 * (mx + mn) / 2, 4.0), mxpos = (a + b) >> 1, mxv = 0;
      for (int i = mn + len - 1; i <= mx; i++)
        if (cnts[i] - cnts[i - len] > mxv)
          mxv = cnts[i] - cnts[i - len], mxpos = i - len / 2;
      result = 0.2 * ((double)sum / 250.0) + 0.8 * mxpos;
    } else {
      result = 0.2 * ((double)sum / 250.0) + 0.8 * ((double)(mx + mn) / 2.0);
    }
    printf("%d\n", (int)floor(result + 0.5));
  }
}