题目描述
Problem Description
There is a juice machine that can produce a cup of juice of any volume per minute.
There are $n$ people standing in a queue. The $i$-th person will arrive at the juice machine at the $t_i$-th minute and take the cup of juice with the largest volume that has been produced so far. The $i$-th person will be satisfied if they receive a cup of juice with a volume greater than or equal to $a_i$. If there is no juice available at that moment, the $i$-th person will also be dissatisfied.
Determine if it is possible to satisfy everyone. If yes, output the minimum total volume of juice needed to satisfy everyone.
Input Format
The first line contains an integer $n$ $(1 \leq n \leq 2 \times 10^5)$, representing the number of people in the queue.
The next line contains $n$ integers $t_1, t_2, \ldots, t_n$ $(1 \leq t_1 \leq t_2 \leq \ldots \leq t_n \leq 10^9)$, where $t_i$ represents the time when the $i$-th person arrives at the juice machine. If two people arrive at the same time, their order is determined from left to right as given in the input.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ $(1 \leq a_i \leq 10^9)$, where $a_i$ represents the volume requirement of the $i$-th person for the juice.
Output Format
If it is not possible to satisfy everyone, output $-1$.
Otherwise, output a single integer representing the minimum total volume needed.
Sample Input and Output
Sample Input #1
5
1 3 4 6 6
3 8 2 7 4
Sample Output #1
24
Sample Input #2
5
1 3 4 5 5
3 8 2 7 4
Sample Output #2
26
Notes
Explanation for Sample #1:
| Time | Produced | Taken |
|---|---|---|
| 1 | 3 | 3 |
| 2 | - | - |
| 3 | 8 | 8 |
| 4 | 2 | 2 |
| 5 | 4 | - |
| 6 | 7 | 7, 4 |
Explanation for Sample #2:
| Time | Produced | Taken |
|---|---|---|
| 1 | 3 | 3 |
| 2 | 4 | - |
| 3 | 8 | 8 |
| 4 | 4 | 4 |
| 5 | 7 | 7, 4 |
| 6 | - | - |
输入格式
见题面 Input 部分
输出格式
见题面 Output 部分