cswork / OAOpen practice workspace

DoorDashOA #6

Maximize Total Profit by Assigning Chefs to Dishes

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyBinary Search

Each chef makes at most one dish, and a dish can be chosen by several chefs; a chef can only make dishes whose difficulty ≤ the chef's skill, and each chef chooses the highest profit among the dishes they can make. If a chef can make no dish, the profit is 0.

Input

n (1..200000) and n skill values; m (1..200000), m difficulties, and m profits; all values are 0..10⁹. This site adopts the typical limits given by the original problem.

Output

Output the total profit (64-bit).

Example 1

Input:

3
1 2 3
3
1 2 3
1 2 3

Output:

6

Example 2

Input:

2
0 1
2
2 0
100 7

Output:

14

Example 3

Input:

2
2 2
2
1 3
8 99

Output:

16

Editorial

Loading solution…