1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

i64 binom(i64 n, int k) {
    if (k == 2) return n*(n-1) / 2;
    if (k == 3) return n*(n-1)*(n-2) / 6;
    assert(false);
}

void solve() {
    int n; cin >> n;
    vector<i64> a(n);
    for (i64 &x: a) cin >> x;

    auto check = [&](int m) {
        i64 left = 0, rem = m;
        for (int i = 0; i < n; i++) {
            auto games = [&](i64 here) {
                return binom(here, 3) +
                    left * binom(here, 2) +
                    binom(here, 2) * (rem-here) +
                    left * here * (rem-here);
            };
            if (games(rem) < a[i]) return false;
            int here = 0;
            while (games(here) < a[i]) here++;
            rem -= here;
            left += here;
        }
        return true;
    };

    int lo = -1, hi = 2e5 + 10;
    assert(check(hi));
    while (hi-lo > 1) {
        int md = (lo+hi)/2;
        (check(md) ? hi : lo) = md;
    }
    cout << hi << '\n';
}

int main() {
    int tc; cin >> tc;
    while (tc--) solve();
}