This question is an exact duplicate of:
I have problem with solve the specific knapsack algorithm problem. Is there someone who give me some tips or help me? I solved it by Brute Force method but the execute time is very long (I checked all possible combinations and take the best solution - it works). I need to solve it by Dynamic Programming or Greedy Algorithm (but better by DP). I read about it a lot and I can't find the solution with it ;/ It is hard exercise. HERE IS description of my exercise
There are a few good tutorials on the internet that explain the Knapsack problem thoroughly.
More specifically, I would recommend this specific one, where the problem and the DP-approach is entirely explained, including the solution in three different languages (including Java).
Source: GeeksForGeeks