Java implementation of the approach
import java . util . * ; class GFG { static int MAX = 10000 ; static int MAX_ELEMENT = 50 ; static int [ ] [ ] [ ] dp = new int [ MAX_ELEMENT ] [ MAX_ELEMENT ] [ MAX ] ;