Problem: Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.

Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.

There are n books in the family library. The i-th book is described by three integers: t_i — the amount of time Alice and Bob need to spend to read it, a_i (equals 1 if Alice likes the i-th book and 0 if not), and b_i (equals 1 if Bob likes the i-th book and 0 if not).

So they need to choose some books from the given n books in such a way that:

  * Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; 
  * the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). 



The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of t_i over all books that are in the chosen set.

Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.

Input

The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 2 ⋅ 10^5).

The next n lines contain descriptions of books, one description per line: the i-th line contains three integers t_i, a_i and b_i (1 ≤ t_i ≤ 10^4, 0 ≤ a_i, b_i ≤ 1), where:

  * t_i — the amount of time required for reading the i-th book; 
  * a_i equals 1 if Alice likes the i-th book and 0 otherwise; 
  * b_i equals 1 if Bob likes the i-th book and 0 otherwise. 

Output

If there is no solution, print only one integer -1. Otherwise print one integer T — the minimum total reading time of the suitable set of books.

Examples

Input

8 4
7 1 1
2 1 1
4 0 1
8 1 1
1 0 1
1 1 1
1 0 1
3 0 0


Output

18


Input

5 2
6 0 0
9 0 0
1 0 1
2 1 1
5 1 0


Output

8


Input

5 3
3 0 0
2 1 0
3 1 0
5 0 1
3 0 1


Output

-1

Solution (in JAVA):

Solution:
import java.util.*;
import java.io.*;

public class e2 {
    public static void main(String[] Args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int toRead = sc.nextInt();
        int mustLike = sc.nextInt();

        long ans = 987654321987654321l;
        int bothNum = 0;
        int aliceNum = 0;
        int bobNum = 0;
        int noneNum = 0;

        ArrayList<Book> alice = new ArrayList<Book>();
        ArrayList<Book> bob = new ArrayList<Book>();
        ArrayList<Book> both = new ArrayList<Book>();
        ArrayList<Book> none = new ArrayList<Book>();

        for (int i = 1; i <= n; i++) {
            int time = sc.nextInt();
            int a = sc.nextInt();
            int b = sc.nextInt();
            if (a + b == 2)
                both.add(new Book(i, time));
            else if (a == 1)
                alice.add(new Book(i, time));
            else if (b == 1)
                bob.add(new Book(i, time));
            else
                none.add(new Book(i, time));
        }

        Collections.sort(alice);
        Collections.sort(bob);
        Collections.sort(none);
        Collections.sort(both);

        int bothCur, aliceCur, bobCur, noneCur;

        long curTime = 0;
        int i;
        for (bothCur = 0; bothCur < toRead && bothCur < both.size(); bothCur++)
            curTime += both.get(bothCur).time;
        for (aliceCur = 0; aliceCur + bothCur < mustLike && aliceCur < alice.size();
                aliceCur++)
            curTime += alice.get(aliceCur).time;
        for (bobCur = 0; bobCur + bothCur < mustLike && bobCur < bob.size();
                bobCur++)
            curTime += bob.get(bobCur).time;
        noneCur = 0;
        if (aliceCur + bothCur < mustLike || bobCur + bothCur < mustLike) {
            System.out.println(-1);
            return;
        }
        while (bothCur + aliceCur + bobCur + noneCur < toRead) {
            if (aliceCur < alice.size() && 
                    (bobCur == bob.size() || 
                     bob.get(bobCur).time >= alice.get(aliceCur).time) &&
                    (noneCur == none.size() ||
                     none.get(noneCur).time >= alice.get(aliceCur).time)) {
                curTime += alice.get(aliceCur++).time;
            } else if (bobCur < bob.size() &&
                    (noneCur == none.size() || 
                     none.get(noneCur).time >= bob.get(bobCur).time)) {
                curTime += bob.get(bobCur++).time;
            } else {
                curTime += none.get(noneCur++).time;
            }
        }
        if (curTime < ans) {
            ans = curTime;
            bothNum = bothCur;
            aliceNum = aliceCur;
            bobNum = bobCur;
            noneNum = noneCur;
        }
        while (bothCur > 0) {
            curTime -= both.get(--bothCur).time;
            while (aliceCur < alice.size() && bothCur + aliceCur < mustLike) {
                curTime += alice.get(aliceCur++).time;
            }
            while (bobCur < bob.size() && bothCur + bobCur < mustLike) {
                curTime += bob.get(bobCur++).time;
            }
            while (bothCur + bobCur + aliceCur + noneCur < toRead) {
                if (aliceCur < alice.size() && 
                        (bobCur == bob.size() || 
                         bob.get(bobCur).time >= alice.get(aliceCur).time) &&
                        (noneCur == none.size() ||
                         none.get(noneCur).time >= alice.get(aliceCur).time)) {
                    curTime += alice.get(aliceCur++).time;
                } else if (bobCur < bob.size() &&
                        (noneCur == none.size() || 
                         none.get(noneCur).time >= bob.get(bobCur).time)) {
                    curTime += bob.get(bobCur++).time;
                } else if (noneCur < none.size()){
                    curTime += none.get(noneCur++).time;
                } else {
                    break;
                }
            }
            while (bothCur + bobCur + aliceCur + noneCur > toRead) {
                if (aliceCur != 0 && aliceCur + bothCur > mustLike && 
                        (bobCur == 0 || 
                         bob.get(bobCur - 1).time <= alice.get(aliceCur - 1).time ||
                         bobCur + bothCur == mustLike) &&
                        (noneCur == 0 ||
                         none.get(noneCur - 1).time <= alice.get(aliceCur - 1).time)) {
                    curTime -= alice.get(--aliceCur).time;
                } else if (bobCur != 0 && bobCur + bothCur > mustLike &&
                        (noneCur == 0 || 
                         none.get(noneCur - 1).time >= bob.get(bobCur - 1).time)) {
                    curTime -= bob.get(--bobCur).time;
                } else if (noneCur != 0) {
                    curTime -= none.get(--noneCur).time;
                } else {
                    break;
                }
            }
            if (bothCur + bobCur + aliceCur + noneCur == toRead &&
                    curTime < ans &&
                    bobCur + bothCur >= mustLike &&
                    aliceCur + bothCur >= mustLike) {
                ans = curTime;
                bothNum = bothCur;
                aliceNum = aliceCur;
                bobNum = bobCur;
                noneNum = noneCur;
            }
        }


        


        
        PrintWriter out = new PrintWriter(new BufferedWriter(
                    new OutputStreamWriter(System.out)));

        if (bothNum + aliceNum + bobNum + noneNum != toRead) {
            out.println(-1);
        } else {
            out.println(ans);
            for (i = 0; i < bothNum; i++)
                out.print(both.get(i).index + " ");
            for (i = 0; i < aliceNum; i++)
                out.print(alice.get(i).index + " ");
            for (i = 0; i < bobNum; i++)
                out.print(bob.get(i).index + " ");
            for (i = 0; i < noneNum; i++)
                out.print(none.get(i).index + " ");
            out.println();
        }
        
        out.close();
    }

    public static class Book implements Comparable<Book> {
        int index, time;
        Book(int i, int t) {
            index = i;
            time = t;
        }

        public int compareTo(Book o) {
            return time - o.time;
        }   
    }
}