Codeforces Round #693 (Div. 3) C. Long Jumps

Polycarp found under the Christmas tree an array a of n elements and instructions for playing with it:

At first, choose index i (1≤i≤n) — starting position in the array. Put the chip at the index i (on the value ai).
While i≤n, add ai to your score and move the chip ai positions to the right (i.e. replace i with i+ai).
If i>n, then Polycarp ends the game.
For example, if n=5 and a=[7,3,1,2,3], then the following game options are possible:

Polycarp chooses i=1. Game process: i=1⟶+78. The score of the game is: a1=7.
Polycarp chooses i=2. Game process: i=2⟶+35⟶+38. The score of the game is: a2+a5=6.
Polycarp chooses i=3. Game process: i=3⟶+14⟶+26. The score of the game is: a3+a4=3.
Polycarp chooses i=4. Game process: i=4⟶+26. The score of the game is: a4=2.
Polycarp chooses i=5. Game process: i=5⟶+38. The score of the game is: a5=3.
Help Polycarp to find out the maximum score he can get if he chooses the starting index in an optimal way.

Input
The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The first line of each test case contains one integer n (1≤n≤2⋅105) — the length of the array a.

The next line contains n integers a1,a2,…,an (1≤ai≤109) — elements of the array a.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output
For each test case, output on a separate line one number — the maximum score that Polycarp can get by playing the game on the corresponding array according to the instruction from the statement. Note that Polycarp chooses any starting position from 1 to n in such a way as to maximize his result.

Example
inputCopy
4
5
7 3 1 2 3
3
2 1 4
6
2 1000 2 3 995 1
5
1 1 1 1 1
outputCopy
7
6
1000
5
Note
The first test case is explained in the statement.

In the second test case, the maximum score can be achieved by choosing i=1.

In the third test case, the maximum score can be achieved by choosing i=2.

In the fourth test case, the maximum score can be achieved by choosing i=1.
将数组倒着处理,看当前数组位置上的数能加几次大于n

#include<map>
#include<stack>
#include<queue>
#include<string>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define ls (k<<1)
#define rs (k<<1|1)
#define pb push_back
#define mid ((l+r)>>1)
#include<bits/stdc++.h>
using namespace std;
const int p=1e4+7;
const int mod=1e9+7;
const int maxn=1e6+1;
typedef long long ll;
const int inf=0x3f3f3f3f;
int a[maxn],b[maxn];
void solve(){
    
    
    int t;
    cin>>t;
    while(t--)
    {
    
    
        int n;
        cin>>n;
        for(int i=1;i<=2*n;i++)
            a[i]=0;
        for(int i=1;i<=n;i++){
    
    
            cin>>a[i];
        }
        int maxx=0;
        for(int i=n;i>=1;i--){
    
    
            if(a[i]+i<=n)
                a[i]=a[a[i]+i]+a[i];
            maxx=max(a[i],maxx);
        }
        cout<<maxx<<endl;
    }
}
 
signed main()
{
    
    
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	solve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45891413/article/details/112297900