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;
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long modInverse(long long a, long long p) { return power(a, p - 2, p); }
void gcd(long long a, long long b, long long& d, long long& u, long long& v) {
  if (b == 0) {
    d = a;
    u = 1;
    v = 0;
  } else {
    gcd(b, a % b, d, v, u);
    v -= a / b * u;
  }
}
long long Inverse(long long a, long long m) {
  a %= m;
  if (a < 0) a += m;
  long long d, u, v;
  gcd(a, m, d, u, v);
  assert(d == 1);
  u %= m;
  if (u < 0) u += m;
  return u;
}
long long gcd(long long x, long long y) {
  if (x == 0 || y == 0) {
    return max(y, x);
  }
  return gcd(y % x, x);
}
long long gcdExtended(long long a, long long b, long long& x, long long& y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long x1, y1;
  long long gcd = gcdExtended(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return gcd;
};
int main() {
  long long n, m, k;
  scanf("%lld %lld %lld", &(n), &(m), &(k));
  vector<pair<long long, long long>> both, a, b, none;
  for (long long i = 1; i <= n; i++) {
    long long t, a1, b1;
    scanf("%lld %lld %lld", &(t), &(a1), &(b1));
    ;
    if (a1 & b1)
      both.push_back({t, i});
    else if (a1)
      a.push_back({t, i});
    else if (b1)
      b.push_back({t, i});
    else
      none.push_back({t, i});
  }
  sort(none.begin(), none.end());
  sort(both.begin(), both.end());
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  long long s1 = both.size(), s2 = a.size(), s3 = b.size(), s4 = none.size();
  long long cnt = -1;
  if (s1 + s2 < k || s1 + s3 < k) {
    cout << -1 << endl;
    return 0;
  }
  while (true) {
    cnt++;
    if (cnt > m || cnt > s1) break;
    if (k > cnt) {
      if (k - cnt <= s2 && k - cnt <= s3 && s2 + s3 + s4 >= m - cnt &&
          m >= 2 * k - cnt) {
        break;
      }
    } else {
      if (s2 + s3 + s4 >= m - cnt && m >= 2 * k - cnt) {
        break;
      }
    }
  }
  if (false) cerr << cnt << endl;
  if (cnt < 0 || cnt > m || cnt > s1) {
    cout << -1 << endl;
    return 0;
  }
  long long ptr = k - cnt;
  if (ptr < 0) ptr = 0;
  assert(s2 + s3 + s4 >= m - cnt);
  if (false) cerr << ptr << endl;
  priority_queue<pair<long long, long long>> q;
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      free;
  long long sum = 0;
  for (long long i = 0; i < cnt; i++) sum += both[i].first;
  for (long long i = 0; i < ptr; i++) sum += a[i].first;
  for (long long i = 0; i < ptr; i++) sum += b[i].first;
  long long taken = cnt + ptr + ptr;
  if (false) cerr << taken << endl;
  for (long long i = ptr; i < s2; i++) free.push(a[i]);
  for (long long i = ptr; i < s3; i++) free.push(b[i]);
  for (long long i = 0; i < s4; i++) free.push(none[i]);
  while (taken < m) {
    sum += free.top().first;
    q.push(free.top());
    free.pop();
    taken++;
  }
  if (false) cerr << sum << " " << taken << endl;
  assert(taken == m);
  if (false)
    cerr << free.size() << " " << q.size() << " " << q.size() + 2 * k - cnt
         << endl;
  long long ans = sum;
  long long index = cnt;
  while (cnt <= min(m, s1)) {
    if (ptr == 0) {
      cnt++;
      if (cnt > m || cnt > s1) break;
      sum += both[cnt - 1].first;
      if (q.size()) {
        sum -= q.top().first;
        q.pop();
      }
      if (ans > sum) {
        ans = sum;
        index = cnt;
      }
      continue;
    }
    cnt++;
    if (cnt > m || cnt > s1) break;
    sum += both[cnt - 1].first;
    long long re1 = a[ptr - 1].first, re2 = b[ptr - 1].first;
    q.push(a[ptr - 1]);
    q.push(b[ptr - 1]);
    while (free.size() && q.top().first > free.top().first) {
      sum += (free.top().first - q.top().first);
      auto t1 = q.top();
      q.pop();
      auto t2 = free.top();
      free.pop();
      q.push(t2);
      free.push(t1);
    }
    sum -= q.top().first;
    free.push(q.top());
    q.pop();
    if (ans > sum) {
      ans = sum;
      index = cnt;
    }
    ptr--;
  }
  cout << ans << endl;
  sum = 0;
  while (free.size()) free.pop();
  for (long long i = 0; i < index; i++) {
    cout << both[i].second << " ";
    sum += both[i].first;
  }
  for (long long i = 0; i < max(0LL, k - index); i++) {
    cout << a[i].second << " " << b[i].second << " ";
    sum += a[i].first;
    sum += b[i].first;
  }
  taken = index + 2 * max(0LL, k - index);
  for (long long i = max(0LL, k - index); i < s2; i++) free.push(a[i]);
  for (long long i = max(0LL, k - index); i < s3; i++) free.push(b[i]);
  for (long long i = 0; i < s4; i++) free.push(none[i]);
  while (taken < m) {
    cout << free.top().second << " ";
    taken++;
    sum += free.top().first;
    free.pop();
  }
  assert(sum == ans);
}