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 = 2147483647;
const int mod = 1000000007;
const int mod2 = 998244353;
const double eps = 1e-9;
const double pi = acos(-1.0);
inline int rint() {
  int x;
  scanf("%d", &x);
  return x;
}
inline long long rll() {
  long long x;
  scanf("%lld", &x);
  return x;
}
inline long long ri64() {
  long long x;
  scanf("%I64d", &x);
  return x;
}
void rvi(vector<int> &a, int n) {
  for (int i = 0; i < n; i++) {
    a.push_back(rint());
  }
}
void rvll(vector<long long> &a, int n) {
  for (int i = 0; i < n; i++) {
    a.push_back(rll());
  }
}
void showvi(vector<int> &a) {
  for (int i = 0; i < a.size(); i++) {
    printf("%d%c", a[i], (i + 1 == a.size() ? '\n' : ' '));
  }
}
void showvll(vector<long long> &a) {
  for (int i = 0; i < a.size(); i++) {
    printf("%lld%c", a[i], (i + 1 == a.size() ? '\n' : ' '));
  }
}
void showviln(vector<int> &a) {
  for (int i = 0; i < a.size(); i++) {
    printf("%d%c", a[i], '\n');
  }
}
void showvllln(vector<long long> &a) {
  for (int i = 0; i < a.size(); i++) {
    printf("%lld%c", a[i], '\n');
  }
}
void showi(int x) { printf("%d", x); }
void cfinit() {
  ios::sync_with_stdio(false);
  cin.tie(0);
}
using namespace std;
struct node {
  int t;
  int a;
  int b;
  int ind;
} ns[200022];
int n, m, k;
bool cmp(node &a, node &b) {
  if (a.t != b.t) return a.t < b.t;
  return a.ind < b.ind;
}
bool check(int x, bool show) {
  int n00 = 0;
  int n10 = 0;
  int n01 = 0;
  int n11 = 0;
  for (int i = 0; i < (x); i++) {
    if (ns[i].a && ns[i].b)
      n11++;
    else if (ns[i].a)
      n10++;
    else if (ns[i].b)
      n01++;
    else
      n00++;
  }
  if (n11 > m) return true;
  int fk = 0;
  int n0 = 0;
  int n1 = 0;
  fk += n11;
  n0 += n11;
  n1 += n11;
  if (n0 < k) {
    int rm = k - n0;
    if (n10 < rm) return false;
    fk += rm;
    n0 = k;
  }
  if (n1 < k) {
    int rm = k - n1;
    if (n01 < rm) return false;
    fk += rm;
    n1 = k;
  }
  if (fk <= m) return true;
  return false;
}
vector<int> n00;
vector<int> n10;
vector<int> n01;
vector<int> n11;
int pos00, pos10, pos01, pos11;
long long sum, now, mi;
int ans00, ans01, ans10, ans11;
int now0, now1;
void push() {
  int fk = -1;
  if (pos00 != n00.size()) fk = 0;
  if (pos01 != n01.size()) {
    if (fk == -1)
      fk = 1;
    else if (ns[n01[pos01]].t < ns[n00[pos00]].t)
      fk = 1;
  }
  if (pos10 != n10.size()) {
    if (fk == -1)
      fk = 2;
    else if (fk == 0 && ns[n10[pos10]].t < ns[n00[pos00]].t)
      fk = 2;
    else if (fk == 1 && ns[n10[pos10]].t < ns[n01[pos01]].t)
      fk = 2;
  }
  if (fk == 0) {
    sum += ns[n00[pos00]].t;
    pos00++;
  }
  if (fk == 1) {
    now1++;
    sum += ns[n01[pos01]].t;
    pos01++;
  }
  if (fk == 2) {
    now0++;
    sum += ns[n10[pos10]].t;
    pos10++;
  }
  now++;
}
int main() {
  cfinit();
  cin >> n >> m >> k;
  for (int i = 0; i < (n); i++) cin >> ns[i].t >> ns[i].a >> ns[i].b;
  for (int i = 0; i < (n); i++) ns[i].ind = i + 1;
  sort(ns, ns + n, cmp);
  if (!check(n, false)) {
    cout << -1 << '\n';
    return 0;
  }
  for (int i = 0; i < (n); i++) {
    if (ns[i].a && ns[i].b)
      n11.push_back(i);
    else if (ns[i].a)
      n10.push_back(i);
    else if (ns[i].b)
      n01.push_back(i);
    else
      n00.push_back(i);
  }
  int len = min(m, (int)n11.size());
  pos00 = 0;
  pos01 = 0;
  pos10 = 0;
  pos11 = len;
  sum = 0;
  now = len;
  now0 = now;
  now1 = now;
  mi = 10000000000000LL;
  ans00 = -1;
  ans01 = -1;
  ans10 = -1;
  ans11 = -1;
  for (int i = 0; i < (len); i++) sum += ns[n11[i]].t;
  bool flag = true;
  for (int i = len; i >= 0; i--) {
    while (now0 < k) {
      if (pos10 == n10.size()) {
        flag = false;
        break;
      }
      sum += ns[n10[pos10]].t;
      pos10++;
      now0++;
      now++;
    }
    if (!flag) break;
    while (now1 < k) {
      if (pos01 == n01.size()) {
        flag = false;
        break;
      }
      sum += ns[n01[pos01]].t;
      pos01++;
      now1++;
      now++;
    }
    if (!flag) break;
    while (now < m) {
      if (pos10 == n10.size() && pos01 == n01.size() && pos00 == n00.size()) {
        flag = false;
        break;
      }
      push();
    }
    if (!flag) break;
    if (now > m) {
      int rm = now - m;
      if (pos00 < rm) break;
      for (int j = 0; j < (rm); j++) {
        pos00--;
        now--;
        sum -= ns[n00[pos00]].t;
      }
    }
    if (sum < mi) {
      mi = sum;
      ans00 = pos00;
      ans01 = pos01;
      ans10 = pos10;
      ans11 = i;
    }
    now--;
    if (i) sum -= ns[n11[i - 1]].t;
    now0--;
    now1--;
  }
  cout << mi << '\n';
  for (int i = 0; i < (ans00); i++) cout << ns[n00[i]].ind << " ";
  for (int i = 0; i < (ans01); i++) cout << ns[n01[i]].ind << " ";
  for (int i = 0; i < (ans10); i++) cout << ns[n10[i]].ind << " ";
  for (int i = 0; i < (ans11); i++) cout << ns[n11[i]].ind << " ";
  return 0;
}