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;
const int inf = 0x7fffffff;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
int n, m, K;
vector<pair<int, int> > a[4];
vector<int> s[4];
int b[200020], len;
vector<int> book;
struct SGT {
  const double alpha = 0.78;
  int cnt, rt;
  SGT() { cnt = 0; }
  struct node {
    int ch[2], siz, sum, val;
    int id;
  } t[200020];
  inline int NewNode(pair<int, int> data) {
    int u = ++cnt;
    t[u].siz = 1;
    t[u].id = data.second;
    t[u].sum = t[u].val = data.first;
    return u;
  }
  inline void update(int u) {
    t[u].siz = t[t[u].ch[0]].siz + t[t[u].ch[1]].siz + 1;
    t[u].sum = t[t[u].ch[0]].sum + t[t[u].ch[1]].sum + t[u].val;
  }
  inline bool isbad(int u) {
    return t[u].siz * alpha <= max(t[t[u].ch[0]].siz, t[t[u].ch[1]].siz);
  }
  void dfs(int u) {
    if (!u) return;
    dfs(t[u].ch[0]);
    b[++len] = u;
    dfs(t[u].ch[1]);
  }
  void build(int l, int r, int &u) {
    if (l > r) return (void)(u = 0);
    int mid = (l + r) >> 1;
    u = b[mid];
    build(l, mid - 1, t[u].ch[0]);
    build(mid + 1, r, t[u].ch[1]);
    update(u);
  }
  void rebuild(int &u) {
    len = 0;
    dfs(u);
    build(1, len, u);
  }
  int Ask(int u, int nd) {
    if (!u || !nd) return 0;
    if (t[t[u].ch[0]].siz + 1 <= nd)
      return t[t[u].ch[0]].sum + t[u].val +
             Ask(t[u].ch[1], nd - 1 - t[t[u].ch[0]].siz);
    return Ask(t[u].ch[0], nd);
  }
  void Insert(int &u, pair<int, int> val) {
    if (!u) return (void)(u = NewNode(val));
    val.first <= t[u].val ? Insert(t[u].ch[0], val) : Insert(t[u].ch[1], val);
    if (isbad(u)) rebuild(u);
    update(u);
  }
} T;
int main() {
  n = read(), m = read(), K = read();
  for (int i = 1; i <= n; ++i) {
    int t = read(), x = read(), y = read();
    a[x << 1 | y].emplace_back(t, i);
  }
  for (int i = 0; i < 4; ++i) {
    sort(a[i].begin(), a[i].end());
    s[i].resize(a[i].size());
    if (!a[i].empty()) {
      s[i][0] = a[i][0].first;
      for (int j = 1; j < (int)a[i].size(); ++j) {
        s[i][j] = a[i][j].first + s[i][j - 1];
      }
    }
  }
  for (auto x : a[0]) {
    T.Insert(T.rt, x);
  }
  for (int i = K + 1; i < (int)a[1].size(); ++i) {
    T.Insert(T.rt, a[1][i]);
  }
  for (int i = K + 1; i < (int)a[2].size(); ++i) {
    T.Insert(T.rt, a[2][i]);
  }
  int ans = inf, pos = -1;
  for (int i = 0; i <= min(m, (int)a[3].size()); ++i) {
    if (i <= K && K - i < (int)a[1].size()) {
      T.Insert(T.rt, a[1][K - i]);
    }
    if (i <= K && K - i < (int)a[2].size()) {
      T.Insert(T.rt, a[2][K - i]);
    }
    int w = 0;
    if (i < K) {
      if (m - 2 * K + i < 0) continue;
      if ((int)a[1].size() < K - i) continue;
      if ((int)a[2].size() < K - i) continue;
      if (T.t[T.rt].siz < m - 2 * K + i) continue;
      w = s[1][K - i - 1] + s[2][K - i - 1] + (!i ? 0 : s[3][i - 1]) +
          T.Ask(T.rt, m - 2 * K + i);
    } else {
      if (T.t[T.rt].siz < m - i) continue;
      w = (!i ? 0 : s[3][i - 1]) + T.Ask(T.rt, m - i);
    }
    if (w < ans) {
      ans = w;
      pos = i;
    }
  }
  if (ans == inf) {
    return !printf("-1\n");
  }
  for (int i = 0; i < pos; ++i) {
    book.push_back(a[3][i].second);
  }
  static vector<pair<int, int> > tmp;
  tmp = a[0];
  int sz = 0;
  if (pos < K) {
    for (int i = 0; i <= K - pos - 1; ++i) {
      book.push_back(a[1][i].second);
      book.push_back(a[2][i].second);
    }
    for (int i = K - pos; i < (int)a[1].size(); ++i) tmp.push_back(a[1][i]);
    for (int i = K - pos; i < (int)a[2].size(); ++i) tmp.push_back(a[2][i]);
    sz = m - 2 * K + pos;
  } else {
    tmp.insert(tmp.end(), a[1].begin(), a[1].end());
    tmp.insert(tmp.end(), a[2].begin(), a[2].end());
    sz = m - pos;
  }
  sort(tmp.begin(), tmp.end());
  for (int i = 0; i < sz; ++i) {
    book.push_back(tmp[i].second);
  }
  printf("%d\n", ans);
  for (auto x : book) {
    printf("%d ", x);
  }
  printf("\n");
  return 0;
}