#include <bits/stdc++.h>
using namespace std;
long long n, m, k;
pair<long long, long long> cost[222222];
long long a[222222];
vector<pair<long long, long long>> b[4];
long long s[4][222222];
long long pos[4];
long long apos[4];
long long adel;
long long al;
long long ai;
long long get_cnt(long long t, long long x, long long y) {
  for (long long i = 0; i < long long(4); ++i) {
    pos[i] =
        upper_bound(b[i].begin(), b[i].end(), make_pair(t, (long long)1e9)) -
        b[i].begin();
    if (i == 3)
      pos[i] = max(pos[i], x);
    else if (i)
      pos[i] = max(pos[i], y);
  }
  return pos[0] + pos[1] + pos[2] + pos[3];
}
set<tuple<long long, long long, long long>> vec;
void solve() {
  scanf("%lld%lld%lld", &n, &m, &k);
  for (long long i = 0; i < long long(n); ++i) {
    scanf("%lld", &cost[i].first);
    cost[i].second = i;
    long long an, bn;
    scanf("%lld%lld", &an, &bn);
    a[i] = 2 * an + bn;
  }
  for (long long i = 0; i < long long(n); ++i) {
    b[a[i]].push_back(cost[i]);
  }
  for (long long i = 0; i < long long(4); ++i) sort(b[i].begin(), b[i].end());
  for (long long i = 0; i < long long(4); ++i)
    for (long long j = 1; j <= long long(b[i].size()); ++j)
      s[i][j] = s[i][j - 1] + b[i][j - 1].first;
  long long A = (long long)1e18;
  for (long long i = 0; i <= k && i <= long long(b[3].size()); ++i) {
    if (k - i > min(long long(b[1].size()), long long(b[2].size()))) continue;
    if (k * 2 - i > m) continue;
    long long l = 0, r = (long long)1e9;
    while (l < r) {
      long long mid = (l + r) / 2;
      if (get_cnt(mid, i, k - i) >= m)
        r = mid;
      else
        l = mid + 1;
    }
    long long cnt = get_cnt(l, i, k - i);
    long long tmp = 0;
    for (long long j = 0; j < long long(4); ++j) tmp += s[j][pos[j]];
    tmp -= 1ll * (cnt - m) * l;
    if (A > tmp) {
      A = tmp;
      long long del = cnt - m;
      swap(apos, pos);
      adel = del;
      al = l;
      ai = i;
    }
  }
  vec.clear();
  swap(pos, apos);
  long long l = al;
  long long del = adel;
  long long i = ai;
  for (long long q = 0; q < long long(4); ++q) {
    for (long long j = 0; j < long long(pos[q]); ++j)
      vec.insert(make_tuple(b[q][j].first, i, b[q][j].second));
  }
  long long c1 = pos[1];
  long long c2 = pos[2];
  long long c3 = pos[3];
  set<tuple<long long, long long, long long>> rem;
  for (auto& it : vec) {
    if (del == 0) break;
    long long c, type, idx;
    tie(c, type, idx) = it;
    if (c != l) continue;
    if (type == 0) {
      --del;
      rem.insert(it);
      continue;
    }
    if (type == 1 && c1 > k - i) {
      --c1;
      --del;
      rem.insert(it);
      continue;
    }
    if (type == 2 && c2 > k - i) {
      --c2;
      --del;
      rem.insert(it);
      continue;
    }
    if (c3 > i) {
      --c3;
      --del;
      rem.insert(it);
      continue;
    }
  }
  for (auto& it : rem) vec.erase(it);
  if (A > 1e17) {
    puts("-1");
    return;
  }
  printf("%lld\n", A);
  for (auto& it : vec) {
    printf("%lld ", get<2>(it) + 1);
  }
  puts("");
}
signed main() {
  long long T = 1;
  while (T--) solve();
  return 0;
}
