PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000286. Trò chơi hai robot (Grid Game)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Trò chơi hai robot (Grid Game)

Grid Game

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Two robots traverse a 2×n2\times n positive-valued grid from the upper-left to the lower-right cell, moving only right or down. The first robot collects points and clears its entire path. The second then collects remaining points on its own path, which may intersect the first. The first minimizes the second robot’s maximum attainable score; the second maximizes its score. Compute that score.

Input

First line: nn. The next two lines each contain nn positive integers, the upper and lower rows respectively.

Output

Print the second robot's score under optimal play.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le30; 1≤ar,c≤1051\le a_{r,c}\le10^5.
  • Subtask 2 (30 points): 1≤n≤500001\le n\le50000; 1≤ar,c≤1051\le a_{r,c}\le10^5; the two rows are identical.
  • Subtask 3 (50 points): 1≤n≤500001\le n\le50000; 1≤ar,c≤1051\le a_{r,c}\le10^5.

Example 1

Input

3
2 5 4
1 5 1

Output

4

Explanation

Turning down at column 2 leaves 4 above and 1 below; robot 2 obtains 4. The other choices allow 9 and 6.

Example 2

Input

3
3 3 1
8 5 2

Output

4

Explanation

Turning down at the first column leaves 4 above. Turning later permits at least 8 points.

Example 3

Input

4
1 3 1 15
1 3 3 1

Output

7

Explanation

Turning down at the last column leaves 7 below. Earlier turns leave at least 15 above.