0-1 knapsack (Book Shop)

Problem link: https://cses.fi/problemset/task/1158/

Code Link: https://ide.codingblocks.com/s/466251

Getting correct output locally but RTE(in some testcases) in Online Judge. How to improve this memoization dp code? Is memoization dp always slower than iterative?? (I have submitted iterative solution and it is passing all testcases.)