So I made two submissions to this problem.
While this solution gets WA at test case 39, this onegets Accepted. While the only difference between them is in the order of calculating a1 and a2. Can anyone help me with the reason as why does the order make a difference? Thanks
I'm not quite sure, but I believe actually both solutions are incorrect. Because, the memo should take into consideration the "flag" state as well. Answers can differ depending if it's 1 or 0. I just modified that and I got AC : http://mirror.codeforces.com/contest/577/submission/12962910.