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 Arg1>
void __f(const char *name, Arg1 &&arg1) {
  std::cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
template <typename T, typename U>
static inline void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T &x, U y) {
  if (x < y) x = y;
}
long long max(long long a, long long b) { return (a > b) ? a : b; }
long long min(long long a, long long b) { return (a < b) ? a : b; }
typedef struct node {
  long long val, prior, size;
  long long sum = 0;
  struct node *l, *r;
} node;
typedef node *pnode;
long long sz(pnode t) { return t ? t->size : 0; }
long long sum(pnode t) { return t ? t->sum : 0; }
long long val(pnode t) { return t ? t->val : 0; }
void upd_sz(pnode t) {
  if (t) {
    t->size = sz(t->l) + 1 + sz(t->r);
    t->sum = sum(t->l) + t->val + sum(t->r);
  }
}
void split(pnode t, pnode &l, pnode &r, long long key) {
  if (!t)
    l = r = NULL;
  else if (t->val <= key)
    split(t->r, t->r, r, key), l = t;
  else
    split(t->l, l, t->l, key), r = t;
  upd_sz(t);
}
void merge(pnode &t, pnode l, pnode r) {
  if (!l || !r)
    t = l ? l : r;
  else if (l->prior > r->prior)
    merge(l->r, l->r, r), t = l;
  else
    merge(r->l, l, r->l), t = r;
  upd_sz(t);
}
void insert(pnode &t, pnode it) {
  if (!t)
    t = it;
  else if (it->prior > t->prior)
    split(t, it->l, it->r, it->val), t = it;
  else
    insert(t->val <= it->val ? t->r : t->l, it);
  upd_sz(t);
}
long long find(pnode t, long long v) {
  if (!t) return 0;
  if (sz(t) < v || v == 0) return 0;
  if (sz(t->l) + 1 == v) {
    return sum(t->l) + t->val;
  }
  if (sz(t->l) >= v) {
    return find(t->l, v);
  }
  return find(t->r, v - sz(t->l) - 1) + sum(t->l) + t->val;
}
void erase(pnode &t, long long key) {
  if (!t)
    return;
  else if (t->val == key) {
    pnode temp = t;
    merge(t, t->l, t->r);
    free(temp);
  } else
    erase(t->val < key ? t->r : t->l, key);
  upd_sz(t);
}
pnode init(long long val) {
  pnode ret = (pnode)malloc(sizeof(node));
  ret->val = val;
  ret->size = 1;
  ret->prior = rand();
  ret->l = ret->r = NULL;
  ret->sum = val;
  return ret;
}
void print(pnode &t) {
  if (!t) return;
  print(t->l);
  cout << t->val << " ";
  print(t->r);
}
long long solve() {
  long long n, m, k;
  cin >> n >> m >> k;
  pnode root = NULL;
  vector<pair<long long, long long> > v[4];
  long long c = 0;
  multiset<pair<long long, long long> > keep;
  long long num[n];
  for (long long i = 0; i < n; i++) {
    long long t, a, b;
    cin >> t >> a >> b;
    num[i] = t;
    keep.insert({t, i});
    if (a && b)
      v[3].push_back({t, i});
    else if (a)
      v[1].push_back({t, i});
    else if (b)
      v[2].push_back({t, i});
    else {
      c++;
      insert(root, init(t));
    }
  }
  for (long long i = 1; i <= 3; i++) {
    sort(v[i].begin(), v[i].end());
    for (long long j = k; j < v[i].size(); j++) {
      c++;
      insert(root, init(v[i][j].first));
    }
    for (long long j = 1; j < v[i].size(); j++) {
      v[i][j].first += v[i][j - 1].first;
    }
  }
  long long ans = 1e18, b = -1;
  for (long long i = 0; i <= min(v[3].size(), k); i++) {
    long long nk = k - i;
    long long nm = m - i;
    long long left = nm;
    long long sum = 0;
    if (i) sum += v[3][i - 1].first;
    if (v[1].size() >= nk && v[2].size() >= nk & nk > 0) {
      sum += v[1][nk - 1].first + v[2][nk - 1].first;
      left -= 2 * nk;
    }
    if (left > 0 && c >= left) sum += find(root, left), left = 0;
    if (left == 0 && min(v[1].size(), v[2].size()) + i >= k) {
      if (ans > sum) {
        ans = sum;
        b = i;
      }
    }
    if (nk > 0) {
      if (v[1].size() >= nk) insert(root, init(num[v[1][nk - 1].second])), c++;
      if (v[2].size() >= nk) insert(root, init(num[v[2][nk - 1].second])), c++;
    }
  }
  if (b == -1) {
    cout << -1 << '\n';
    return 0;
  }
  cout << ans << '\n';
  for (long long i = 0; i < b; i++)
    cout << v[3][i].second + 1 << " ",
        keep.erase({num[v[3][i].second], v[3][i].second});
  long long nk = k - b;
  nk = max(0, nk);
  for (long long i = 0; i < nk; i++)
    cout << v[1][i].second + 1 << " " << v[2][i].second + 1 << " ",
        keep.erase({num[v[2][i].second], v[2][i].second}),
        keep.erase({num[v[1][i].second], v[1][i].second});
  long long left = m - b - 2 * nk;
  while (left > 0) {
    left--;
    cout << keep.begin()->second + 1 << " ";
    keep.erase(keep.begin());
  }
  return 0;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}