How can we solve this by using dp?
https://codeforces.com/problemset/problem/1661/B
CodeChef Starters 131 Solution Discussion