1472C - Long Jumps - CodeForces Solution


dp graphs *1100

Please click on ads to support us..

Python Code:

def solve():
    l = int(input())
    lst = list(map(int,input().strip().split()))[:l]

    dp = [0 for i in range(l)]
    for i in range(l - 1, -1, -1):
        dp[i] = lst[i]
        j = i + lst[i]
        if j < l:
            dp[i] += dp[j]

    return max(dp)

if __name__ == '__main__':
    n = int(input())

    for i in range(n):
        print(solve())

C++ Code:

#include<iostream>
using namespace std;
long long a[200010],n,maxn;
int main(){
  	int t;
  	cin>>t;
  	for(int j=0;j<t;j++){
  		maxn=0;
  		cin>>n;
  		for(int i=1;i<=n;i++)
		cin>>a[i];
  		for(int i=n;i>0;i--){
  		    if(i+a[i]<=n)
			a[i]+=a[i+a[i]];
  			if(a[i]>maxn) 
			maxn=a[i];
  		}
  	cout<<maxn<<endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

544B - Sea and Islands
152B - Steps
1174D - Ehab and the Expected XOR Problem
1511A - Review Site
1316A - Grade Allocation
838A - Binary Blocks
1515D - Phoenix and Socks
1624D - Palindromes Coloring
1552F - Telepanting
1692G - 2Sort
1191A - Tokitsukaze and Enhancement
903A - Hungry Student Problem
52B - Right Triangles
1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours
747A - Display Size
285A - Slightly Decreasing Permutations