PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000112. Đọc sách – phiên bản khó (Reading Books (hard version))

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đọc sách – phiên bản khó (Reading Books (hard version))

Reading Books (hard version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn books; book ii requires tit_i time, and bits ai,bia_i,b_i indicate whether Alice and Bob like it. Choose exactly mm distinct books, with at least kk liked by Alice and at least kk liked by Bob, minimizing the total reading time. Print -1 if impossible, otherwise the minimum time and exactly mm indices.

Input

Line 1 contains n,m,kn,m,k. Each of the next nn lines contains ti,ai,bit_i,a_i,b_i.

Output

Print -1 if impossible. Otherwise line 1 contains the minimum time and line 2 contains exactly mm distinct indices from [1,n][1,n]. Any optimum selection is accepted.

Subtasks

  • Subtask 1 (20 points): 1≤k≤m≤n≤121\le k\le m\le n\le 12; 1≤ti≤1041\le t_i\le10^4; ai,bi∈{0,1}a_i,b_i\in\{0,1\}.
  • Subtask 2 (30 points): 1≤k≤m≤n≤25001\le k\le m\le n\le 2500; 1≤ti≤1041\le t_i\le10^4; ai,bi∈{0,1}a_i,b_i\in\{0,1\}.
  • Subtask 3 (50 points): 1≤k≤m≤n≤2000001\le k\le m\le n\le 200000; 1≤ti≤1041\le t_i\le10^4; ai,bi∈{0,1}a_i,b_i\in\{0,1\}.

Examples

Input

6 3 1
6 0 0
11 1 0
9 0 1
21 1 1
10 1 0
8 0 1

Output

24
5 6 1

Explanation

Select books 66 (time 88, liked by Bob), 55 (time 1010, liked by Alice), and 11 (time 66), totaling 2424 with exactly three books and each reader liking at least one. This is optimal.