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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <cmath>
#include <complex>
#include <climits>

using namespace std;

__int128 get_exact_val(__int128 const a, __int128 const x, __int128 const b) {
    return a*b*(x-a-b) + (x-b)*b*(b-1)/2 + b*(b-1)*(b-2)/6;
}

bool checkResult(__int128 const x, __int128 positiveMatches, vector<long long> const & matches) {
    __int128 a = 0;

    for(auto const & i : matches) {
        if(i == 0) {
            continue;
        }

        __int128 beg = 1;
        __int128 end = x-a;
        if(get_exact_val(a, x, end) < (__int128)i) {
            return false;
        }
        while(beg < end) {
            __int128 mid = (beg + end) / 2;
            if(get_exact_val(a, x, mid) >= i) {
                end = mid;
            } else {
                beg = mid + 1;
            }
        }
        a += beg;
    }

    return true;
}

void input(int & n, vector<long long> & matches) {
    cin >> n;
    matches.resize(n);

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

void solve() {
    int n;
    vector<long long> matches;
    input(n, matches);

    __int128 matchesSum = 0;
    __int128 positiveMatches = 0;
    for(int i = 0; i < n; i++) {
        matchesSum += matches[i];
        if(matches[i] > 0) {
            positiveMatches++;
        }
    }

    __int128 beg = 3;
    __int128 end = matchesSum;
    while(beg < end) {
        __int128 mid = (beg + end) / 2;
        if(checkResult(mid, positiveMatches, matches)) {
            end = mid;
        } else {
            beg = mid + 1;
        }
    }
    
    cout << (long long)beg << "\n";
}

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

    int t;
    cin >> t;
    while(t--) {
        solve();
    }

    return 0;
}