-
Notifications
You must be signed in to change notification settings - Fork 0
/
1491C.cpp
58 lines (50 loc) · 871 Bytes
/
1491C.cpp
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
#include <iostream>
#include <cstdio>
using namespace std;
const int MAX = 5010;
int n;
int a[MAX];
long long c[MAX];
long long res = 0 ;
int main(){
int t;
scanf("%d",&t);
while(--t>=0){
scanf("%d",&n);
for(int i = 0 ; i < n; ++i){
scanf("%d",a+i);
}
res = 0;
for(int i = 0 ; i < n; ++i){
c[i] = 0;
}
int i = -1;
while( i+1 < n && a[i+1] == 1){
++i;
}
i = i == -1 ? 0 : i;
for(;i < n;++i){
if(i == n-1){
res += a[i] - 1;
c[i] = max((long long)0,c[i] - a[i] + 1);
res += c[i];
break;
}
int out = n - i - 1;
if(a[i] > out){
int rem = a[i] - out;
res += rem;
a[i] = out;
c[i] = max((long long)0, c[i] - rem);
}
for(int j = a[i]; j > 1; --j){
c[i + j]++;
}
c[i] = max((long long)0,c[i] - a[i] + 1);
if(c[i]){
c[i+1] += c[i];
}
}
printf("%lld\n",res );
}
}