cswork / OAOpen practice workspace

AmazonOA #389

Minimum Time Spent

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyBinary SearchPrefix & Ranges

Each movie has a release time and a duration and can be started at any time after its release. Watch at least one movie of each of the two genres; you cannot watch two movies at once, but you may switch back to back with no gap. Find the earliest finish time; the two genres may be watched in either order.

Input

The first line contains n m; the next four lines are, in order, n comedy release times, n comedy durations, m drama release times, and m drama durations. Original bounds: 1≤n,m≤100000; all values in the four arrays are 1..10^6.

Output

Output a single integer answer.

Example 1

Input:

2 2
1 4
3 2
5 2
2 2

Output:

6

Example 2

Input:

1 1
10
1
1
2

Output:

11

Example 3

Input:

1 1
1
2
1
3

Output:

6

Example notes

The first example is 6: the comedy (release 1, duration 3) ends at 4, then watch the drama (release 2, duration 2). The original explanation described the movie released at 5 as starting at 4; this site corrects it. The second example is 11 and the third is 6.

Editorial

Loading solution…