Instead of treating 7 copies as 7 separate items, represent 7 in binary: . Create three "bundle" items with weights and values . Any quantity from 0 to 7 can be formed by including/excluding these bundles. Want 5 copies? Take the bundles for 1 and 4. Want 3? Take 1 and 2. This reduces items to items. Now run standard 0/1 knapsack. Total time: .
Time complexity: .
Space complexity: for the dp array.