-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathB.cpp
69 lines (56 loc) · 1.19 KB
/
B.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
59
60
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1000000000
ll ara[200009], n, dp[200009][2];
bool vis[200009][2];
ll dfs(ll u, bool state)
{
if(u <= 0 || u > n)
return 0;
if(u == 1) {
if(state == 1) return 0;
else return -1;
}
if(vis[u][state]) {
if(dp[u][state] == -inf)
return -1;
else
return dp[u][state];
}
vis[u][state] = 1;
ll ret = 0;
if(!state)
ret = dfs(u + ara[u], state ^ 1);
else
ret = dfs(u - ara[u], state ^ 1);
if(ret != -1)
dp[u][state] = ara[u] + ret;
else
dp[u][state] = -1;
return dp[u][state];
}
int main()
{
for(ll i = 0; i <= 200002; i++) {
for(ll j = 0; j < 2; j++)
dp[i][j] = -inf;
}
cin >> n;
for(ll i = 2; i <= n; i++)
scanf("%lld", &ara[i]);
for(ll i = 2; i <= n; i++) {
if(!vis[i][0])
dfs(i, 0);
if(!vis[i][1])
dfs(i, 1);
}
for(ll i = 1; i < n; i++) {
if(dp[i + 1][1] != -1) {
printf("%lld\n", dp[i + 1][1] + i);
}
else
printf("-1\n");
}
return 0;
}