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 t[200005], a[200005], b[200005];
pair<int, int> aa[200005], bb[200005], ab[200005];
int sumaa[200005], sumbb[200005], sumab[200005];
int BITcnt[10005], BITsum[10005];
int anss[200005], ccc;
vector<int> v[10005];
bool f[200005];
int lowbit(int x) { return x & (-x); }
void updatecnt(int x, int d) {
  while (x <= 10000) {
    BITcnt[x] += d;
    x += lowbit(x);
  }
}
void updatesum(int x, int d) {
  while (x <= 10000) {
    BITsum[x] += d;
    x += lowbit(x);
  }
}
int querycnt(int x) {
  int ret = 0;
  while (x) {
    ret += BITcnt[x];
    x -= lowbit(x);
  }
  return ret;
}
int querysum(int x) {
  int ret = 0;
  while (x) {
    ret += BITsum[x];
    x -= lowbit(x);
  }
  return ret;
}
bool check(int x, int c) { return querycnt(x) < c; }
int binarysearch(int l, int r, int c) {
  if (l == r)
    return l;
  else if (r - l == 1) {
    if (check(r, c)) return r;
    return l;
  }
  int mid = (l + r) / 2;
  if (check(mid, c))
    return binarysearch(mid, r, c);
  else
    return binarysearch(l, mid - 1, c);
}
int main() {
  int n, m, k, cnta, cntb, cntab, ans, now, p, pp, qq, ansi;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) scanf("%d%d%d", &t[i], &a[i], &b[i]);
  cnta = 0;
  cntb = 0;
  cntab = 0;
  for (int i = 1; i <= n; i++)
    if (a[i] && b[i])
      ab[++cntab] = make_pair(t[i], i);
    else if (a[i])
      aa[++cnta] = make_pair(t[i], i);
    else if (b[i])
      bb[++cntb] = make_pair(t[i], i);
  sort(ab + 1, ab + cntab + 1);
  sort(aa + 1, aa + cnta + 1);
  sort(bb + 1, bb + cntb + 1);
  for (int i = 1; i <= cntab; i++) sumab[i] = sumab[i - 1] + ab[i].first;
  for (int i = 1; i <= cnta; i++) sumaa[i] = sumaa[i - 1] + aa[i].first;
  for (int i = 1; i <= cntb; i++) sumbb[i] = sumbb[i - 1] + bb[i].first;
  for (int i = 1; i <= cntab; i++) {
    updatecnt(ab[i].first, 1);
    updatesum(ab[i].first, ab[i].first);
  }
  for (int i = k + 1; i <= cnta; i++) {
    updatecnt(aa[i].first, 1);
    updatesum(aa[i].first, aa[i].first);
  }
  for (int i = k + 1; i <= cntb; i++) {
    updatecnt(bb[i].first, 1);
    updatesum(bb[i].first, bb[i].first);
  }
  for (int i = 1; i <= n; i++)
    if (!a[i] && !b[i]) {
      updatecnt(t[i], 1);
      updatesum(t[i], t[i]);
    }
  ans = 0x7fffffff;
  for (int i = 0; i <= cntab; i++) {
    pp = max(0, k - i);
    qq = max(0, k - i);
    if (cnta < k - i || cntb < k - i) {
      if (i < cntab) {
        updatecnt(ab[i + 1].first, -1);
        updatesum(ab[i + 1].first, -ab[i + 1].first);
      }
      if (pp > 0 && pp <= cnta) {
        updatecnt(aa[pp].first, 1);
        updatesum(aa[pp].first, aa[pp].first);
      }
      if (qq > 0 && qq <= cntb) {
        updatecnt(bb[qq].first, 1);
        updatesum(bb[qq].first, bb[qq].first);
      }
      continue;
    }
    if (i + pp + qq > m) {
      if (i < cntab) {
        updatecnt(ab[i + 1].first, -1);
        updatesum(ab[i + 1].first, -ab[i + 1].first);
      }
      if (pp) {
        updatecnt(aa[pp].first, 1);
        updatesum(aa[pp].first, aa[pp].first);
      }
      if (qq) {
        updatecnt(bb[qq].first, 1);
        updatesum(bb[qq].first, bb[qq].first);
      }
      continue;
    }
    if (i + pp + qq == m) {
      if (sumab[i] + sumaa[pp] + sumbb[qq] < ans) {
        ans = sumab[i] + sumaa[pp] + sumbb[qq];
        ansi = i;
      }
    } else {
      p = binarysearch(0, 10000, m - i - pp - qq);
      if (sumab[i] + sumaa[pp] + sumbb[qq] + querysum(p) +
              (m - i - pp - qq - querycnt(p)) * (p + 1) <
          ans) {
        ans = sumab[i] + sumaa[pp] + sumbb[qq] + querysum(p) +
              (m - i - pp - qq - querycnt(p)) * (p + 1);
        ansi = i;
      }
    }
    if (i < cntab) {
      updatecnt(ab[i + 1].first, -1);
      updatesum(ab[i + 1].first, -ab[i + 1].first);
    }
    if (pp) {
      updatecnt(aa[pp].first, 1);
      updatesum(aa[pp].first, aa[pp].first);
    }
    if (qq) {
      updatecnt(bb[qq].first, 1);
      updatesum(bb[qq].first, bb[qq].first);
    }
  }
  if (ans == 0x7fffffff)
    printf("-1\n");
  else {
    printf("%d\n", ans);
    for (int i = 0; i <= 10000; i++) BITcnt[i] = 0;
    for (int i = 0; i <= 10000; i++) BITsum[i] = 0;
    for (int i = 1; i <= ansi; i++) {
      anss[ccc++] = ab[i].second;
      f[ab[i].second] = true;
    }
    for (int i = 1; i <= k - ansi; i++) {
      anss[ccc++] = aa[i].second;
      f[aa[i].second] = true;
    }
    for (int i = 1; i <= k - ansi; i++) {
      anss[ccc++] = bb[i].second;
      f[bb[i].second] = true;
    }
    for (int i = 1; i <= n; i++)
      if (!f[i]) {
        updatecnt(t[i], 1);
        updatesum(t[i], t[i]);
        v[t[i]].push_back(i);
      }
    if (ccc != m) {
      p = binarysearch(0, 10000, m - ccc);
      int tt = m - ccc - querycnt(p);
      for (int i = 1; i <= p; i++)
        for (int j = 0; j < v[i].size(); j++) anss[ccc++] = v[i][j];
      for (int i = 0; i < tt; i++) anss[ccc++] = v[p + 1][i];
    }
    for (int i = 0; i < ccc; i++)
      if (i != ccc - 1)
        printf("%d ", anss[i]);
      else
        printf("%d\n", anss[i]);
  }
  return 0;
}