Choosing conference speakers

N people want to speak at a conference; each has a rating and a topic. Choose at most K speakers so that no topic is chosen more than Q times and the total rating is as large as possible.

Reported in: Infosys (SP / DSE · 22 Nov 2026) (memory-based)

Input

First line: N. Next N lines: rating and topic. Last line: K and Q.

Output

The largest total rating.

Constraints

1 ≤ N ≤ 10^5; 1 ≤ K, Q ≤ N; 1 ≤ rating ≤ 10^9.

Example 1

Input:
5
9 1
8 1
7 2
5 1
4 3
3 1

Output:
20

Example 2

Input:
4
10 1
9 1
8 1
1 2
3 2

Output:
20

Hint: Go from the highest rating down, skipping anyone whose topic is already used Q times, until you have K speakers. Both limits only count people, so this greedy choice is always optimal.

All coding practice