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.io.*;
import java.util.*;
import java.awt.image.BandedSampleModel;
import java.lang.reflect.Array;
import java.util.Scanner;
public class A {

    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader()
        {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException
        {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do
            {
                ret = ret * 10 + c - '0';
            }  while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException
        {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException
        {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.')
            {
                while ((c = read()) >= '0' && c <= '9')
                {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException
        {
            if (din == null)
                return;
            din.close();
        }
    }

    public static void main(String args[]) throws IOException {
        Reader sc = new Reader();
         {
            int n = sc.nextInt();
            int k = sc.nextInt();
            //int t1=0;
            //int t2=0;
            int books[][] = new int[n][3];

             int alc=0;
             int bothc=0;
             int bobc=0;
            for(int j=0;j<n;j++)
            {
                books[j][0]=sc.nextInt();
                int x=sc.nextInt();
                books[j][1]=x;
                int y=sc.nextInt();
                books[j][2]=y;
                if(x==1&&y==0)
                {
                   alc++;
                }
                else if(x==0&&y==1)
                {
                    bobc++;
                }
                else if(x==1&&y==1)
                {
                    bothc++;
                }
            }
            int alice[]=new int[alc];
            int bob[]=new int[bobc];
            int both[]=new int[bothc];
            alc=0;
            bobc=0;
            bothc=0;
             for(int j=0;j<n;j++)
             {
                 //books[j]=sc.nextInt();
                 //int x=sc.nextInt();
                 //int y=sc.nextInt();
                 int x=books[j][1];
                 int y=books[j][2];
                 if(x==1&&y==0)
                 {
                     alice[alc]=books[j][0];
                     alc++;
                 }
                 else if(x==0&&y==1)
                 {
                     bob[bobc]=books[j][0];
                     bobc++;
                 }
                 else if(x==1&&y==1)
                 {
                     both[bothc]=books[j][0];
                     bothc++;
                 }
             }

            if(alice.length+both.length<k||bob.length+both.length<k)
            {
                System.out.println(-1);
            }
            else
            {
                long ans=0;
                int[] al=new int[alice.length];


                Arrays.sort(alice);
                Arrays.sort(bob);
                Arrays.sort(both);
                int ac=0;
                int bc=0;
                int boc=0;
                while(k>0) {
                    if (ac < alice.length && bc < bob.length && boc < both.length) {
                        if (alice[(ac)] + bob[(bc)] < both[(boc)]) {
                            ans = ans + alice[(ac)] + bob[(bc)];
                            ac++;
                            bc++;
                            k--;
                        } else {
                            ans = ans + both[(boc)];
                            boc++;
                            k--;
                        }
                    } else if (ac >= alice.length || bc >= bob.length) {
                        ans = ans + both[(boc)];
                        boc++;
                        k--;
                    } else {
                        ans = ans + alice[(ac)] + bob[(bc)];
                        ac++;
                        bc++;
                        k--;
                    }
                }
                System.out.println(ans);
            }

        }
    }
}