cswork / OAOpen practice workspace

AmazonOA #341

Maximize Secondary Tasks Scheduling

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyTwo Pointers

There are n primary tasks and n secondary tasks. Over n days, schedule exactly one primary task each day; each day may also take at most one secondary task, each task can be used only once, and a day's total duration must not exceed limit. Find the maximum number of secondary tasks. If some primary task alone exceeds the limit, return −1 according to this site's no-solution output convention. Zero durations are also judged by whether the total stays within the limit.

Input

The first line contains n limit, the second line contains n primary values, and the third line contains n secondary values. On this site 1≤n≤100000, 0≤limit and all durations≤10^9. Inputs with a primary task over the limit are kept, and only the −1 no-solution encoding is added.

Output

Output a single integer, the answer.

Example 1

Input:

2 10
1 9
1 9

Output:

2

Example 2

Input:

1 0
0
0

Output:

1

Example 3

Input:

1 2
3
0

Output:

-1

Example notes

The outputs of the three public examples are 2; 1; and -1, checked by independent enumeration or direct simulation.

Editorial

Loading solution…