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
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 200'005;
int arr[N],arr2[N],pref[N];
int arr3[N];
ll choose[N][4];
int n;

bool satisfies(int W){
    // cout << "satisfies(" << W << ")" << endl;
    int cnt_left = 0;

    auto cnt_matches = [&](const ll k, int &_cnt_left, int &_W)->ll {
        if(k > 2000) return LONG_LONG_MAX;
        return choose[k][3] + choose[k][2]*(_W-k) + _cnt_left*k*(_W-k-_cnt_left); 
    };

    for(int index=0; index<n; ++index){
        const int maxi = W-cnt_left-(n-1-index);
        // cout << " index:" << index << " maxi:" << maxi << endl;
        // cout << " cnt:" << cnt_matches(maxi, cnt_left, W) << "\n";
        // cout << " W:" << W << " cnt_left:" << cnt_left  << "\n";
        // cout << " choose[maxi][2]:" << choose[maxi][2] << "\n";
        // cout << " choose[maxi][3]:" << choose[maxi][3] << "\n";

        // Jeśli nie starczy ludzi na mecze to się nie da        
        if(cnt_matches(maxi, cnt_left, W) < arr[index]){
            // cout << " index:" << index << endl;
            // cout << " maxi:" << maxi << " cnt:" << cnt_matches(maxi, cnt_left, W) << " cnt_left:" << cnt_left << " arr:" << arr[index] << endl;
            return false;
        }
        if(index == n-1){
            arr2[index] = maxi;
            break;
        }
        
        int x_infl = 2*cnt_left;
        // Ternany search 1. [1, min(x_infl, maxi)], szukamy max na wypukłej f. kwadratowej
        int L = 1;
        int R = min(max(x_infl, 1), maxi);
        int mid_1,mid_2,mid;
        int best_i=1;
        ll best_val=cnt_matches(1, cnt_left, W);
        while(R-L > 10){
            mid_1 = L+(R-L)/3;
            mid_2 = R-(R-L)/3;
            if(cnt_matches(mid_1, cnt_left, W) < cnt_matches(mid_2, cnt_left, W)){
                L = mid_1;
            }else{
                R = mid_2;
            }
        }
        for(int i=L; i<=R; ++i){
            if(cnt_matches(i, cnt_left, W) > best_val){
                best_i = i;
                best_val = cnt_matches(i, cnt_left, W);
            }
        }
        const int x_crit_1 = best_i;
        // cout << " x_crit_1:" << x_crit_1 << endl;
        // Ternany search 2. [x_infl, maxi], szukamy min na wklęsłej f. kwadratowej
        L = min(max(x_infl, 1), maxi);
        R = maxi;
        best_i=maxi, best_val=cnt_matches(maxi, cnt_left, W);
        while(R-L > 10){
            mid_1 = L+(R-L)/3;
            mid_2 = R-(R-L)/3;
            if(cnt_matches(mid_1, cnt_left, W) < cnt_matches(mid_2, cnt_left, W)){
                R = mid_2;
            }else{
                L = mid_1;
            }
        }
        for(int i=L; i<=R; ++i){
            if(cnt_matches(i, cnt_left, W) < best_val){
                best_i = i;
                best_val = cnt_matches(i, cnt_left, W);
            }
        }
        const int x_crit_2 = best_i; 
        // cout << " x_crit_2:" << x_crit_2 << endl;
        best_i = 1e9;
        // Binsearch 1. [1, x_crit_1]
        L = 1;
        R = x_crit_1;
        while(R-L > 5){
            mid = (L+R)/2;
            if(cnt_matches(mid, cnt_left, W) >= arr[index]){
                best_i = min(best_i, mid);
                R = mid-1;
            }else{
                L = mid+1;
            }
        }
        for(int i=max(L-1,1); i<=min(R+1,maxi); ++i){
            if(cnt_matches(i, cnt_left, W) >= arr[index]){
                best_i = min(best_i, i);
            }
        }
        // Binsearch 2. [x_crit_1, x_crit_2]
        L = x_crit_1;
        R = x_crit_2;
        while(R-L > 5){
            mid = (L+R)/2;
            if(cnt_matches(mid, cnt_left, W) >= arr[index]){
                L = mid+1;
                best_i = min(best_i, mid);
            }else{
                R = mid-1;
            }
        }
        for(int i=max(L-1,1); i<=min(R+1,maxi); ++i){
            if(cnt_matches(i, cnt_left, W) >= arr[index]){
                best_i = min(best_i, i);
            }
        }
        // Binsearch 3. [x_crit_2, maxi]
        L = x_crit_2;
        R = maxi;
        while(R-L > 5){
            mid = (L+R)/2;
            if(cnt_matches(mid, cnt_left, W) >= arr[index]){
                best_i = min(best_i, mid);
                R = mid-1;
            }else{
                L = mid+1;
            }
        }
        for(int i=max(L-1,1); i<=min(R+1,maxi); ++i){
            if(cnt_matches(i, cnt_left, W) >= arr[index]){
                best_i = min(best_i, i);
            }
        }

        // cout << " val:" << best_i << endl;
        arr2[index] = best_i;
        cnt_left += best_i;
    }

    // Nic się nie stało strasznego po drodze, jest ok
    for(int i=0; i<n; ++i)
        arr3[i] = arr2[i];
    return true;
}

void solve(){
    cin >> n;
    {
        vector<int> tmp;
        for(int i=0,a; i<n; ++i){
            cin >> a;
            if(a>0) tmp.push_back(a);
        }
        for(int i=0; i<tmp.size(); ++i)
            arr[i] = tmp[i];
        n = tmp.size();
    }
    int L = n, R = 2000LL*n, mid, result=-1;
    // satisfies(38);
    while(L<=R){
        mid = (L+R)/2;
        // cout << " L:" << L << " R:" << R << endl;
        if(satisfies(mid)){
            result = mid;
            R = mid-1;
        }else{
            L = mid+1;
        }
    }

    cout << result << "\n";
    // cout << " [";
    // for(int i=0; i<n; ++i)
    //     cout << arr3[i] << (i==n-1 ? "]\n" : " ");
}   

int main(){
    cin.tie(0)->sync_with_stdio(0);
    for(int n=0; n<N; ++n){
        for(int k=0; k<=3; ++k){
            if(k>n) choose[n][k] = 0;
            else if(k==n || k==0) choose[n][k] = 1;
            else choose[n][k] = choose[n-1][k] + choose[n-1][k-1];
        }
    }
    int t;
    cin >> t;
    while(t--) solve();
}