codeforce 339d dp O(m*n)