cswork / OAOpen practice workspace

AmazonOA #268

Maximize Secondary Tasks Scheduled

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyTwo Pointers

n primary tasks are completed over n days, exactly one per day. The total time on each day must not exceed limit; each day can also hold at most one secondary task, and each of the n secondary tasks can be used at most once. Find the maximum number of secondary tasks that can be completed; some secondary tasks may be left unscheduled.

Input

The first line contains n limit, the second line primary, and the third line secondary. The original text had no numeric bounds; this site uses 1≤n≤200000, 1≤limit≤10^9, 1≤primary[i]≤limit, 1≤secondary[i]≤10^9.

Output

Output a single integer, the answer.

Example 1

Input:

2 11
10 1
1 10

Output:

2

Example 2

Input:

2 5
4 4
2 3

Output:

0

Example 3

Input:

3 4
1 2 3
3 2 1

Output:

3

Example notes

The answers are 2, 0 and 3. In the first example the leftover capacities 1 and 10 should be matched with 1 and 10 respectively; the largest leftover must not be wasted on the shortest task first.

Editorial

Loading solution…