PivotOJ

Bitaro the Brave 2

시간 제한: 1000ms메모리 제한: 2048MB출처: JOI 2024-2025 본선BOJ 33725

문제

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

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

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

  1. Start with a specific monster jj (1 ≤ j ≤ N) and defeat the monsters in order: j,j+1,,Nj, 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 xx required for Bitaro to defeat all the monsters.

입력

Read the following data from the standard input.

NN

A1A_1 A2A_2 \dots ANA_N

B1B_1 B2B_2 \dots BNB_N

출력

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

예제

예제 1

입력
5
1 3 2 8 6
4 3 1 1 2
출력
1

예제 2

입력
5
1 6 3 3 2
1 2 1 0 1
출력
3

예제 3

입력
10
11 9 8 12 7 7 8 12 9 10
1 1 1 1 1 1 1 1 1 1
출력
9

예제 4

입력
7
1125 638 0 37 737 820 1202
23 984 558 350 52 345 580
출력
0
코드를 제출하려면 로그인하세요.