I am trying to solve this on spoj. I have first precomputed the index for both the case(appending number to beginning and appending number to end). Then, I used top down approach to find the minimum value. It works on most test cases but gives wrong answer on spoj. I am not able to figure out the reason. Please help. My Soloution
Your target is value of
f(a, 1, n, n, n)
. And approach reversed than in code, fordp[x][y]
you needf(v,min(x,y)+1,y,n,level-1)
andf(v,x,max(x,y)-1,n,level-1)
. Recursive base isif (x == y) return dx[x][x]
(ordy[x][x]
).tks for your advice