https://codeforces.com/problemset/problem/1336/C

can anyone explain how this is done using dp,i am not able to understand its editorial.

Maybe this might help?