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;
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename C, typename T = decay<decltype(*begin(declval<C>()))>,
          typename enable_if<!is_same<C, string>::value>::type * = nullptr>
ostream &operator<<(ostream &os, const C &c) {
  bool f = true;
  os << "[";
  for (const auto &x : c) {
    if (!f) os << ", ";
    f = false;
    os << x;
  }
  return os << "]";
}
template <typename T>
void debug(string s, T x) {
  cerr << s << " = " << x << "\n";
}
template <typename T, typename... Args>
void debug(string s, T x, Args... args) {
  cerr << s.substr(0, s.find(',')) << " = " << x << " | ";
  debug(s.substr(s.find(',') + 2), args...);
}
struct Book {
  int t, a, b, idx;
  bool operator<(const Book &other) const { return t < other.t; }
} book[200005];
int cnt[4 * 200005], st[4 * 200005];
void build(int p, int l, int r) {
  if (l == r) {
    st[p] = book[l].t;
    cnt[p] = 1;
    return;
  }
  int m = (l + r) / 2;
  build(2 * p, l, m);
  build(2 * p + 1, m + 1, r);
  st[p] = st[2 * p] + st[2 * p + 1];
  cnt[p] = cnt[2 * p] + cnt[2 * p + 1];
}
void update(int p, int l, int r, int idx) {
  if (l == r) {
    if (st[p]) {
      st[p] = cnt[p] = 0;
    } else {
      st[p] = book[l].t;
      cnt[p] = 1;
    }
    return;
  }
  int m = (l + r) / 2;
  if (idx <= m)
    update(2 * p, l, m, idx);
  else
    update(2 * p + 1, m + 1, r, idx);
  st[p] = st[2 * p] + st[2 * p + 1];
  cnt[p] = cnt[2 * p] + cnt[2 * p + 1];
}
int query(int p, int l, int r, int k) {
  if (k == 0) return 0;
  if (l == r) return st[p];
  int m = (l + r) / 2;
  if (cnt[2 * p] >= k)
    return query(2 * p, l, m, k);
  else
    return st[2 * p] + query(2 * p + 1, m + 1, r, k - cnt[2 * p]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> book[i].t >> book[i].a >> book[i].b;
    book[i].idx = i + 1;
  }
  sort(book, book + n);
  build(1, 0, n - 1);
  vector<pair<int, int>> alice, bob, both;
  for (int i = 0; i < n; i++) {
    if (book[i].a && book[i].b)
      both.emplace_back(book[i].t, i);
    else if (book[i].a)
      alice.emplace_back(book[i].t, i);
    else if (book[i].b)
      bob.emplace_back(book[i].t, i);
  }
  int ret = INT_MAX, retIdx = -1, retA = -1, retB = -1, retT = -1, aCnt = 0,
      bCnt = 0, ptrA = 0, ptrB = 0, cur = 0, tot = 0;
  for (int i = 0; i <= min((int)both.size(), m); i++) {
    while (aCnt < k && ptrA < alice.size()) {
      aCnt++;
      tot++;
      cur += alice[ptrA].first;
      update(1, 0, n - 1, alice[ptrA].second);
      ptrA++;
    }
    while (bCnt < k && ptrB < bob.size()) {
      bCnt++;
      tot++;
      cur += bob[ptrB].first;
      update(1, 0, n - 1, bob[ptrB].second);
      ptrB++;
    }
    while (aCnt > k && ptrA - 1 >= 0) {
      aCnt--;
      tot--;
      ptrA--;
      cur -= alice[ptrA].first;
      update(1, 0, n - 1, alice[ptrA].second);
    }
    while (bCnt > k && ptrB - 1 >= 0) {
      bCnt--;
      tot--;
      ptrB--;
      cur -= bob[ptrB].first;
      update(1, 0, n - 1, bob[ptrB].second);
    }
    if (tot <= m && aCnt >= k && bCnt >= k) {
      int calc = cur + query(1, 0, n - 1, m - tot);
      if (calc < ret) {
        ret = calc;
        retIdx = i;
        retA = ptrA;
        retB = ptrB;
        retT = tot;
      }
    }
    if (i < both.size()) {
      aCnt++;
      bCnt++;
      tot++;
      cur += both[i].first;
      update(1, 0, n - 1, both[i].second);
    }
  }
  if (retIdx == -1)
    cout << "-1\n";
  else {
    cout << ret << "\n";
    set<int> st;
    for (int i = 0; i < retIdx; i++) st.insert(book[both[i].second].idx);
    for (int i = 0; i < retA; i++) st.insert(book[alice[i].second].idx);
    for (int i = 0; i < retB; i++) st.insert(book[bob[i].second].idx);
    for (int i = 0; i < n && m - retT; i++)
      if (!st.count(book[i].idx)) {
        st.insert(book[i].idx);
        retT++;
      }
    for (int x : st) cout << x << " ";
    cout << "\n";
  }
  return 0;
}