Imagine you're a professional baker with a beautiful m × n rectangular cake that needs to be cut into individual 1 × 1 pieces for serving. However, each cut comes with a cost!
The Challenge: You have two types of cuts available:
- Horizontal cuts: Cut along any horizontal line
iat costhorizontalCut[i] - Vertical cuts: Cut along any vertical line
jat costverticalCut[j]
Key insight: When you make a cut, it affects ALL pieces that cross that line. For example, if you've already made some vertical cuts creating multiple columns, a horizontal cut will cost the same but will cut through ALL those columns simultaneously!
Goal: Find the minimum total cost to cut the entire cake into 1 × 1 pieces.
Input: Dimensions m, n, and arrays horizontalCut (size m-1) and verticalCut (size n-1) representing cut costs.
Output: Minimum total cost as an integer.
Input & Output
Constraints
- 1 ≤ m, n ≤ 105
- horizontalCut.length == m - 1
- verticalCut.length == n - 1
- 1 ≤ horizontalCut[i], verticalCut[i] ≤ 106
- The answer may be large, consider using long long in C++