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 CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
int n, k, m, x, y, z;
int ans;
vector<int> a[2][2];
vector<pair<int, int> > f[2][2];
int id[2][2];
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &x, &y, &z);
    a[y][z].push_back(x);
    f[y][z].push_back({x, i + 1});
  }
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 2; j++) {
      sort(a[i][j].begin(), a[i][j].end());
      a[i][j].push_back(1000000);
      sort(f[i][j].begin(), f[i][j].end());
    }
  int ans = -1, sum = 0, u = 0;
  while (u < a[1][1].size()) {
    if (u > m) {
      u++;
      continue;
    }
    int r = max(k - u, 0);
    if (r >= a[1][0].size() || r >= a[0][1].size()) {
      u++;
      continue;
    }
    if ((u + 2 * r) > m) {
      u++;
      continue;
    }
    if (a[1][0].size() + a[0][1].size() + a[0][0].size() + u < m + 3) {
      u++;
      continue;
    }
    break;
  }
  if (u >= a[1][1].size()) {
    cout << -1;
    return 0;
  }
  int r = max(k - u, 0);
  int s1 = r, s2 = r, s3 = 0;
  while (u + s1 + s2 + s3 < m) {
    int d = min(min(a[1][0][s1], a[0][1][s2]), a[0][0][s3]);
    if (d > 10000) {
      cout << -1;
      return 0;
    }
    if (d == a[1][0][s1]) {
      s1++;
      continue;
    }
    if (d == a[0][1][s2]) {
      s2++;
      continue;
    }
    if (d == a[0][0][s3]) {
      s3++;
      continue;
    }
  }
  for (int i = 0; i < u; i++) sum += a[1][1][i];
  for (int i = 0; i < s1; i++) sum += a[1][0][i];
  for (int i = 0; i < s2; i++) sum += a[0][1][i];
  for (int i = 0; i < s3; i++) sum += a[0][0][i];
  ans = sum;
  int q1 = s1, q2 = s2, q3 = s3, q4 = u;
  s1--;
  s2--;
  s3--;
  while (u + 1 < a[1][1].size()) {
    sum += a[1][1][u];
    u++;
    if (s1 == -1 && s2 == -1 && s3 == -1) break;
    int d = 0, pp = 0;
    if ((s1 != -1)) {
      d += a[1][0][s1];
      pp++;
      s1--;
    }
    if ((s2 != -1)) {
      d += a[0][1][s2];
      pp++;
      s2--;
    }
    if ((s3 != -1)) {
      d += a[0][0][s3];
      pp++;
      s3--;
    }
    sum -= d;
    for (int i = 0; i < pp - 1; i++) {
      int gg = min(min(a[1][0][s1 + 1], a[0][1][s2 + 1]), a[0][0][s3 + 1]);
      if (gg > 10000) {
        cout << -1;
        return 0;
      }
      if (gg == a[1][0][s1 + 1]) {
        sum += a[1][0][s1 + 1];
        s1++;
        continue;
      }
      if (gg == a[0][1][s2 + 1]) {
        sum += a[0][1][s2 + 1];
        s2++;
        continue;
      }
      if (gg == a[0][0][s3 + 1]) {
        sum += a[0][0][s3 + 1];
        s3++;
        continue;
      }
    }
    if (sum < ans) {
      ans = sum;
      q4 = u;
      q3 = s3 + 1;
      q2 = s2 + 1;
      q1 = s1 + 1;
    }
  }
  printf("%d\n", ans);
  for (int i = 0; i < q4; i++) printf("%d ", f[1][1][i].second);
  for (int i = 0; i < q3; i++) printf("%d ", f[0][0][i].second);
  for (int i = 0; i < q2; i++) printf("%d ", f[0][1][i].second);
  for (int i = 0; i < q1; i++) printf("%d ", f[1][0][i].second);
  return 0;
}