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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int a[200200];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    ll t;
    cin >> t;
    while(t--) {
        ll n;
        cin >> n;

        for(int i = 0; i < n; i++) {
            cin >> a[i];
        }

        ll p = 0;
        ll q = 1e6;
        ll ansi = -1;
        while(p < q) {
            ll i = (p + q)/2;
            ll to_left = 0;
            for(int j = 0; j < n; j++) {
                ll k;
                for(k = 0; k < 1000; k++) {
                    ll games = 0;

                    if(k > 0 && j != 0 && j != n - 1) {
                        games += k*to_left*(i - k - to_left);
                    }

                    if(k > 1) {
                        games += k*(k - 1)/2*(i - k);
                    }

                    if(k > 2) {
                        games += k*(k - 1)*(k - 2)/6;
                    }

                    if(games >= a[j]) {
                        break;
                    }
                }

                if(k == 999) {
                    to_left = i + 1;
                    break;
                }

                to_left += k;
            }

            if(to_left <= i) {
                ansi = i;
                q = i;
            } else {
                p = i + 1;
            }
        }

        cout << ansi << "\n";
    }

    return 0;
}