Giving Runtime Error , but i can't figure out why but it is working fine on my ide and sometimes it give bus error , so it is because no input was given or anything else?

I see your dp recurrence incorrect!


it contains both bottom up and top down approaches

Sir , May i have any test case on which my solution fails ??

To avoid run error, initialize dp and a as global arrays, you will see WA verdict,
you may download testcases to see what goes wrong.

But sir how initiallizing dp or array globally is related to runtime errors , and sir i try clicking the View TEST CASE option which is provided in there but it is not working . And not only for this question but for any question i have seen uptil now , if they actually show in other people then do let me know i will surely report to support coding blocks .

This is a common mistake and something most are unaware of.


see your code now

Sir , i tried by declaring it globally but still getting wrong answer !!

@aryan_007 that’s why I provided you my code so that you may see correct approach.

Sir ,is my approach correct ?? what i used is if i am on i th state then i check i+1 and i+2 th state and if i+1 th state has more value then i go there provided i-1 th state was not choosen else i go to i+2 th state .

So , sir is this approach correct or wrong and if wrong then why ???

this seems a greedy approach, hence it is not guaranteed that you get correct answer always

Yes , sir this is a greedy approach but is kind of same as that of kadane’s algorithm . So,why it should fail ??

5
1 2 3 40 70
answer should be 113, but your output is 74. as you know this, using DP(or trying out every possibility) is the only way to solve this problem correctly.

Okay sir , i will try again !!