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
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const ll MAX_PLAYERS = 1000000000;

ll binomial_2(ll n)
{
    return (n * (n - 1)) / 2;
}

ll binomial_3(ll n)
{
    return (n * (n - 1) * (n - 2)) / 6;
}

ll min_players_at_idx(ll expected_games, ll cnt_before, ll players_cnt)
{
    ll cnt_after;
    ll cnt_idx;
    
    ll left = 1, right = players_cnt + 1;
    while (left < right)
    {
        cnt_idx = (left + right) / 2;
        cnt_after = players_cnt - cnt_idx;
        
        bool ok = false;
        ll games = cnt_before * cnt_after * cnt_idx;
        if (cnt_idx >= 2 && (cnt_before + cnt_after) > 0)
        {
            // Zabezpieczenie przed overflowem
            if (cnt_idx >= 2000)
            {
                ok = true;
            }
            else
            {
                games += binomial_2(cnt_idx) * (cnt_before + cnt_after);
            }
        }
        if (cnt_idx >= 3)
        {
            // Zabezpieczenie przed overflowem
            if (cnt_idx >= 200)
            {
                ok = true;
            }
            else
            {
                games += binomial_3(cnt_idx);
            }
        }
        
        if (ok || games >= expected_games)
        {
            right = cnt_idx;
        }
        else
        {
            left = cnt_idx + 1;
        }
    }
    
    if (left >= players_cnt)
    {
        return -1;
    }
    return left;
}

bool possible(const vector<ll>& a, ll players_cnt)
{
    ll cnt_before = 0;
    for (int i = 0; i < (int) a.size(); ++i)
    {
        ll min_players = min_players_at_idx(a[i], cnt_before, players_cnt);
        if (min_players == -1)
        {
            return false;
        }
        
        cnt_before += min_players;
        players_cnt -= min_players;
    }
    
    return true;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t;
    cin >> t;
    
    while (t--)
    {
        int buildings_cnt, i;
        cin >> buildings_cnt;
        
        vector<ll> a;
        for (i = 0; i < buildings_cnt; ++i)
        {
            ll x;
            cin >> x;
            
            if (x > 0)
            {
                a.push_back(x);
            }
        }
        
        ll left = 3, right = MAX_PLAYERS + 1;
        while (left < right)
        {
            ll players_cnt = (left + right) / 2;
            
            if (possible(a, players_cnt))
            {
                right = players_cnt;
            }
            else
            {
                left = players_cnt + 1;
            }
        }
        
        assert(left < MAX_PLAYERS);
        cout << left << "\n";
    }

    return 0;
}