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;
const long double PI = acos(-1);
const long long MOD = 1000000000 + 7;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long add(long long a, long long b, long long m = MOD) {
  if (a >= m) a %= m;
  if (b >= m) b %= m;
  if (a < 0) a += m;
  if (b < 0) b += m;
  long long res = a + b;
  if (res >= m or res <= -m) res %= m;
  if (res < 0) res += m;
  return res;
}
long long mul(long long a, long long b, long long m = MOD) {
  if (a >= m) a %= m;
  if (b >= m) b %= m;
  if (a < 0) a += m;
  if (b < 0) b += m;
  long long res = a * b;
  if (res >= m or res <= -m) res %= m;
  if (res < 0) res += m;
  return res;
}
long long pow_mod(long long a, long long b, long long m = MOD) {
  long long res = 1LL;
  a = a % m;
  while (b) {
    if (b & 1) res = mul(res, a, m);
    b >>= 1;
    a = mul(a, a, m);
  }
  return res;
}
long long fastexp(long long a, long long b) {
  long long res = 1LL;
  while (b) {
    if (b & 1) res = res * a;
    b >>= 1;
    a *= a;
  }
  return res;
}
int gcdExtendido(int a, int b, int *x, int *y) {
  if (a == 0) {
    *x = 0;
    *y = 1;
    return b;
  }
  int x1, y1;
  int gcd = gcdExtendido(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
int modInverso(int a, int m) {
  int x, y;
  int g = gcdExtendido(a, m, &x, &y);
  if (g != 1)
    return -1;
  else
    return (x % m + m) % m;
}
const int N = 200000 + 5;
const long long inf = 1e5;
int n, m, k;
int a[N];
int b[N];
int t[N];
int len;
int pos[N];
long long st[4 * N];
int cnt[4 * N];
bool none[N];
vector<int> justA, justB, both, extras;
void build(int pos = 1, int l = 0, int r = len) {
  if (l == r) {
    st[pos] = (l and none[extras[l - 1]]) ? t[extras[l - 1]] : 0;
    cnt[pos] = (st[pos] > 0);
    return;
  }
  int mi = (l + r) / 2;
  build(2 * pos, l, mi);
  build(2 * pos + 1, mi + 1, r);
  st[pos] = st[2 * pos] + st[2 * pos + 1];
  cnt[pos] = cnt[2 * pos] + cnt[2 * pos + 1];
}
void update(int x, int y, int pos = 1, int l = 0, int r = len) {
  if (l == r) {
    st[pos] = y;
    cnt[pos] = (y > 0);
    return;
  }
  int mi = (l + r) / 2;
  if (l <= x and x <= mi)
    update(x, y, 2 * pos, l, mi);
  else
    update(x, y, 2 * pos + 1, mi + 1, r);
  st[pos] = st[2 * pos] + st[2 * pos + 1];
  cnt[pos] = cnt[2 * pos] + cnt[2 * pos + 1];
}
long long query(int x, int y, int pos = 1, int l = 0, int r = len) {
  if (y < l or r < x or x > y) return 0LL;
  if (x <= l and r <= y) return st[pos];
  int mi = (l + r) / 2;
  return query(x, y, 2 * pos, l, mi) + query(x, y, 2 * pos + 1, mi + 1, r);
}
int solve(int k, int pos = 1, int l = 0, int r = len) {
  if (cnt[pos] < k) return -1;
  if (l == r) return l;
  int mi = (l + r) / 2;
  if (cnt[2 * pos] >= k)
    return solve(k, 2 * pos, l, mi);
  else
    return solve(k - cnt[2 * pos], 2 * pos + 1, mi + 1, r);
}
vector<long long> getPrefix(vector<int> &v) {
  vector<long long> ans(1, 0LL);
  for (int i = 0; i < v.size(); i++) {
    ans.emplace_back(ans.back() + t[v[i]]);
  }
  return ans;
}
vector<int> merge(vector<int> &a, vector<int> &b) {
  vector<int> ans;
  int p1 = 0, p2 = 0;
  while (p1 < a.size() and p2 < b.size()) {
    if (t[a[p1]] < t[b[p2]]) {
      ans.emplace_back(a[p1]);
      p1 += 1;
    } else {
      ans.emplace_back(b[p2]);
      p2 += 1;
    }
  }
  while (p1 < a.size()) {
    ans.emplace_back(a[p1]);
    p1 += 1;
  }
  while (p2 < b.size()) {
    ans.emplace_back(b[p2]);
    p2 += 1;
  }
  return ans;
}
vector<int> getAnswer(int bestid) {
  vector<int> ans;
  for (int i = 0; i < bestid; i++) {
    ans.emplace_back(both[i]);
  }
  int wantA = max(0, k - bestid);
  int wantB = max(0, k - bestid);
  for (int i = 0; i < wantA; i++) {
    ans.emplace_back(justA[i]);
  }
  for (int i = 0; i < wantB; i++) {
    ans.emplace_back(justB[i]);
  }
  build();
  for (int i = wantA; i < justA.size(); i++) {
    int x = justA[i];
    update(pos[x] + 1, t[x]);
  }
  for (int i = wantB; i < justB.size(); i++) {
    int x = justB[i];
    update(pos[x] + 1, t[x]);
  }
  int extra = m - bestid - wantB - wantA;
  for (int i = 0; i < extra; i++) {
    int p = solve(1);
    ans.emplace_back(extras[p - 1]);
    update(p, 0);
  }
  return ans;
}
int main() {
  scanf("%d %d %d", &(n), &(m), &(k));
  for (int i = 1; i <= n; i++) {
    scanf("%d %d %d", &(t[i]), &(a[i]), &(b[i]));
    if (a[i] and !b[i]) justA.emplace_back(i);
    if (!a[i] and b[i]) justB.emplace_back(i);
    if (a[i] and b[i]) both.emplace_back(i);
    if (!a[i] and !b[i]) {
      extras.emplace_back(i);
      none[i] = true;
    }
  }
  sort((justA).begin(), (justA).end(),
       [&](int i, int j) { return t[i] < t[j]; });
  sort((justB).begin(), (justB).end(),
       [&](int i, int j) { return t[i] < t[j]; });
  sort((both).begin(), (both).end(), [&](int i, int j) { return t[i] < t[j]; });
  sort((extras).begin(), (extras).end(),
       [&](int i, int j) { return t[i] < t[j]; });
  extras = merge(extras, justA);
  extras = merge(extras, justB);
  len = extras.size();
  for (int i = 0; i < extras.size(); i++) {
    pos[extras[i]] = i;
  }
  build();
  vector<long long> bothP = getPrefix(both);
  vector<long long> justAP = getPrefix(justA);
  vector<long long> justBP = getPrefix(justB);
  long long best = LLONG_MAX;
  int bestid = -1;
  int posA = justA.size() - 1;
  int posB = justB.size() - 1;
  for (int i = 0; i < bothP.size(); i++) {
    int wantA = max(0, k - i);
    int wantB = max(0, k - i);
    if (wantA >= justAP.size()) continue;
    if (wantB >= justBP.size()) continue;
    while (posA >= wantA) {
      int x = justA[posA];
      update(pos[x] + 1, t[x]);
      posA -= 1;
    }
    while (posB >= wantB) {
      int x = justB[posB];
      update(pos[x] + 1, t[x]);
      posB -= 1;
    }
    int suma = i + wantA + wantB;
    if (suma > m) continue;
    int need = solve(m - suma);
    if (need == -1) continue;
    long long cur = bothP[i] + justAP[wantA] + justBP[wantB] + query(0, need);
    if (cur < best) {
      best = cur;
      bestid = i;
    }
  }
  if (best == LLONG_MAX) best = -1;
  printf("%lld\n", best);
  if (best >= 0) {
    vector<int> ans = getAnswer(bestid);
    for (int i = 0; i < ans.size(); i++) {
      printf("%d%c", ans[i], " \n"[i + 1 == ans.size()]);
    }
  }
  return 0;
}