Link to problem : http://codeforces.net/problemset/problem/543/A
DP solution with 501*501*501 memory (MLE but gives correct results when tested on PC) : http://ideone.com/edQhJr
DP solution with 2*501*501 memory ( dimension compressed) : gives WA on sample? : http://ideone.com/YNbwRO
Anyone has an idea why? Thanks.