#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long llinf = (1LL << 60);
const int inf = (1 << 30);
const int nmax = 2e5 + 50;
const long long mod = 1e9 + 7;
using namespace std;
int n, m, k, i, cur, tot, rs, ai, bi, id, tt[nmax], vl[nmax], ps[nmax], t, x, y;
vector<pair<int, int> > a, b, bt, vc;
vector<int> vec;
pair<int, int> st[4 * nmax];
void upd(int nod, int l, int r, int p, int v) {
  if (l == r) {
    if (v)
      st[nod] = make_pair(v, 1);
    else
      st[nod] = make_pair(v, 0);
    return;
  }
  int mid = (l + r) / 2;
  if (p <= mid)
    upd(2 * nod, l, mid, p, v);
  else
    upd(2 * nod + 1, mid + 1, r, p, v);
  st[nod].first = st[2 * nod].first + st[2 * nod + 1].first;
  st[nod].second = st[2 * nod].second + st[2 * nod + 1].second;
}
int qry(int nod, int l, int r, int k) {
  if (l == r) return st[nod].first;
  int mid = (l + r) / 2;
  if (st[nod * 2].second >= k)
    return qry(nod * 2, l, mid, k);
  else
    return st[nod * 2].first +
           qry(nod * 2 + 1, mid + 1, r, k - st[nod * 2].second);
}
int get(int nod, int l, int r, int tl, int tr) {
  if (tr < l || tl > r) return 0;
  if (tl <= l && r <= tr) return st[nod].first;
  int mid = (l + r) / 2;
  return get(2 * nod, l, mid, tl, tr) + get(2 * nod + 1, mid + 1, r, tl, tr);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  for (i = 1; i <= n; i++) {
    cin >> t >> x >> y;
    vc.push_back(make_pair(t, i));
    tt[i] = t;
    if (x && y)
      bt.push_back(make_pair(t, i));
    else if (x && !y)
      a.push_back(make_pair(t, i));
    else if (!x && y)
      b.push_back(make_pair(t, i));
  }
  sort(vc.begin(), vc.end());
  for (i = 0; i < n; i++) vl[vc[i].second] = i + 1, ps[i + 1] = vc[i].second;
  for (i = 1; i <= n; i++) upd(1, 1, n, vl[i], tt[i]);
  sort(bt.begin(), bt.end());
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for (i = 0; i < min((int)bt.size(), m); i++)
    upd(1, 1, n, vl[bt[i].second], 0), cur += bt[i].first;
  rs = -1;
  for (i = min(m, (int)bt.size()); i >= 0; i--) {
    if (k - i > min((int)a.size(), (int)b.size())) break;
    while (ai < k - i) {
      cur += a[ai].first;
      upd(1, 1, n, vl[a[ai].second], 0);
      ai++;
    }
    while (bi < k - i) {
      cur += b[bi].first;
      upd(1, 1, n, vl[b[bi].second], 0);
      bi++;
    }
    if (ai + bi + i > m) break;
    if (ai + bi + i != m)
      tot = qry(1, 1, n, m - (ai + bi + i));
    else
      tot = 0;
    if (rs == -1 || cur + tot < rs) {
      rs = cur + tot;
      id = i;
    }
    if (i)
      upd(1, 1, n, vl[bt[i - 1].second], tt[bt[i - 1].second]),
          cur -= bt[i - 1].first;
  }
  cout << rs << '\n';
  if (rs == -1) return 0;
  for (i = 1; i <= n; i++) upd(1, 1, n, vl[i], tt[i]);
  cur = tot = ai = bi = 0;
  for (i = 0; i < min((int)bt.size(), m); i++)
    upd(1, 1, n, vl[bt[i].second], 0), cur += bt[i].first;
  for (i = min(m, (int)bt.size()); i >= id; i--) {
    if (k - i > min((int)a.size(), (int)b.size())) break;
    while (ai < k - i) {
      cur += a[ai].first;
      upd(1, 1, n, vl[a[ai].second], 0);
      ai++;
    }
    while (bi < k - i) {
      cur += b[bi].first;
      upd(1, 1, n, vl[b[bi].second], 0);
      bi++;
    }
    if (ai + bi + i > m) break;
    if (i == id) break;
    if (i)
      upd(1, 1, n, vl[bt[i - 1].second], tt[bt[i - 1].second]),
          cur -= bt[i - 1].first;
  }
  for (i = 0; i < ai; i++) vec.push_back(a[i].second);
  for (i = 0; i < bi; i++) vec.push_back(b[i].second);
  for (i = 0; i < id; i++) vec.push_back(bt[i].second);
  cur = m - (ai + bi + id);
  for (i = 1; i <= n; i++) {
    if (!cur) break;
    if (!get(1, 1, n, i, i)) continue;
    vec.push_back(ps[i]);
    cur--;
  }
  for (i = 0; i < m; i++) cout << vec[i] << " ";
  cout << endl;
  return 0;
}

### Prompt

In cpp, your task is to solve the following 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 

### Response

```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long llinf = (1LL << 60);
const int inf = (1 << 30);
const int nmax = 2e5 + 50;
const long long mod = 1e9 + 7;
using namespace std;
int n, m, k, i, cur, tot, rs, ai, bi, id, tt[nmax], vl[nmax], ps[nmax], t, x, y;
vector<pair<int, int> > a, b, bt, vc;
vector<int> vec;
pair<int, int> st[4 * nmax];
void upd(int nod, int l, int r, int p, int v) {
  if (l == r) {
    if (v)
      st[nod] = make_pair(v, 1);
    else
      st[nod] = make_pair(v, 0);
    return;
  }
  int mid = (l + r) / 2;
  if (p <= mid)
    upd(2 * nod, l, mid, p, v);
  else
    upd(2 * nod + 1, mid + 1, r, p, v);
  st[nod].first = st[2 * nod].first + st[2 * nod + 1].first;
  st[nod].second = st[2 * nod].second + st[2 * nod + 1].second;
}
int qry(int nod, int l, int r, int k) {
  if (l == r) return st[nod].first;
  int mid = (l + r) / 2;
  if (st[nod * 2].second >= k)
    return qry(nod * 2, l, mid, k);
  else
    return st[nod * 2].first +
           qry(nod * 2 + 1, mid + 1, r, k - st[nod * 2].second);
}
int get(int nod, int l, int r, int tl, int tr) {
  if (tr < l || tl > r) return 0;
  if (tl <= l && r <= tr) return st[nod].first;
  int mid = (l + r) / 2;
  return get(2 * nod, l, mid, tl, tr) + get(2 * nod + 1, mid + 1, r, tl, tr);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  for (i = 1; i <= n; i++) {
    cin >> t >> x >> y;
    vc.push_back(make_pair(t, i));
    tt[i] = t;
    if (x && y)
      bt.push_back(make_pair(t, i));
    else if (x && !y)
      a.push_back(make_pair(t, i));
    else if (!x && y)
      b.push_back(make_pair(t, i));
  }
  sort(vc.begin(), vc.end());
  for (i = 0; i < n; i++) vl[vc[i].second] = i + 1, ps[i + 1] = vc[i].second;
  for (i = 1; i <= n; i++) upd(1, 1, n, vl[i], tt[i]);
  sort(bt.begin(), bt.end());
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for (i = 0; i < min((int)bt.size(), m); i++)
    upd(1, 1, n, vl[bt[i].second], 0), cur += bt[i].first;
  rs = -1;
  for (i = min(m, (int)bt.size()); i >= 0; i--) {
    if (k - i > min((int)a.size(), (int)b.size())) break;
    while (ai < k - i) {
      cur += a[ai].first;
      upd(1, 1, n, vl[a[ai].second], 0);
      ai++;
    }
    while (bi < k - i) {
      cur += b[bi].first;
      upd(1, 1, n, vl[b[bi].second], 0);
      bi++;
    }
    if (ai + bi + i > m) break;
    if (ai + bi + i != m)
      tot = qry(1, 1, n, m - (ai + bi + i));
    else
      tot = 0;
    if (rs == -1 || cur + tot < rs) {
      rs = cur + tot;
      id = i;
    }
    if (i)
      upd(1, 1, n, vl[bt[i - 1].second], tt[bt[i - 1].second]),
          cur -= bt[i - 1].first;
  }
  cout << rs << '\n';
  if (rs == -1) return 0;
  for (i = 1; i <= n; i++) upd(1, 1, n, vl[i], tt[i]);
  cur = tot = ai = bi = 0;
  for (i = 0; i < min((int)bt.size(), m); i++)
    upd(1, 1, n, vl[bt[i].second], 0), cur += bt[i].first;
  for (i = min(m, (int)bt.size()); i >= id; i--) {
    if (k - i > min((int)a.size(), (int)b.size())) break;
    while (ai < k - i) {
      cur += a[ai].first;
      upd(1, 1, n, vl[a[ai].second], 0);
      ai++;
    }
    while (bi < k - i) {
      cur += b[bi].first;
      upd(1, 1, n, vl[b[bi].second], 0);
      bi++;
    }
    if (ai + bi + i > m) break;
    if (i == id) break;
    if (i)
      upd(1, 1, n, vl[bt[i - 1].second], tt[bt[i - 1].second]),
          cur -= bt[i - 1].first;
  }
  for (i = 0; i < ai; i++) vec.push_back(a[i].second);
  for (i = 0; i < bi; i++) vec.push_back(b[i].second);
  for (i = 0; i < id; i++) vec.push_back(bt[i].second);
  cur = m - (ai + bi + id);
  for (i = 1; i <= n; i++) {
    if (!cur) break;
    if (!get(1, 1, n, i, i)) continue;
    vec.push_back(ps[i]);
    cur--;
  }
  for (i = 0; i < m; i++) cout << vec[i] << " ";
  cout << endl;
  return 0;
}

```