I managed to find a recursive solution which gives tle for last test case of second subtask and first test case of last. This is because I couldn't find a way to cache the computed value so there was repitition of calculation.

My process included finding all possible negatives in pairs and then taking triplets to find all cases that produce negative sum . I take the negatives only in a seperate array and all I now have to do is find all combinations in which no two negatives are adjacent .

Here is the link to my solution(20pts) : www.codechef.com/viewsolution/18569209

I do understand all I have to do is store the best possible sum from index i to n-1 for every index i and also the array traversal which has to be stored in a 2d array for every i in every row. I do not understand how to do it. Please help.

if(a[0] >= a[1]) dp1[0] = -a[0]; Shouldn't it be : if(a[0]< a[1]) dp1[0] = -a[0]; ?

rishup119:07, May 17Ritesh Guptahello, liman

first, the below test case give tle for your code.

DP SOLUTION:)I am going to explain you the top-down approach. we need two dp arrays like dp1 and dp2 in which we can store the best answer for a prefix of an array. in dp1[i], we store the best result of prefix up to index i if the element at index i is going to be negative in answer and in dp2[i], we store the best result of prefix up to index i if the element at index i is going to be positive in answer.

first, you have to initialize both the dp array with INF and then define a base condition and then write the recurrence relation. you also need two cnt arrays to store the sign so you can backtrack your solution.

ok, that's all you need for implementation of this question. I hope you will understand.

Please make sure the answer is not too short