Public Judge

pjudge

時間限制: 3 s 記憶體限制: 1024 MB 總分: 100

#21885. 【PR #14】Wolf Catches Rabbit

统计

Given a set of points $pos_{1...n}$ on a number line, there are some wolves located at these points. Their positions are within the range of $pos_{1...n}$. They want to prey on some rabbits, but due to limited energy, each wolf can prey on at most one rabbit. Since they cannot starve, each wolf must prey on at least one rabbit.

There are two main bases for rabbits located at $0$ and $L$, each containing an infinite number of rabbits. Additionally, there are some stray rabbits whose positions are also within the range of $pos_{1...n}$.

A wolf at position $x$ wanting to prey on a rabbit at position $y$ incurs a cost of $|x-y|$. What is the minimum total cost?

Input

The first line contains a positive integer $n$, representing the size of the set of positions where wolves and rabbits exist.

The next line contains $n$ integers $a_i$. If $a_i > 0$, it means there are $a_i$ wolves at position $pos_i$; otherwise, it means there are $-a_i$ rabbits at position $pos_i$.

The next line contains $n+1$ integers $b_{0 \sim n}$, representing $pos_{i+1} - pos_i$, where $pos_0 = 0$ and $pos_{n+1} = L$. Note that $b_i$ may be $0$.

Output

Output a single integer representing the answer.

Examples

Input 1

1
-100
1 1

Output 1

0

Input 2

1
100
1 1

Output 2

100

Note 3~4

See the additional files.

Constraints

For $100\%$ of the data, let $c_i = |a_i|$. Then $1 \le n \le 5 \times 10^5$, $0 \le c_i \le 10^5$, and $0 \le L \le 5 \times 10^8$.

The additional constraints for each subtask are as follows:

Subtask ID Score Constraints
1 15 $n, \sum c_i \le 15$
2 10 $n, \sum c_i \le 5000$
3 5 $a_i \ge 0$
4 20 $b_i = 1$
5 25 $n, \sum c_i \le 5 \times 10^5$
6 5 $n \le 10^5$
7 20 No additional constraints

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.