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;
const int INF = 2e9 + 1;
int n, m, k, p1, p2, sum = 0, sumSmall = 0, minVal = INF;
pair<int, int> rs;
vector<pair<int, int> > a[2][2];
set<pair<int, int> > small, large;
void updateSmall(pair<int, int> val, int type) {
  if (type == 0) {
    small.insert(val);
    sumSmall += val.first;
  } else {
    small.erase(small.find(val));
    sumSmall -= val.first;
  }
}
void balance() {
  while (small.size() > m - p1 - 2 * p2 - 3) {
    large.insert(*small.rbegin());
    updateSmall(*small.rbegin(), 1);
  }
  while (small.size() < m - p1 - 2 * p2 - 3) {
    updateSmall(*large.begin(), 0);
    large.erase(large.begin());
  }
  while (small.size() > 0 && large.size() > 0) {
    pair<int, int> p1 = *small.rbegin();
    pair<int, int> p2 = *large.begin();
    if (p1.first <= p2.first) break;
    updateSmall(p1, 1);
    large.erase(large.find(p2));
    updateSmall(p2, 0);
    large.insert(p1);
  }
}
void init() {
  p1 = min((int)a[1][1].size(), k) - 1,
  p2 = max(k - (int)a[1][1].size(), 0) - 1;
  for (int i = 0; i <= p1; i++) sum += a[1][1][i].first;
  for (int i = 0; i <= p2; i++) sum += a[0][1][i].first + a[1][0][i].first;
  for (int i = p1 + 1; i < a[1][1].size(); i++) updateSmall(a[1][1][i], 0);
  for (int i = p2 + 1; i < a[0][1].size(); i++) updateSmall(a[0][1][i], 0);
  for (int i = p2 + 1; i < a[1][0].size(); i++) updateSmall(a[1][0][i], 0);
  for (int i = 0; i < a[0][0].size(); i++) updateSmall(a[0][0][i], 0);
  balance();
  minVal = sum + sumSmall;
  rs = pair<int, int>(p1, p2);
}
void process() {
  while (p1 >= 0) {
    updateSmall(a[1][1][p1], 0);
    sum -= a[1][1][p1--].first;
    p2++;
    if (p2 >= min(a[0][1].size(), a[1][0].size())) return;
    if (m - p1 - 2 * p2 - 3 < 0) return;
    sum += a[0][1][p2].first + a[1][0][p2].first;
    if (small.find(a[0][1][p2]) != small.end())
      updateSmall(a[0][1][p2], 1);
    else
      large.erase(large.find(a[0][1][p2]));
    if (small.find(a[1][0][p2]) != small.end())
      updateSmall(a[1][0][p2], 1);
    else
      large.erase(large.find(a[1][0][p2]));
    balance();
    if (minVal > sum + sumSmall) {
      minVal = sum + sumSmall;
      rs = pair<int, int>(p1, p2);
    }
  }
}
void print() {
  cout << minVal << '\n';
  for (int i = 0; i <= rs.first; i++) cout << a[1][1][i].second << ' ';
  for (int i = 0; i <= rs.second; i++)
    cout << a[0][1][i].second << ' ' << a[1][0][i].second << ' ';
  vector<pair<int, int> > comb;
  for (int i = 0; i < a[0][0].size(); i++) comb.push_back(a[0][0][i]);
  for (int i = rs.first + 1; i < a[1][1].size(); i++)
    comb.push_back(a[1][1][i]);
  for (int i = rs.second + 1; i < a[0][1].size(); i++)
    comb.push_back(a[0][1][i]);
  for (int i = rs.second + 1; i < a[1][0].size(); i++)
    comb.push_back(a[1][0][i]);
  sort(comb.begin(), comb.end());
  int rem = m - rs.first - 2 * rs.second - 3;
  for (int i = 0; i < rem; i++) cout << comb[i].second << ' ';
}
int main() {
  cin >> n >> m >> k;
  int t, x, y;
  for (int i = 1; i <= n; i++) {
    cin >> t >> x >> y;
    a[x][y].push_back(pair<int, int>(t, i));
  }
  for (int i = 0; i <= 1; i++)
    for (int j = 0; j <= 1; j++) sort(a[i][j].begin(), a[i][j].end());
  if (a[1][1].size() + min(a[1][0].size(), a[0][1].size()) < k ||
      min((int)a[1][1].size(), m) + 2 * max(k - (int)a[1][1].size(), 0) > m) {
    cout << -1;
    return 0;
  }
  init();
  process();
  print();
}