change to dynamic programming coping with the state of A[i]
passed test cases