시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 2048 MB150615347.748%

문제

Bitaro, the brave hero, has set out on an adventure to defeat monsters.

Bitaro has a strength value, denoted as $x$, which starts at an initial value. There are $N$ monsters, each labeled with a number from $1$ to $N$. To defeat the $i$-th monster ($1 ≤ i ≤ N$), Bitaro must have a strength of at least $A_i$. Defeating the $i$-th monster increases Bitaro’s strength by $B_i$.

Bitaro wants to defeat all the monsters using the following strategy:

  1. Start with a specific monster $j$ ($1 ≤ j ≤ N$) and defeat the monsters in order: $j, j + 1, \dots , N$.
  2. If $j ≥ 2$, go back and defeat the monsters $1, 2, \dots , j − 1$ in sequence.

Given the information about the monsters, write a program to determine the minimum initial strength $x$ required for Bitaro to defeat all the monsters.

입력

Read the following data from the standard input.

$N$

$A_1$ $A_2$ $\dots$ $A_N$

$B_1$ $B_2$ $\dots$ $B_N$

출력

Output a single integer, the minimum initial strength $x$ required for Bitaro to defeat all the monsters.

제한

  • $2 ≤ N ≤ 500\, 000$.
  • $0 ≤ A_i ≤ 10^9$ ($1 ≤ i ≤ N$).
  • $0 ≤ B_i ≤ 10^9$ ($1 ≤ i ≤ N$).
  • Given values are all integers.

서브태스크

번호배점제한
110

$N ≤ 2\, 000$, and the minimum initial strength $x$ is $10$ or less.

221

$N ≤ 2\, 000$.

319

The minimum initial strength $x$ is $10$ or less.

422

$B_i = 1$ ($1 ≤ i ≤ N$).

528

No additional constraints.

예제 입력 1

5
1 3 2 8 6
4 3 1 1 2

예제 출력 1

1
  • Start with an initial strength of $1$.
  • Defeat monsters in the following order:
    1. Defeat monster $1$. Strength increases by $4$ to $5$.
    2. Defeat monster $2$. Strength increases by $3$ to $8$.
    3. Defeat monster $3$. Strength increases by $1$ to $9$.
    4. Defeat monster $4$. Strength increases by $1$ to $10$.
    5. Defeat monster $5$. Strength increases by $2$ to $12$.

This sample input satisfies the constraints of Subtasks 1, 2, 3 and 5.

예제 입력 2

5
1 6 3 3 2
1 2 1 0 1

예제 출력 2

3
  • Start with an initial strength of $3$.
  • Defeat monsters in the following order:
    1. Defeat monster $3$. Strength increases by $1$ to $4$.
    2. Defeat monster $4$. Strength increases by $0$ to $4$.
    3. Defeat monster $5$. Strength increases by $1$ to $5$.
    4. Defeat monster $1$. Strength increases by $1$ to $6$.
    5. Defeat monster $2$. Strength increases by $2$ to $8$.

This sample input satisfies the constraints of Subtasks 1, 2, 3 and 5.

예제 입력 3

10
11 9 8 12 7 7 8 12 9 10
1 1 1 1 1 1 1 1 1 1

예제 출력 3

9

This sample input satisfies the constraints of all the subtasks.

예제 입력 4

7
1125 638 0 37 737 820 1202
23 984 558 350 52 345 580

예제 출력 4

0

This sample input satisfies the constraints of Subtasks 1, 2, 3 and 5.

채점 및 기타 정보

  • 예제는 채점하지 않는다.
  • 이 문제의 채점 우선 순위는 2이다.