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;
struct Node {
  int son[2], val, pri, siz;
  int sum;
} t[2000086];
int cnt;
int root;
inline int rand() {
  static int seed = 12345;
  return seed = (int)seed * 482711LL % 2147483647;
}
void up(int x) {
  t[x].siz = t[(t[x].son[0])].siz + t[(t[x].son[1])].siz + 1;
  t[x].sum = t[(t[x].son[0])].sum + t[(t[x].son[1])].sum + t[x].val;
}
void split(int x, int val, int &a, int &b) {
  if (!x) {
    a = b = 0;
    return;
  }
  if (t[x].val <= val)
    a = x, split((t[x].son[1]), val, t[x].son[1], b);
  else
    b = x, split((t[x].son[0]), val, a, t[x].son[0]);
  up(x);
}
void Split(int x, int k, int &a, int &b) {
  if (!x) {
    a = b = 0;
    return;
  }
  if (t[(t[x].son[0])].siz < k)
    a = x, Split((t[x].son[1]), k - t[(t[x].son[0])].siz - 1, t[x].son[1], b);
  else
    b = x, Split((t[x].son[0]), k, a, t[x].son[0]);
  up(x);
}
int merge(int x, int y) {
  if (x == 0 || y == 0) return x + y;
  if (t[x].pri > t[y].pri) {
    t[x].son[1] = merge((t[x].son[1]), y);
    up(x);
    return x;
  } else {
    t[y].son[0] = merge(x, (t[y].son[0]));
    up(y);
    return y;
  }
}
int newnode(int val) {
  cnt++;
  t[cnt].val = t[cnt].sum = val;
  t[cnt].pri = rand();
  t[cnt].siz = 1;
  return cnt;
}
int n, m, k;
vector<pair<int, int>> v[3];
int x, a, b, c, d;
int ans = 2e9 + 5, id;
inline void insert(int x) {
  split(root, x, a, b);
  root = merge(merge(a, newnode(x)), b);
}
inline void erase(int x) {
  split(root, x, a, b);
  split(a, x - 1, c, d);
  root = merge(merge(c, merge((t[d].son[0]), (t[d].son[1]))), b);
}
void dfs(int i) {
  if (!i) return;
  dfs((t[i].son[0]));
  printf("%d ", t[i].val);
  dfs((t[i].son[1]));
}
inline int query(int x) {
  if (!x) return 0;
  int a, b;
  Split(root, x, a, b);
  int sum = t[a].sum;
  root = merge(a, b);
  return sum;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
    q;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &x, &a, &b);
    if (!a && !b) {
      insert(x), q.push({x, i});
      continue;
    }
    if (a && b)
      v[0].push_back({x, i}), insert(x);
    else if (a)
      v[1].push_back({x, i});
    else
      v[2].push_back({x, i});
  }
  if (v[0].size() + v[1].size() < k || v[0].size() + v[2].size() < k)
    return printf("-1"), 0;
  if (v[0].size() < k && v[0].size() + 2 * (k - v[0].size()) > m)
    return printf("-1"), 0;
  for (int i = 0; i < 3; i++) sort(v[i].begin(), v[i].end());
  for (int i = 0; i < 3; i++) {
    for (int j = 1; j < v[i].size(); j++) v[i][j].first += v[i][j - 1].first;
  }
  for (int i = 1; i < 3; i++)
    for (int j = k; j < v[i].size(); j++)
      insert(v[i][j].first - v[i][j - 1].first);
  if (v[1].size() >= k && v[2].size() >= k) {
    if (2 * k <= m) {
      int sum = v[1][k - 1].first + v[2][k - 1].first + query(m - 2 * k);
      if (sum < ans) ans = sum, id = 0;
    }
  }
  if (v[1].size() >= k)
    insert(v[1][k - 1].first - (k - 1 ? v[1][k - 2].first : 0));
  if (v[2].size() >= k)
    insert(v[2][k - 1].first - (k - 1 ? v[2][k - 2].first : 0));
  for (int i = 0; i < k - 1 && i < v[0].size(); i++) {
    erase(v[0][i].first - (i ? v[0][i - 1].first : 0));
    if (k - 2 - i >= 0 && k - 2 - i < v[1].size() && k - 2 - i < v[2].size()) {
      if (i + 1 + 2 * (k - (i + 1)) <= m) {
        int sum = v[0][i].first + v[1][k - 2 - i].first +
                  v[2][k - 2 - i].first +
                  query(m - (i + 1 + 2 * (k - (i + 1))));
        if (sum < ans) ans = sum, id = i + 1;
      }
    }
    if (k - 2 - i >= 0 && k - 2 - i < v[1].size())
      insert(v[1][k - 2 - i].first - (k - 2 - i ? v[1][k - 3 - i].first : 0));
    if (k - 2 - i >= 0 && k - 2 - i < v[2].size())
      insert(v[2][k - 2 - i].first - (k - 2 - i ? v[2][k - 3 - i].first : 0));
  }
  if (v[0].size() >= k) {
    erase(v[0][k - 1].first - (k - 1 ? v[0][k - 2].first : 0));
    int sum = v[0][k - 1].first + query(m - k);
    if (sum < ans) ans = sum, id = k;
  }
  printf("%d\n", ans);
  for (int i = 0; i < id; i++) printf("%d ", v[0][i].second);
  for (int i = 1; i <= k - id; i++)
    printf("%d %d ", v[1][i - 1].second, v[2][i - 1].second);
  for (int i = id; i < v[0].size(); i++)
    q.push({v[0][i].first - (i ? v[0][i - 1].first : 0), v[0][i].second});
  for (int i = k - id; i < v[1].size(); i++)
    q.push({v[1][i].first - (i ? v[1][i - 1].first : 0), v[1][i].second});
  for (int i = k - id; i < v[2].size(); i++)
    q.push({v[2][i].first - (i ? v[2][i - 1].first : 0), v[2][i].second});
  for (int i = m - id - (k - id) * 2; i; i--) {
    printf("%d ", q.top().second), q.pop();
  }
}