#P4647. Minimizing Sail Discount on a Pirate Ship
Minimizing Sail Discount on a Pirate Ship
Minimizing Sail Discount on a Pirate Ship
You are given a pirate ship with N flagpoles. The i-th flagpole has a height of H_i (which means it is divided into H_i unit sections) and will hang exactly A_i sails. Each sail occupies one section and on each flagpole, no two sails can share the same section.
When the wind blows, the arrangement of sails affects the total discounted pushing force of the ship. For any sail placed on some flagpole, its pushing force is discounted by the number of sails at the same height on any flagpole behind it (i.e. with a larger index). In other words, if a sail is placed at a certain height, its discount equals the number of sails positioned at that same height on flagpoles behind it. The total discount is the sum of the discounts over all sails.
Your task is to assign positions (i.e. choose a section from 1 to the height of that flagpole) for each sail on every flagpole such that the total discount is minimized.
Reformulation
Suppose we number the flagpoles from 1 to N (from front to back). For each flagpole i, you must choose Ai distinct sections (from 1 to Hi). For any given section height h, let xh be the total number of sails placed at height h (across all flagpoles that are tall enough to have this section). Then, the total discount contributed by height h is
The problem reduces to choosing nonnegative integers \(x_1,x_2,\ldots,x_m\) (where \(m = \max\{H_1, H_2, \ldots, H_N\}\)) satisfying
with the constraint that for each \(h\), \(x_h\) cannot exceed the number of flagpoles with height at least \(h\) (denoted by \(\text{cap}(h)\)). That is,
$$x_h \leq \text{cap}(h) = |\{ i \mid H_i \ge h \}|. $$Your goal is to minimize the total discount:
$$\text{Total Discount} = \sum_{h=1}^m \frac{x_h (x_h - 1)}{2}. $$Solve this optimization problem and output the minimal total discount.
inputFormat
The input consists of three lines:
- The first line contains a single integer N (the number of flagpoles).
- The second line contains N space‐separated integers: H1, H2, ..., HN, representing the heights of the flagpoles.
- The third line contains N space‐separated integers: A1, A2, ..., AN, where Ai is the number of sails to be hung on the i-th flagpole. It is guaranteed that \(0 \le A_i \le H_i\).
outputFormat
Output a single integer representing the minimum total discounted pushing force achievable by optimally assigning sail positions.
sample
3
3 3 3
1 1 1
0