题目链接
翻译
translation
题解
动规。
(f[i] = f[i+a[i]]+a[i]) 类似这样?...倒着推一下,注意边界就行。
代码
#include <bits/stdc++.h>
#define lson l,mid,rt*2
#define rson mid+1,r,rt*2+1
#define LL long long
using namespace std;
const int N = 2e5;
int n;
int a[N+10];
LL f[N+10];
int main(){
ios::sync_with_stdio(0),cin.tie(0);
int T;
cin >> T;
while (T--){
cin >> n;
for (int i = 1;i <= n; i++){
cin >> a[i];
}
LL ans = 0;
for (int i = n;i >= 1;i--){
int j = i+a[i];
if (j > n){
f[i] = a[i];
}else{
f[i] = a[i]+f[j];
}
ans = max(ans,f[i]);
}
cout << ans << endl;
}
return 0;
}