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
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <deque>
#include <tuple>
#include <bitset>

using namespace std;

#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define x first
#define y second

typedef long long ll;
typedef pair<ll, ll> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vi;
typedef vector<ll> vll;

vll A;
ll n;

ll house_value(ll L, ll R, ll x, bool last=false){
    ll s = 0;
    
    if(last){
        R=0;
    }

    // Wez 1
    s += L*R*x;

    // Wez 2
    s += (L+R)*x*(x-1)/2;

    // Wez 3
    s += x*(x-1)*(x-2)/6;

    return s;
}


ll binary_house(ll L, ll P, ll a, bool last=false){
    ll l = 0;
    ll r = min(200ll, P-L);
    ll ans = -1;
    while (l <= r) {
        ll m = l + (r - l) / 2;
        ll score = house_value(L, (P-L-m), m, last);

        if(score < 0){
            cout << score << "\n";
        }
        if(score >= a){
            ans = m;
            r = m-1;
        }else{
            l = m+1;
        }
    }

    return ans;
}

ll binary_all(ll l, ll r){
    ll ans = -1;

    while (l <= r) {
        ll m = l + (r - l) / 2;

        ll L=0;
        bool f = true;

        for(ll i=0; i<n; i++){
            ll cur_house = binary_house(L, m, A[i], i==n-1);

            if(cur_house == -1){
                f=false;
                break;
            }

            L+=cur_house;
            if(L>m){
                f=false;
                break;
            }
        }

        if(f){
            ans = m;
            r = m-1;
        }else{
            l = m+1;
        }
    }

    return ans;
}


int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    ll t;
    cin >> t;

    while(t--){
        cin >> n;
        A.clear();

        for(ll i=0; i<n; i++){
            ll a;
            cin >> a;
            A.pb(a);
        }
        cout << binary_all(0, 500000) << "\n";
    }

    return 0;
}