[LeetCode] Minimum Cost to Acquire Required Items

3789. Minimum Cost to Acquire Required Items

You are given five integers cost1, cost2, costBoth, need1, and need2.

There are three types of items available:

  • An item of type 1 costs cost1 and contributes 1 unit to the type 1 requirement only.
  • An item of type 2 costs cost2 and contributes 1 unit to the type 2 requirement only.
  • An item of type 3 costs costBoth and contributes 1 unit to both type 1 and type 2 requirements.

You must collect enough items so that the total contribution toward type 1 is at least need1 and the total contribution toward type 2 is at least need2.

Return an integer representing the minimum possible total cost to achieve these requirements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
long long minimumCost(int cost1, int cost2, int costBoth, int need1, int need2) {
costBoth = min(costBoth, cost1 + cost2);
long long res = 1ll * min(need1, need2) * costBoth;
if(need1 > need2) {
res += 1ll * (need1 - need2) * min(cost1, costBoth);
}
if(need2 > need1) {
res += 1ll * (need2 - need1) * min(cost2, costBoth);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/minimum-cost-to-acquire-required-items/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.