cswork / OAOpen practice workspace

MetaOA #24

Get Minimum Round Trip Cost

EasyJudge-ready

Topics (AI-labelled)In-place ArraysPrefix & Ranges

Given arrays departing and returning of equal length, giving the outbound and return fares on day i respectively. Choose a departure day i and a return day j, where the return must be strictly later than the departure (j > i), and find the minimum of departing[i] + returning[j]. This strict ordering constraint was filled in from public reports of the same problem.

Site constraints: 2≤n≤200000, -1000000000≤any fare≤1000000000.

Input

The first line contains n; the second line contains n outbound fares departing; the third line contains n return fares returning.

Output

Output the minimum total cost such that the return day j is strictly greater than the departure day i.

Example 1

Input:

4
1 2 3 4
4 3 2 1

Output:

2

Example 2

Input:

2
100 1
1 100

Output:

200

Example 3

Input:

3
8 1 5
7 2 9

Output:

10

Example 4

Input:

2
4 4
3 3

Output:

7

Example 5

Input:

3
-5 8 -2
7 -9 4

Output:

-14

Example 6

Input:

4
1 2 3 4
1 2 3 4

Output:

3

Example notes

Scan from right to left, maintaining the minimum of returning[i+1..n-1], pair it with departing[i], and update the answer.

Editorial

Loading solution…