how to solve manthan codefest17 problemB

problem link

What I did, was to keep a track of minimum from start upto index i, maximum from start upto index i, minimum from end upto index i, maximum from end upto index i;

Now I made cases, and accordingly used the formula (eg- if p<0 use p*min upto index i; if p>0, use p* max upto index i etc.)

For every case, you can check liek-

For i from 1 to N
   ans=max(ans, p*max_or_min till i from start + q*arr[i] + r *max or min till i from end);

But I believe making cases was redundant, but i thought better safe than sorry XD

please give the solution code

For sake of your own sanity, you shouldnt take a look at my code :3

the explaination is enough thank you

take four array
prefixmin[] (min upto i)
prefixmax[] (max upto i)
sufmin[] (min from n-1 to i)
sufmax[] (max from n-1 to i)

fix j form 0 to n-1

for i and k
check if p<0 multiply p with premin[i]
else multiply p with premax[i]

same for r but multiply with sufmin and sufmax

and check

for more check my code:

however my solution got hacked just for one line mx=-1e18 instead LLONG_MIN