Problem list
Log in
#coci00713 · COCI 2025/2026 Contest 3

Ravnalo

Unrated 1.0 s 512 MB

Architect Hrvoje has been tasked with drawing a large irregular wall composed of vertical columns. The wall consists of $n$ columns placed next to each other, where the $i$-th column has height $a_{i}$ and width 1. In order to make the drawing as complicated as possible, the $i$-th column is divided into $b_{i}$ parts of equal height. Hrvoje has only a ruler and a pencil at his disposal. With one stroke of the pencil he can draw one line segment (a straight line between two points) without lifting the pencil. His goal is to draw the entire wall, including all column edges and all boundaries between their divisions, using as few line segments as possible. For the given wall, output the minimum number of line segments that need to be drawn in order to draw the wall completely.

Input

The first line contains a natural number $n$ ($1 \le n \le 10^{5}$), the number from the problem statement. The second line contains a sequence of $n$ numbers $1 \le a_{i} \le 10^{9}$, from the problem statement. The third line contains a sequence of $n$ numbers $1 \le b_{i} \le 10^{9}$, from the problem statement.

Output

In the first and only line output one number – the minimum number of line segments Hrvoje needs to draw to draw the wall.

Example 1

Input
3
4 6 4
2 3 4
Output
10

Example 2

Input
3
4 6 3
3 3 2
Output
12

Pojašnjenje drugog probnog primjera: Hrvoje will extend the topmost line segment in the first
column into the second column and thus “merge” 2 line segments into one. He will do the same with the
3 line segments at the bottom of the wall. This way, he will draw a total of 12 line segments, and it can
be shown that this is the minimum number of line segments that Hrvoje needs to draw.
Source
COCI 2025/2026 Contest 3
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code