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
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>

using namespace std;

long long maxCombinations(long long playersNum) {
    return (playersNum * (playersNum - 1) * (playersNum - 2))/6;
}

long long gamesForIndex(long long players, long long index) {
    return (players - 2 - index) * (index + 1);
}

int main() {
    int cases;
    scanf("%d\n", &cases);

    for (int i = 0; i < cases; i++) {
        int buildNum;
        scanf("%d\n", &buildNum);
        
        vector<long long> games;

        long long sum = 0;
        for (int j = 0; j < buildNum; j++) {
            long long gamesNum;
            if (j == 0) {
                scanf("%lld", &gamesNum);
            } else {
                scanf(" %lld", &gamesNum);
            }

            if (gamesNum > 0) {
                games.push_back(gamesNum);
                sum += gamesNum;
            }
        }
        scanf("\n");

        long long playersBySum = 3;
        while (maxCombinations(playersBySum) < sum) {
            playersBySum++;
        }

        long long startPlayers = max(
            playersBySum, (long long) games.size()
        );

        long long currPlayers = startPlayers;
        bool found = false;
        while (!found) {
            int currBuilding = 0;
            long long currIx = 0;
            while (currBuilding < games.size() && currIx < currPlayers - 2) {
                long long currGames = 0;
                while (currIx < currPlayers - 2 && currGames < games[currBuilding]) {
                    currGames += gamesForIndex(currPlayers, currIx);
                    currIx++;
                }

                if (currGames >= games[currBuilding]) {
                    currBuilding++;
                }
            }

            if (currBuilding == games.size()) {
                found = true;
            } else {
                currPlayers++;
            }
        }

        printf("%lld\n", currPlayers);
    } 

    return 0;
}