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 exactly m 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 exactly m 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 m 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 three integers n, m and k (1 ≤ k ≤ m ≤ 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.

If the solution exists, print T in the first line — the minimum total reading time of the suitable set of books. In the second line print m distinct integers from 1 to n in any order — indices of books which are in the set you found.

If there are several answers, print any of them.

Examples

Input

6 3 1
6 0 0
11 1 0
9 0 1
21 1 1
10 1 0
8 0 1


Output

24
6 5 1 

Input

6 3 2
6 0 0
11 1 0
9 0 1
21 1 1
10 1 0
8 0 1


Output

39
4 6 5 

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<long long> sh, sp, rd, ot;
vector<pair<long long, long long> > shs, sps, rds, ots;
long long ax, bx, cx, viva;
long long arr[200005];
long long sum(long long i, long long j, long long s) {
  i = max(i, -1LL);
  j = max(j, -1LL);
  for (long long k = 20; k >= 0; k--) {
    long long x = i + (1 << k);
    if (x < shs.size() && shs[x].first <= s) {
      i = x;
    }
  }
  for (long long k = 20; k >= 0; k--) {
    long long x = j + (1 << k);
    if (x < sps.size() && sps[x].first <= s) {
      j = x;
    }
  }
  long long l = -1;
  for (long long k = 20; k >= 0; k--) {
    long long x = l + (1 << k);
    if (x < ots.size() && ots[x].first <= s) {
      l = x;
    }
  }
  ax = i;
  bx = j;
  cx = l;
  return i + 1 + j + 1 + l + 1;
}
long long get(long long i, long long j, long long tot) {
  i = max(i, 0LL);
  j = max(j, 0LL);
  if (i > sh.size()) return 1000000000000000000;
  if (j > sp.size()) return 1000000000000000000;
  if (i + j > tot) return 1000000000000000000;
  if (tot > sp.size() + sh.size() + ot.size()) return 1000000000000000000;
  if (tot <= 0) return 0;
  if (i + j == tot) {
    long long ret = 0;
    if (i) ret += sh[i - 1];
    if (j) ret += sp[j - 1];
    return ret;
  }
  long long x = 0;
  for (long long k = 20; k >= 0; k--) {
    long long cur = x + (1 << k);
    if (sum(i - 1, j - 1, cur) <= tot) {
      x = cur;
    }
  }
  viva = x;
  long long ans = 0;
  ans += (tot - sum(i - 1, j - 1, x)) * (x + 1);
  if (ax >= 0) ans += sh[ax];
  if (bx >= 0) ans += sp[bx];
  if (cx >= 0) ans += ot[cx];
  return ans;
}
void solve() {
  long long i, j, k, l, r, m, n, a, b, c, d, x, y, z, t;
  cin >> n >> m >> k;
  ax = bx = cx = -1;
  for (i = 0; i < n; i++) {
    cin >> x >> y >> z;
    if (y && z) {
      rd.push_back(x);
      rds.push_back({x, i + 1});
    } else if (y) {
      sh.push_back(x);
      shs.push_back({x, i + 1});
    } else if (z) {
      sp.push_back(x);
      sps.push_back({x, i + 1});
    } else {
      ot.push_back(x);
      ots.push_back({x, i + 1});
    }
    arr[i + 1] = x;
  }
  sort(rd.begin(), rd.end());
  sort(sh.begin(), sh.end());
  sort(sp.begin(), sp.end());
  sort(ot.begin(), ot.end());
  sort(rds.begin(), rds.end());
  sort(shs.begin(), shs.end());
  sort(sps.begin(), sps.end());
  sort(ots.begin(), ots.end());
  for (i = 1; i < rd.size(); i++) rd[i] += rd[i - 1];
  for (i = 1; i < sh.size(); i++) sh[i] += sh[i - 1];
  for (i = 1; i < sp.size(); i++) sp[i] += sp[i - 1];
  for (i = 1; i < ot.size(); i++) ot[i] += ot[i - 1];
  long long ans = get(k, k, m);
  long long flag = 0;
  for (i = 0; i < rd.size(); i++) {
    if (i + 1 > m) continue;
    long long cur = rd[i] + get(k - i - 1, k - i - 1, m - i - 1);
    if (cur < ans) {
      ans = cur;
      flag = 1;
      x = i;
    }
  }
  if (ans >= 1000000000000000) {
    cout << -1 << endl;
    return;
  }
  vector<long long> numb;
  if (flag) {
    get(k - x - 1, k - x - 1, m - i - 1);
    for (i = 0; i <= x; i++) {
      numb.push_back(rds[i].second);
    }
    sum(k - x - 2, k - x - 2, viva);
  } else {
    get(k, k, m);
    sum(k - 1, k - 1, viva);
  }
  for (i = 0; i <= ax; i++) numb.push_back(shs[i].second);
  for (i = 0; i <= bx; i++) numb.push_back(sps[i].second);
  for (i = 0; i <= cx; i++) numb.push_back(ots[i].second);
  if (flag)
    assert(numb.size() == (ax + bx + cx + x + 4));
  else
    assert(numb.size() == (ax + bx + cx + 3));
  vector<pair<long long, long long> > baal;
  for (i = ax + 1; i < shs.size(); i++) {
    baal.push_back(shs[i]);
  }
  for (i = bx + 1; i < sps.size(); i++) {
    baal.push_back(sps[i]);
  }
  for (i = cx + 1; i < ots.size(); i++) {
    baal.push_back(ots[i]);
  }
  sort(baal.begin(), baal.end());
  for (i = 0; i < baal.size(); i++) {
    if (numb.size() < m) {
      numb.push_back(baal[i].second);
    }
  }
  cout << ans << endl;
  for (auto &cv : numb) {
    cout << cv << ' ';
  }
  cout << endl;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long t = 1;
  while (t--) solve();
  return 0;
}