#P10381. 「HOI R1」杂赛选比

    ID: 10790 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP线段树并查集2024O2优化洛谷比赛

「HOI R1」杂赛选比

Background

You are right, but when Little \iiint was playing CF, he mistook Earn or Unlock as the weird parody below, wasted 2 hours, and had to leave with regret. Hope everyone can take this as a warning.

Problem Description

Given an array aa of length nn, initially only a1a_1 is unlocked. Now there is an integer ii with initial value 11. Little \iiint plays a game on this array:

  • If aia_i is not unlocked, the game ends.
  • Otherwise, he can either set ai+1i+aia_{i+1\sim i+a_i} to unlocked, or gain aia_i coins (if ai=0a_i=0 then he cannot unlock any element), and then increase ii by 11.

Please find the maximum number of coins you can obtain after the game ends.

Input Format

This problem has multiple test cases.

The first line contains an integer TT, the number of test cases.

For each test case, the first line contains a positive integer nn.

The next line contains nn non-negative integers a1na_{1\sim n}.

Output Format

For each test case, output one number per line, representing the answer.

3
2
1 2
5
2 4 5 0 1
4
0 4 4 4

2
9
0

1
10
1 1 4 5 1 4 1 9 1 9
26

Hint

Sample 1 Explanation

For the first test case, you can unlock a2a_2, then gain a2a_2 coins. For the third test case, you cannot unlock a2a_2, so you can only gain 00 coins.

For the second test case, you can unlock a2,a3a_2,a_3 and gain 99 coins.

Sample 2 Explanation

Using positions 1,2,3,61,2,3,6 for unlocking is the optimal strategy.

Constraints

For 100%100\% of the data, 1n1051\le n\le10^5, 0ai1050\le a_i\le10^5, T5T\le 5.

Test Point ID nn\leq aia_i\leq T=T= Special Property
11 1010 00 11 /
232\sim3 55
454\sim5 600600
686\sim8 50005000
9109\sim10 10510^5 55 55
111211\sim12 5×1045\times10^4 10510^5 ai>na_i>n
132013\sim20 10510^5 /

Translated by ChatGPT 5