n items: item i is worth values[i] and carries tag tags[i]. Choose a set of items with the largest total worth while following two rules:
- the set has at most max_items items;
- at most per_tag items share the same tag.
Return the largest total worth of such a set.values=[7, 6, 5, 3], tags=[1, 1, 1, 2], max_items=3, per_tag=2 → 16
Tag 1 allows only two items: 7 and 6. The third pick is 3 from tag 2: 7 + 6 + 3 = 16.
values=[4, 9, 2], tags=[5, 5, 5], max_items=3, per_tag=1 → 9
All items share one tag and only one may be taken, so we take 9.
values=[0, 8, 1, 4], tags=[2, 3, 4, 3], max_items=2, per_tag=2 → 12
values=[7, 6, 5, 3], tags=[1, 1, 1, 2], max_items=3, per_tag=2
16
Run checks the first 3 tests in your browser. Submit checks all 102 tests on the server, hidden ones included.