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;
int a[205000];
int b[205000];
int cost[205000];
vector<pair<int, int> > pa;
vector<pair<int, int> > pb;
vector<pair<int, int> > pc;
vector<pair<int, int> > pd;
long long suma[205000];
long long sumb[205000];
long long sumc[205000];
long long sumd[205000];
long long ans;
long long acnt;
long long bcnt;
long long dcnt;
long long val(char id, int pos) {
  if (id == 'a') return suma[pos] - suma[pos - 1];
  if (id == 'b') return sumb[pos] - sumb[pos - 1];
  if (id == 'c') return sumc[pos] - sumc[pos - 1];
  if (id == 'd') return sumd[pos] - sumd[pos - 1];
}
int add() {
  if (acnt < pa.size() && bcnt < pb.size() && dcnt < pd.size()) {
    if (val('a', acnt + 1) <= min(val('b', bcnt + 1), val('d', dcnt + 1)))
      acnt++;
    else if (val('b', bcnt + 1) <= min(val('a', acnt + 1), val('d', dcnt + 1)))
      bcnt++;
    else if (val('d', dcnt + 1) <= min(val('a', acnt + 1), val('b', bcnt + 1)))
      dcnt++;
  } else if (acnt < pa.size() && bcnt < pb.size()) {
    if (val('a', acnt + 1) <= val('b', bcnt + 1))
      acnt++;
    else
      bcnt++;
  } else if (bcnt < pb.size() && dcnt < pd.size()) {
    if (val('b', bcnt + 1) <= val('d', dcnt + 1))
      bcnt++;
    else
      dcnt++;
  } else if (acnt < pa.size() && dcnt < pd.size()) {
    if (val('a', acnt + 1) <= val('d', dcnt + 1))
      acnt++;
    else
      dcnt++;
  } else if (acnt < pa.size())
    acnt++;
  else if (bcnt < pb.size())
    bcnt++;
  else if (dcnt < pd.size())
    dcnt++;
  else
    return -1;
  return 0;
}
int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = (1); i <= (n); i++) {
    scanf("%d%d%d", cost + i, a + i, b + i);
    if (a[i] == 1 && b[i] == 1)
      pc.push_back({cost[i], i});
    else if (a[i] == 1)
      pa.push_back({cost[i], i});
    else if (b[i] == 1)
      pb.push_back({cost[i], i});
    else
      pd.push_back({cost[i], i});
  }
  sort(pa.begin(), pa.end());
  sort(pb.begin(), pb.end());
  sort(pc.begin(), pc.end());
  sort(pd.begin(), pd.end());
  for (int i = 1; i <= (int)pa.size(); i++)
    suma[i] = suma[i - 1] + pa[i - 1].first;
  for (int i = 1; i <= (int)pb.size(); i++)
    sumb[i] = sumb[i - 1] + pb[i - 1].first;
  for (int i = 1; i <= (int)pc.size(); i++)
    sumc[i] = sumc[i - 1] + pc[i - 1].first;
  for (int i = 1; i <= (int)pd.size(); i++)
    sumd[i] = sumd[i - 1] + pd[i - 1].first;
  if (min(pa.size(), pb.size()) + pc.size() < k) {
    cout << "-1" << endl;
    return 0;
  }
  ans = min(m, (int)pc.size());
  acnt = max(k - ans, 0ll);
  bcnt = max(k - ans, 0ll);
  dcnt = 0;
  if (acnt + bcnt + ans > m) {
    cout << "-1" << endl;
    return 0;
  }
  while (acnt + bcnt + dcnt + ans < m) {
    if (add() == -1) {
      cout << "-1" << endl;
      return 0;
    }
  }
  long long cmp = sumc[ans] + suma[acnt] + sumb[bcnt] + sumd[dcnt];
  int cur = ans - 1;
  int alim = acnt;
  int blim = bcnt;
  int dlim = dcnt;
  while (cur >= 0) {
    if (acnt + cur < k && bcnt + cur < k) {
      long long del = 2ll * k - acnt - bcnt - 2ll * cur;
      if (dcnt + 1 < del) break;
      while (dcnt && acnt + cur < k && acnt < pa.size()) {
        acnt++, dcnt--;
      }
      while (dcnt && bcnt + cur < k && bcnt < pb.size()) {
        bcnt++, dcnt--;
      }
    }
    if (acnt + cur == k - 1)
      acnt++;
    else if (bcnt + cur == k - 1)
      bcnt++;
    else {
      if (add() == -1) break;
    }
    if (pa.size() < acnt || pb.size() < bcnt || pd.size() < dcnt) break;
    long long now = sumc[cur] + suma[acnt] + sumb[bcnt] + sumd[dcnt];
    if (now < cmp) {
      cmp = now;
      alim = acnt;
      blim = bcnt;
      dlim = dcnt;
      ans = cur;
    }
    cur--;
  }
  cout << cmp << endl;
  for (int i = 0; i < alim; i++) printf("%d ", pa[i].second);
  for (int i = 0; i < blim; i++) printf("%d ", pb[i].second);
  for (int i = 0; i < dlim; i++) printf("%d ", pd[i].second);
  for (int i = 0; i < ans; i++) printf("%d ", pc[i].second);
}