CF283B.Cow Program

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Farmer John has just given the cows a program to play with! The program contains two integer variables, x and y, and performs the following operations on a sequence _a_1, _a_2, ..., a__n of positive integers:

  1. Initially, x = 1 and y = 0. If, after any step, x ≤ 0 or x > n, the program immediately terminates.
  2. The program increases both x and y by a value equal to a__x simultaneously.
  3. The program now increases y by a__x while decreasing x by a__x.
  4. The program executes steps 2 and 3 (first step 2, then step 3) repeatedly until it terminates (it may never terminate). So, the sequence of executed steps may start with: step 2, step 3, step 2, step 3, step 2 and so on.

The cows are not very good at arithmetic though, and they want to see how the program works. Please help them!

You are given the sequence _a_2, _a_3, ..., a__n. Suppose for each i (1 ≤ i ≤ n - 1) we run the program on the sequence i, _a_2, _a_3, ..., a__n. For each such run output the final value of y if the program terminates or -1 if it does not terminate.

约翰农民刚刚给奶牛们一个程序来玩!该程序包含两个整数变量 xx 和 yy,并对一个正整数序列 a1, a2, ..., ana_1, a_2, ..., a_n 执行如下操作:

  1. 初始时,x = 1x = 1 且 y = 0y = 0。若在任意步骤之后,x ≤ 0x \leq 0 或 x > nx > n,则程序立即终止。
  2. 程序同时将 xx 和 yy 均增加 axa_x。
  3. 程序将 yy 增加 axa_x,同时将 xx 减少 axa_x。
  4. 程序重复执行步骤 2 和步骤 3(先执行步骤 2,再执行步骤 3),直至终止(也可能永不终止)。因此,所执行的步骤序列可能以如下方式开始:步骤 2、步骤 3、步骤 2、步骤 3、步骤 2,依此类推。

然而,奶牛们的算术并不太好,她们想看看这个程序是如何运行的。请帮帮她们!

你将得到序列 a2, a3, ..., ana_2, a_3, ..., a_n。假设对每个 ii(其中 1 ≤ i ≤ n − 11 \leq i \leq n - 1),我们在序列 i, a2, a3, ..., ani, a_2, a_3, ..., a_n 上运行该程序。对每次这样的运行,请输出程序终止时 yy 的最终值;若程序不终止,则输出 −1-1。

输入格式

The first line contains a single integer, n (2 ≤ n ≤ 2·105). The next line contains n - 1 space separated integers, _a_2, _a_3, ..., a__n (1 ≤ a__i ≤ 109).

第一行包含一个整数 nn(2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5)。第二行包含 n−1n-1 个用空格分隔的整数 a2, a3, …, ana_2,\ a_3,\ \dots,\ a_n(1≤ai≤1091 \leq a_i \leq 10^9)。

输出格式

Output n - 1 lines. On the i-th line, print the requested value when the program is run on the sequence i, _a_2, _a_3, ...a__n.

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

输出 n−1n-1 行。在第 ii 行中,输出当程序在序列 i, a2, a3, …, ani,\,a_2,\,a_3,\,\dots,\,a_n 上运行时所要求的值。

请注意:在 C++ 中,请勿使用 %lld 说明符读取或写入 64 位整数。推荐使用 cin、cout 流,或 %I64d 说明符。

输入输出样例

  • 输入#1

    4
    2 4 1

    输出#1

    3
    6
    8
  • 输入#2

    3
    1 2

    输出#2

    -1
    -1

说明/提示

In the first sample

  1. For i = 1,  x becomes and y becomes 1 + 2 = 3.
  2. For i = 2,  x becomes and y becomes 2 + 4 = 6.
  3. For i = 3,  x becomes and y becomes 3 + 1 + 4 = 8.

在第一个样例中:

  1. 当 i=1i = 1 时,xx 变为 ,而 yy 变为 1+2=31 + 2 = 3。
  2. 当 i=2i = 2 时,xx 变为 ,而 yy 变为 2+4=62 + 4 = 6。
  3. 当 i=3i = 3 时,xx 变为 ,而 yy 变为 3+1+4=83 + 1 + 4 = 8。

输入解题思路,AI测评打分。不知道怎么写?

首页