#P8109. Balloon Distribution

    ID: 21292 Type: Default 1000ms 256MiB

Balloon Distribution

Balloon Distribution

You are given two arrays of positive integers: \(a = [a_1, a_2, \ldots, a_n]\) and \(b = [b_1, b_2, \ldots, b_n]\). The array \(a\) represents the predicted number of teams that will solve each of the \(n\) problems in a contest, and \(b\) represents the number of balloons available for each of \(n\) distinct colors. Due to budgeting constraints, each balloon color can be assigned to at most one problem, and each problem can be assigned exactly one balloon color.

When a problem \(i\) is assigned a color with \(b_j\) balloons, the actual number of balloons awarded for that problem is \(\min(a_i, b_j)\). Your task is to determine an assignment of balloon colors to problems such that the total number of balloons distributed is maximized.

Input Format: The first line contains an integer \(n\) representing the number of problems. The second line contains \(n\) space-separated integers \(a_1, a_2, \ldots, a_n\). The third line contains \(n\) space-separated integers \(b_1, b_2, \ldots, b_n\).

Output Format: Output a single integer, the maximum total number of balloons that can be distributed.

Note: All formulas are given in \(\LaTeX\) format.

inputFormat

The input consists of three lines:

  • The first line contains an integer \(n\) (\(1 \leq n \leq 2 \times 10^5\)).
  • The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) where \(1 \leq a_i \leq 10^9\).
  • The third line contains \(n\) integers \(b_1, b_2, \ldots, b_n\) where \(1 \leq b_i \leq 10^9\).

outputFormat

Output a single integer representing the maximum number of balloons that can be awarded under an optimal assignment of colors to problems.

sample

3
3 1 2
2 1 3
6