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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <bits/stdc++.h>
using namespace std;

typedef int64_t ll;
typedef uint64_t ull;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
constexpr ll LINF = 1e18;
constexpr int INF = 1e9;

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

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

ll find_sum_bound(ll sum) {
    ll n_players = pow(6 * sum, 1. / 3.);
    while (binom_3(n_players) < sum) {
        ++n_players;
    }
    return n_players;
}

ll get_max_games(ll here, ll before, ll after) {
    return binom_3(here) + binom_2(here) * (before + after) + here * before * after;
}

int binsearch_bound(ll n_games, int before, int after) {
    int low = 1, high = 183;
    while (low < high) {
        int mid = (low + high) / 2;
        ll max_games = get_max_games(mid, before, after);

        if (max_games < n_games) {
            low = mid + 1;
        } else if (max_games > n_games) {
            high = mid;
        } else {
            return mid;
        }
    }

    return low;
}

int find_min_players(ll n_games, int before, int sum) {
    for (int here = 1; here <= 183; ++here) {
        int after = sum - before - here;

        if (after < 0) {
            return 184;
        }

        ll max_games = get_max_games(here, before, after);
        if (max_games >= n_games) {
            return here;
        }
    }

    return 184;
}

struct Elem {
    int i;
    int n_before;
    int n_after;
};

bool is_sum_possible(const vector<Elem> &to_compute, int sum, const vi &games, bool print = false) {
    int before = 0;
    vi n_players(to_compute.size());

    for (size_t j = 0; j < to_compute.size() - 1; ++j) {
        const Elem &e = to_compute[j];
        int i = e.i;
        int b = before + e.n_before;
        int here = find_min_players(games[i], b, sum);
        n_players[j] = here;

        // cout << i << " " << here << " " << b << " " << before << endl;
        if (sum - b - here < e.n_after) {
            return false;
        }

        before += here - 1 - (i == 1);
    }

    // Last element
    Elem last = to_compute.back();
    int here = min(sum - before - last.n_before, 184);
    ll n_games = get_max_games(here, before + last.n_before, last.n_after);
    n_players.back() = here;

    if (n_games < games[last.i]) {
        return false;
    }

    // if (print) {
    //     for (size_t i = 0; i < n_players.size(); ++i) {
    //         cout << n_players[i] << ' ';
    //     }
    //     cout << endl;
    // }

    return true;
}

int find_houses_bound(const vi &a, int sum_bound) {
    int n = a.size();
    vi b(a.size());
    vi b_sums(a.size());

    if (n == 2) {
        return sum_bound;
    }

    for (int i = 1; i < n; ++i) {
        b[i] = i == 1 ? 2 : 1;
        b_sums[i] = b_sums[i - 1] + b[i];
    }
    ++b[n - 1];
    ++b_sums[n - 1];

    vector<Elem> to_compute;
    for (int i = 1; i < n; ++i) {
        int before = b_sums[i - 1];
        int after = b_sums[n - 1] - b_sums[i];
        ll max_games = get_max_games(b[i], before, after);

        if (max_games < a[i]) {
            to_compute.push_back({i, before, after});
        }
    }

    if (to_compute.size() == 0)  {
        return max(sum_bound, b_sums[n - 1]);
    }

    int low = max(sum_bound, n + 2), high = 183 * n;
    while (low < high) {
        int mid = (low + high) / 2;
        bool is_possible = is_sum_possible(to_compute, mid, a);

        // cerr << mid << " " << is_possible << endl;

        if (is_possible) {
            high = mid;
        } else {
            low = mid + 1;
        }
    }

    // is_sum_possible(to_compute, high, a, true);

    return high;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

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

        vi a({0});
        ll sum = 0;
        for (int i = 1; i <= n; ++i) {
            int a_i;
            cin >> a_i;
            if (a_i) {
                sum += a_i;
                a.push_back(a_i);
            }
        }

        ll sum_bound = find_sum_bound(sum);
        ll houses_bound = find_houses_bound(a, sum_bound);

        // cout << "Sum bound: " << sum_bound << endl;
        // cout << "Houses bound: " << houses_bound << endl;

        cout << max(sum_bound, houses_bound) << '\n';
    }

    return 0;
}