forked from tamimsal/Problem-Solving
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1674C
More file actions
59 lines (42 loc) · 936 Bytes
/
1674C
File metadata and controls
59 lines (42 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define debug(x) cout<<"[" << #x << " is:" << x << "] "<<endl;
int cal(int index, int n , vector<int>&dp ,vector<int>&v){
if( dp[index] != -1){
return dp[index];
}
if(index >= n){
return 0;
}
int ans =v[index];
if(v[index]+index < n)
ans = max(ans, v[index] +cal(v[index]+ index, n , dp ,v));
return dp[index] = ans;
}
void solve()
{
int n;
cin >> n;
vector<int>v(n);
for(int i =0 ; i< n ; i ++){
cin >> v[i];
}
vector<int>dp(n,-1);
int ans =0;
for(int i =0 ;i < n; i++){
ans = max(cal(i,n,dp,v),ans);
}
cout << ans << endl;
}
int32_t main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t = 1;
cin >> t;
while(t--){
solve();
}
return 0;
}