cswork / OAOpen practice workspace

VisaOA #18

Minimum Cost to Select People for Skill Quotas

Estimated HardJudge-ready

Topics (AI-labelled)GreedyPrefix & Ranges

Each person has a non-negative cost and two skill flags. For K=1..n, find the minimum cost so that at least K people have skill 1 and at least K people have skill 2; a person with both skills counts toward both sides; output −1 if infeasible.

Input

n (1≤n≤200000), followed by n lines of cost skill1 skill2. cost is 0..10⁹, and skills are 0/1. Non-negative costs are site-added; answers use 64-bit integers.

Output

Output the minimum cost or −1 for K=1..n in order.

Example 1

Input:

3
5 1 1
4 1 0
4 0 1

Output:

5 13 -1

Example 2

Input:

2
3 1 0
2 0 1

Output:

5 -1

Example 3

Input:

2
0 1 1
7 1 1

Output:

0 7

Editorial

Loading solution…