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;
using ll = long long;
const int INF = 1000000000;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
struct IOSetup {
  IOSetup() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(12);
  }
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << p.first << " " << p.second;
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++)
    os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &x : v) is >> x;
  return is;
}
template <typename T>
struct BinaryIndexedTree {
  vector<T> data;
  BinaryIndexedTree() = default;
  BinaryIndexedTree(int sz) : data(sz + 1, 0) {}
  BinaryIndexedTree(const vector<T> &a) : data(a.size() + 1, 0) {
    for (int i = 0; i < (int)a.size(); i++) data[i + 1] = a[i];
    for (int i = 1; i < (int)data.size(); i++) {
      int j = i + (i & -i);
      if (j < (int)data.size()) data[j] += data[i];
    }
  }
  void add(int k, const T &x) {
    for (++k; k < (int)data.size(); k += (k & -k)) data[k] += x;
  }
  T sum(int k) {
    T ret = T();
    for (; k > 0; k -= (k & -k)) ret += data[k];
    return ret;
  }
  T query(int l, int r) { return sum(r) - sum(l); }
  T operator[](const int &k) { return query(k, k + 1); }
  int lower_bound(T x) {
    int r = 1, i = 0;
    while (r < (int)data.size()) r <<= 1;
    for (; r > 0; r >>= 1)
      if (i + r < (int)data.size() and data[i + r] < x) {
        x -= data[i + r];
        i += r;
      }
    return i;
  }
};
template <typename T>
struct kSumOffline {
  int sz;
  map<T, int> comp;
  vector<T> v;
  BinaryIndexedTree<T> bc, bv;
  kSumOffline(vector<T> _v, bool greater = false) : v(_v) {
    sort(v.begin(), v.end());
    v.erase(unique(begin(v), end(v)), end(v));
    if (greater) reverse(v.begin(), v.end());
    sz = (int)v.size();
    for (int i = 0; i < (sz); i++) comp[v[i]] = i;
    bc = BinaryIndexedTree<T>(sz);
    bv = BinaryIndexedTree<T>(sz);
  }
  void add(T x, int cnt = 1) {
    assert(comp.find(x) != comp.end());
    bc.add(comp[x], cnt);
    bv.add(comp[x], x * cnt);
  }
  void sub(T x, int cnt = 1) {
    assert(comp.find(x) != comp.end());
    bc.add(comp[x], -cnt);
    bv.add(comp[x], -x * cnt);
  }
  T get(int k) {
    if (k == 0) return (T());
    if (bc.sum(sz) < k) return bv.sum(sz);
    int x = bc.lower_bound(k);
    int rem = bc.sum(x + 1) - k;
    return bv.sum(x + 1) - v[x] * rem;
  }
};
signed main() {
  int n, m, k;
  cin >> n >> m >> k;
  using P = pair<ll, int>;
  vector<ll> w;
  vector<P> v[2][2];
  for (int i = 0; i < (n); i++) {
    ll t;
    int a, b;
    cin >> t >> a >> b;
    v[a][b].push_back(P(t, i));
    w.push_back(t);
  }
  kSumOffline<ll> sum(w);
  for (int i = 0; i < (2); i++)
    for (int j = 0; j < (2); j++)
      if (i + j) sort(v[i][j].begin(), v[i][j].end());
  while (v[0][1].size() > v[1][0].size()) {
    v[0][0].push_back(v[0][1].back());
    v[0][1].pop_back();
  }
  while (v[1][0].size() > v[0][1].size()) {
    v[0][0].push_back(v[1][0].back());
    v[1][0].pop_back();
  }
  sort(v[0][0].begin(), v[0][0].end());
  vector<ll> s[3];
  for (int i = 0; i < (3); i++) s[i].push_back(0);
  for (int i = 0; i < ((int)v[0][0].size()); i++)
    s[0].push_back(s[0].back() + v[0][0][i].first);
  for (int i = 0; i < ((int)v[1][1].size()); i++)
    s[2].push_back(s[2].back() + v[1][1][i].first);
  for (int i = 0; i < ((int)v[1][0].size()); i++)
    s[1].push_back(s[1].back() + v[1][0][i].first + v[0][1][i].first);
  for (int i = 0; i < ((int)v[0][0].size()); i++) sum.add(v[0][0][i].first);
  for (auto &p : v[1][1]) sum.add(p.first);
  ll cost = LINF;
  int ri = -1;
  for (int i = (int)v[1][0].size(), j = 0; i >= 0; i--) {
    int t = k - i;
    while (min(t, (int)v[1][1].size()) > j) {
      sum.sub(v[1][1][j].first);
      j++;
    }
    int u = m - t - 2 * i;
    if (0 <= t and t < (int)s[2].size() and 0 <= u and
        sum.bc.query(0, sum.sz) >= u) {
      if (chmin(cost, s[1][i] + s[2][t] + sum.get(u))) ri = i;
    }
    if (i) {
      sum.add(v[0][1][i - 1].first);
      sum.add(v[1][0][i - 1].first);
    }
  }
  if (ri < 0) {
    cout << -1 << endl;
    return 0;
  }
  cout << cost << endl;
  int t = k - ri;
  int u = m - t - 2 * ri;
  vector<int> ans;
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 0; i < (ri); i++) {
    ans.push_back(v[1][0][i].second + 1);
    ans.push_back(v[0][1][i].second + 1);
  }
  for (int i = ri; i < (int)v[1][0].size(); i++) {
    que.push(v[1][0][i]);
    que.push(v[0][1][i]);
  }
  for (int i = 0; i < (t); i++) ans.push_back(v[1][1][i].second + 1);
  for (int i = t; i < (int)v[1][1].size(); i++) que.push(v[1][1][i]);
  for (int i = 0; i < ((int)v[0][0].size()); i++) que.push(v[0][0][i]);
  while (u--) {
    ans.push_back(que.top().second + 1);
    que.pop();
  }
  cout << ans << endl;
  return 0;
}