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

#GD0000039. Lập lịch hai máy (Setting Problems)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Lập lịch hai máy (Setting Problems)

Setting Problems

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn jobs. Job ii requires sis_i time on machine 1 and then gig_i time on machine 2. Each machine handles one job at a time and every job uses machine 1 before machine 2. Choose a common order minimizing the makespan.

Input

The first line contains nn. The second line contains s1..sns_1..s_n and the third line contains g1..gng_1..g_n.

Output

Print the minimum makespan.

Constraints

  • 1≤n≤2⋅1051\le n\le 2\cdot10^5.
  • 1≤si,gi≤1091\le s_i,g_i\le 10^9 for 1≤i≤n1\le i\le n.

Subtasks

  • Subtask 1 (20 points): n≤8n\le 8.
  • Subtask 2 (30 points): n≤30n\le 30.
  • Subtask 3 (50 points): Full constraints.

Examples

Input

3
2 4 3
5 1 2

Output

10

Explanation

The sample follows the ordering rule described in the Vietnamese explanation and produces the shown optimum.