Bitaro the Brave 2

시간 제한1초메모리 제한2048 MB

요약
시작 몬스터 j를 정해 j번부터 N번까지, 그다음 1번부터 j-1번까지 처치할 때 필요한 최소 초기 강도를 구한다.
난이도

보통10점 중 6점

유형
누적 합, 배열, 시뮬레이션
정답자
아직 제출이 없습니다

문제

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≤N1 ≤ i ≤ N), Bitaro must have a strength of at least A_iA\_i. Defeating the ii-th monster increases Bitaro’s strength by B_iB\_i.

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

  1. Start with a specific monster jj (1≤j≤N1 ≤ j ≤ N) and defeat the monsters in order: j,j+1,…,Nj, j + 1, \dots , N.
  2. If j≥2j ≥ 2, go back and defeat the monsters 1,2,…,j−11, 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

A_1A\_1 A_2A\_2 …\dots A_NA\_N

B_1B\_1 B_2B\_2 …\dots B_NB\_N

출력

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

제한

  • 2≤N≤500,0002 ≤ N ≤ 500\\, 000.
  • 0≤A_i≤1090 ≤ A\_i ≤ 10^9 (1≤i≤N1 ≤ i ≤ N).
  • 0≤B_i≤1090 ≤ B\_i ≤ 10^9 (1≤i≤N1 ≤ i ≤ N).
  • Given values are all integers.

예제4

  1. 예제 1

    입력
    5
    1 3 2 8 6
    4 3 1 1 2
    
    예상 출력
    1
    
  2. 예제 2

    입력
    5
    1 6 3 3 2
    1 2 1 0 1
    
    예상 출력
    3
    
  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. 예제 4

    입력
    7
    1125 638 0 37 737 820 1202
    23 984 558 350 52 345 580
    
    예상 출력
    0