in this problem i want to solve it using left to right iteration so i m making an array of dp[n][2] which stores maximum score where [2] is for it comes whether it comes by choosing or not choosing that element
please give me your way to approach it using left to right please
lli n;
cin>>n;
lli a[n];
scanarr(a,n);
lli dp[n];
lli maxi = -1;
for(lli i=n-1;i>=0;i--)
{
dp[i] = a[i];
if((i+a[i])<n)
dp[i] += dp[(i+a[i])];
maxi = max(maxi , dp[i]);
}
print(maxi);
1 Like