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;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
template <class T1, class T2>
struct cmpf {
  bool rev;
  inline bool operator()(const pair<T1, T2>& a, const pair<T1, T2>& b) const {
    return (a.first < b.first) ^ rev;
  }
  cmpf(bool b = false) { rev = b; }
};
template <class T1, class T2>
struct cmps {
  bool rev;
  inline bool operator()(const pair<T1, T2>& a, const pair<T1, T2>& b) const {
    return (a.second < b.second) ^ rev;
  }
  cmps(bool b = false) { rev = b; }
};
string to_string(const char& c) { return string(1, c); }
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(const bool& b) { return (b ? "true" : "false"); }
string to_string(const vector<bool>& v, const string& sep = " ") {
  string s = "[";
  for (int i = 0; i < v.size(); i++) {
    if (i) {
      s += sep;
    }
    s += to_string(v[i]);
  }
  return s + "]";
}
template <size_t N>
string to_string(const bitset<N>& v) {
  string s = "[";
  for (size_t i = 0; i < N; i++) s += v[i] ? '1' : '0';
  return s + "]";
}
template <class T1, class T2>
string to_string(const pair<T1, T2>& p, const string& sep = ",");
template <class T>
string to_string(const T& v, const string& sep = " ") {
  bool first = true;
  string s = "{";
  for (const auto& x : v) {
    if (!first)
      s += sep;
    else
      first = false;
    s += to_string(x);
  }
  return s + "}";
}
template <class T>
string to_string(const T& v, const int& sz, const string& sep = " ") {
  string s = "[";
  for (int i = 0; i < sz; i++) {
    if (i) {
      s += sep;
    }
    s += to_string(v[i]);
  }
  return s + "]";
}
template <class T1, class T2>
string to_string(const pair<T1, T2>& p, const string& sep) {
  return "(" + to_string(p.first) + sep + to_string(p.second) + ")";
}
long long binpow(const long long& x, const long long& p, const long long& mod) {
  assert(mod > 0);
  if (p == 0) return 1;
  if (p == 1) return x % mod;
  if (p & 1) return (binpow((x * x) % mod, p / 2, mod) * x) % mod;
  return binpow((x * x) % mod, p / 2, mod) % mod;
}
template <class T>
void chmn(T& a, const T& b) {
  if (a > b) a = b;
}
template <class T>
void chmx(T& a, const T& b) {
  if (a < b) a = b;
}
void setIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}
int chr[8] = {-1, 0, 1, 0, -1, -1, 1, 1};
int chc[8] = {0, 1, 0, -1, -1, 1, -1, 1};
const long long MOD = 1e9 + 7;
const int INIT = 1001001001;
const int MN = 200001;
struct Book {
  int num;
  bool a, b;
  long long time;
};
string to_string(const Book& b) {
  return "{" + to_string(b.a) + " " + to_string(b.b) + " " + to_string(b.time) +
         "}";
}
struct comp_time {
  inline const bool operator()(const Book& a, const Book& b) {
    return a.time > b.time;
  }
};
struct comp_rev_time {
  inline const bool operator()(const Book& a, const Book& b) {
    return a.time < b.time;
  }
};
int get_time(const priority_queue<Book, vector<Book>, comp_time>& pq) {
  if (pq.empty()) return INIT;
  return pq.top().time;
}
Book book[MN];
void solve(bool hard) {
  long long N, M, K;
  if (hard) {
    cin >> N >> M >> K;
  } else {
    cin >> N >> K;
  }
  char c1, c2;
  for (int i = 0; i < N; i++) {
    cin >> book[i].time >> c1 >> c2;
    book[i].a = c1 == '1';
    book[i].b = c2 == '1';
    book[i].num = i + 1;
  }
  priority_queue<Book, vector<Book>, comp_time> alice, bob, both, other;
  for (int i = 0; i < N; i++) {
    if (book[i].a && book[i].b) {
      both.push(book[i]);
    } else if (book[i].a) {
      alice.push(book[i]);
    } else if (book[i].b) {
      bob.push(book[i]);
    } else {
      other.push(book[i]);
    }
  }
  priority_queue<Book, vector<Book>, comp_rev_time> used_a, used_b, used_both,
      used_other;
  long long ans = 0;
  int ct = 0;
  int total = 0;
  while (alice.size() + bob.size() + both.size() > 0 && (ct < K)) {
    int ab = INIT;
    if (alice.size() > 0 && bob.size() > 0) {
      ab = alice.top().time + bob.top().time;
    }
    int c = INIT;
    if (both.size() > 0) {
      c = both.top().time;
    }
    if (ab == INIT && c == INIT) {
      ans = -1;
      break;
    }
    if (ab < c) {
      used_a.push(alice.top());
      used_b.push(bob.top());
      alice.pop();
      bob.pop();
      ans += ab;
      total += 2;
    } else {
      used_both.push(both.top());
      both.pop();
      ans += c;
      total += 1;
    }
    ++ct;
  }
  if (ct < K) {
    cout << -1 << endl;
    return;
  }
  if (!hard) {
    cout << ans << endl;
    return;
  }
  int cta = ct, ctb = ct;
  while (total < M) {
    if (alice.size() + bob.size() + both.size() + other.size() == 0) {
      cout << -1 << endl;
      return;
    }
    int opts[] = {get_time(alice), get_time(bob), get_time(both),
                  get_time(other)};
    int idx = min_element(opts, opts + 4) - opts;
    assert(opts[idx] != INIT);
    ans += opts[idx];
    ++total;
    if (idx == 0) {
      used_a.push(alice.top());
      alice.pop();
      ++cta;
    } else if (idx == 1) {
      used_b.push(bob.top());
      bob.pop();
      ++ctb;
    } else if (idx == 2) {
      used_both.push(both.top());
      both.pop();
    } else if (idx == 3) {
      used_other.push(other.top());
      other.pop();
    } else
      assert(false);
    while (cta > K && used_both.size() > 0 &&
           used_both.top().time > get_time(bob)) {
      ans += bob.top().time - used_both.top().time;
      both.push(used_both.top());
      used_both.pop();
      used_b.push(bob.top());
      bob.pop();
      --cta;
    }
    while (ctb > K && used_both.size() > 0 &&
           used_both.top().time > get_time(alice)) {
      ans += alice.top().time - used_both.top().time;
      both.push(used_both.top());
      used_both.pop();
      used_a.push(alice.top());
      alice.pop();
      --ctb;
    }
  }
  while (total > M) {
    if (used_a.empty() || used_b.empty() || both.empty()) {
      cout << -1 << endl;
      return;
    }
    ans += both.top().time - used_a.top().time - used_b.top().time;
    alice.push(used_a.top());
    used_a.pop();
    bob.push(used_b.top());
    used_b.pop();
    used_both.push(both.top());
    both.pop();
    --total;
  }
  cout << ans << endl;
  while (used_a.size() > 0) {
    cout << used_a.top().num << ' ';
    used_a.pop();
  }
  while (used_b.size() > 0) {
    cout << used_b.top().num << ' ';
    used_b.pop();
  }
  while (used_both.size() > 0) {
    cout << used_both.top().num << ' ';
    used_both.pop();
  }
  while (used_other.size() > 0) {
    cout << used_other.top().num << ' ';
    used_other.pop();
  }
  cout << endl;
}
int main() {
  setIO();
  long long T = 1;
  while (T--) solve(true);
  return 0;
}