This is problem 2 from Ahmed Aly's Div-C ladder. I got the O(N^2)
solution easily but it did time out so I am thinking of some ways to go for the O(N)
solution. I believe it will involve dynamic programming similar to knapsack.
I just need some hints, is this correct?
Thanks