Farmer John had just given the cows a program to play with! The program contains-variables, x and Y, and performs the following operations on a sequence a1,?a2,?...,? an of Positive integers:
The cows is not very good in arithmetic though, and they want to see how the program works. Please help them!
You are given the sequence a2,?a3,?...,? a. Suppose for each i (1?≤?i?≤?n?-? 1) We run the program on the sequence I,?A2,?A3, ?...,? an. For each such run output, the final value of Y if the program terminates or-1 if it does not terminate.
Input
The first line contains a single integer, N (2?≤?n?≤?2 105). The next line contains n?-? 1 space separated integers, a2,?a3,?...,? an (1?≤?ai?≤?109).
Output
Output n?-? 1 lines. On the i-th line, print the requested value and the program was run on the sequence i,?a2,?a3,?... an.
%lld specifier to read or write 64-bit integers inс++. It is preferred to use the CIN, cout streams or the%i64d specifier.
Sample Test (s)
Input
4
2 4 1
Output
3
6
8
Input
3
1 2
Output
-1
-1
Note
In the first sample
Notice that each X has 2 transfer directions, set DP[I][2], respectively to I+a[i] and I-a[i],
The final value of Y, and then the memory of the search on the line, pay attention to the ring
/************************************************************************* > File name:cf-174-d.cpp > Aut Hor:alex > Mail: [email protected] > Created time:2015 May 02 Saturday 21:07 26 Seconds ******************************* *****************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;Static Const intN =200110; LL dp[n][2]; LL Arr[n];intN LL DFS (intUintFlag) {if(U <=0|| U > N) {return 0; }if(Dp[u][flag]! =-inf) {returnDp[u][flag]; } Dp[u][flag] =-1;intSGN = flag?1: -1; LL ans = DFS (U + sgn * Arr[u], flag ^1);if(ans! =-1) {Dp[u][flag] = ans + arr[u]; }returnDp[u][flag];}intMain () { while(~scanf("%d", &n)) { for(inti =2; I <= N; ++i) {scanf("%i64d", &arr[i]); } for(inti =1; I <= N; ++i) {dp[i][0] = dp[i][1] =-inf;//init} for(inti =1; I <= N; ++i) { for(intj =0; J <2; ++J) {Dfs (i, j); } } for(inti =1; I <= N-1; ++i) {if(Dp[i +1][0] == -1) {printf("%i64d\n", Dp[i +1][0]); }Else{printf("%i64d\n", Dp[i +1][0] + i); } } }return 0;}
Codeforces Round #174 (Div. 2)---D. Cow program (DP, Memory Search)