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
/*
 * Opis: Główny nagłówek
 */
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r)for(int i=(l);i<=(r);++i)
#define REP(i,n)FOR(i,0,(n)-1)
#define ssize(x)int(x.size())
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

constexpr int W = 1e6 + 5;
constexpr int inf = 1e9;
constexpr int R = 2005;

int all_triples(int a) {
	return (int) min((LL)inf, LL(a) * (a - 1) * (a - 2) / 6);
}

int all_doubles(int a) {
	return (int) min((LL)inf, LL(a) * (a - 1) / 2);
}

void solve() {
	int n;
	cin >> n;
	vector<int> arr;
	REP(i, n) {
		int a;
		cin >> a;
		if (a != 0) {
			arr.push_back(a);
		}
	}
	n = ssize(arr);

	vector<int> initial(n);
	REP(i, n) {
		initial[i] = (int) min(LL(W), i * LL(n-i-1));
	}
	debug(initial);

	vector<int> at_most(n);
	REP(i, n) {
		while (all_triples(at_most[i] + 1) + initial[i] < arr[i]) {
			at_most[i]++;
		}
	}
	debug(at_most);

	vector<int> dp, temp; // for each sum count smalles requirement
	dp.push_back(0);
	REP(i, n) {
		if (initial[i] >= arr[i]) {
			continue;
		}
		temp.resize(ssize(dp) + at_most[i], inf);
		if (ssize(temp) > R) {
			temp.resize(R);
		}
		vector<int> interesting;
//		int start = 0;
		REP(j, ssize(dp)) {
			if (dp[j] + j > R) {
				continue;
			}
			if (j > 0 && dp[j] == dp[j-1]) {
				continue;
			}
			interesting.push_back(j);
		}
		if (i % 10 == 0) {
			debug(i, ssize(interesting));
		}
//		while (dp[start] > 2 * R) {
//			start++;
//		}
		REP(j, at_most[i] + 1) {
			int base = initial[i] + all_triples(j + 1);
			base = min(base, inf);
//			FOR(k, start, ssize(dp)-1) {
			for (int k : interesting) {
				if (k + j >= R) {
					continue;
				}
				int prv = k + i;
				int req = dp[k];
				LL tmp = base + (LL) all_doubles(j + 1) * prv;
				int curr = (int) min((LL) inf, (LL) tmp);
				int rest = max(0, arr[i] - curr);
					
//				debug("HEJO", i, j, k);
//				debug(prv, req, curr, rest);				

				tmp = all_doubles(j + 1)  + (LL) (j + 1) * prv;
				
				rest += (int) min((LL)inf, LL(i) * (n-i-1));

				int slope = (int) min((LL) inf, tmp);
				int on_right = inf;
				if (slope != 0) {
					on_right = (rest + slope - 1) / slope;
				}
				on_right = max(0, on_right - (n-i-1));
				int new_req = max(on_right, req - j);
				
//				if (new_req < temp[k + j]) {
//					debug("I DID IT!!!", new_req, k + j);
//				}
				temp[k + j] = min(temp[k + j], new_req);
			}
		}
		while (ssize(temp) > 1 && temp[ssize(temp)-2] == 0) {
			temp.pop_back();
		}

		swap(dp, temp);
		temp.clear();
//		debug("FINISHING", i);
//		debug(dp);
	}
	int res = inf;
	REP(i, ssize(dp)) {
		if (dp[i] == 0) {
			res = i;
			break;
		}
	}
	cout << res + n << "\n";
}

int main() {
	cin.tie(0)->sync_with_stdio(0);
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
}