All problems

Problem 881

Earliest Possible Day of Full Bloom

Hard · 30 pts · Array, Greedy, Sorting

Problem

You have N seeds. Seed i needs PLANT[i] full days of your planting work (on any days, one seed at a time, not necessarily consecutive) and then GROW[i] days of growing before it blooms. Growing happens on its own, in parallel. Find the earliest day by which all seeds are blooming, counting from day 0. Planting the seeds with the longest growing time first is optimal: the answer is the largest finishing time when seeds are planted in that order.

Input

The first line contains N. The second line contains PLANT. The third line contains GROW.

Output

Print the earliest day when all seeds bloom.

Constraints

1 <= N <= 100000; 1 <= PLANT[i], GROW[i] <= 10^4

Example input

3
1 4 3
2 3 1

Example output

9

Explanation

With PLANT = 1 4 3 and GROW = 2 3 1, plant seed 1 (days 0 to 3, blooms on day 7), then seed 0 (day 4, blooms on day 7), then seed 2 (days 5 to 7, blooms on day 9). The answer is 9.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Count Pairs in Two Arrays Next: Divide Chocolate