#include <bits/stdc++.h>
using namespace ::std;
const long double PI = acos(-1);
const long long MOD = 1000000000 + 7;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long add(long long a, long long b, long long m = MOD) {
  if (a >= m) a %= m;
  if (b >= m) b %= m;
  if (a < 0) a += m;
  if (b < 0) b += m;
  long long res = a + b;
  if (res >= m or res <= -m) res %= m;
  if (res < 0) res += m;
  return res;
}
long long mul(long long a, long long b, long long m = MOD) {
  if (a >= m) a %= m;
  if (b >= m) b %= m;
  if (a < 0) a += m;
  if (b < 0) b += m;
  long long res = a * b;
  if (res >= m or res <= -m) res %= m;
  if (res < 0) res += m;
  return res;
}
long long pow_mod(long long a, long long b, long long m = MOD) {
  long long res = 1LL;
  a = a % m;
  while (b) {
    if (b & 1) res = mul(res, a, m);
    b >>= 1;
    a = mul(a, a, m);
  }
  return res;
}
long long fastexp(long long a, long long b) {
  long long res = 1LL;
  while (b) {
    if (b & 1) res = res * a;
    b >>= 1;
    a *= a;
  }
  return res;
}
int gcdExtendido(int a, int b, int *x, int *y) {
  if (a == 0) {
    *x = 0;
    *y = 1;
    return b;
  }
  int x1, y1;
  int gcd = gcdExtendido(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
int modInverso(int a, int m) {
  int x, y;
  int g = gcdExtendido(a, m, &x, &y);
  if (g != 1)
    return -1;
  else
    return (x % m + m) % m;
}
const int N = 200000 + 5;
const long long inf = 1e5;
int n, m, k;
int a[N];
int b[N];
int t[N];
int len;
int pos[N];
long long st[4 * N];
int cnt[4 * N];
bool none[N];
vector<int> justA, justB, both, extras;
void build(int pos = 1, int l = 0, int r = len) {
  if (l == r) {
    st[pos] = (l and none[extras[l - 1]]) ? t[extras[l - 1]] : 0;
    cnt[pos] = (st[pos] > 0);
    return;
  }
  int mi = (l + r) / 2;
  build(2 * pos, l, mi);
  build(2 * pos + 1, mi + 1, r);
  st[pos] = st[2 * pos] + st[2 * pos + 1];
  cnt[pos] = cnt[2 * pos] + cnt[2 * pos + 1];
}
void update(int x, int y, int pos = 1, int l = 0, int r = len) {
  if (l == r) {
    st[pos] = y;
    cnt[pos] = (y > 0);
    return;
  }
  int mi = (l + r) / 2;
  if (l <= x and x <= mi)
    update(x, y, 2 * pos, l, mi);
  else
    update(x, y, 2 * pos + 1, mi + 1, r);
  st[pos] = st[2 * pos] + st[2 * pos + 1];
  cnt[pos] = cnt[2 * pos] + cnt[2 * pos + 1];
}
long long query(int x, int y, int pos = 1, int l = 0, int r = len) {
  if (y < l or r < x or x > y) return 0LL;
  if (x <= l and r <= y) return st[pos];
  int mi = (l + r) / 2;
  return query(x, y, 2 * pos, l, mi) + query(x, y, 2 * pos + 1, mi + 1, r);
}
int solve(int k, int pos = 1, int l = 0, int r = len) {
  if (cnt[pos] < k) return -1;
  if (l == r) return l;
  int mi = (l + r) / 2;
  if (cnt[2 * pos] >= k)
    return solve(k, 2 * pos, l, mi);
  else
    return solve(k - cnt[2 * pos], 2 * pos + 1, mi + 1, r);
}
vector<long long> getPrefix(vector<int> &v) {
  vector<long long> ans(1, 0LL);
  for (int i = 0; i < v.size(); i++) {
    ans.emplace_back(ans.back() + t[v[i]]);
  }
  return ans;
}
vector<int> merge(vector<int> &a, vector<int> &b) {
  vector<int> ans;
  int p1 = 0, p2 = 0;
  while (p1 < a.size() and p2 < b.size()) {
    if (t[a[p1]] < t[b[p2]]) {
      ans.emplace_back(a[p1]);
      p1 += 1;
    } else {
      ans.emplace_back(b[p2]);
      p2 += 1;
    }
  }
  while (p1 < a.size()) {
    ans.emplace_back(a[p1]);
    p1 += 1;
  }
  while (p2 < b.size()) {
    ans.emplace_back(b[p2]);
    p2 += 1;
  }
  return ans;
}
vector<int> getAnswer(int bestid) {
  vector<int> ans;
  for (int i = 0; i < bestid; i++) {
    ans.emplace_back(both[i]);
  }
  int wantA = max(0, k - bestid);
  int wantB = max(0, k - bestid);
  for (int i = 0; i < wantA; i++) {
    ans.emplace_back(justA[i]);
  }
  for (int i = 0; i < wantB; i++) {
    ans.emplace_back(justB[i]);
  }
  build();
  for (int i = wantA; i < justA.size(); i++) {
    int x = justA[i];
    update(pos[x] + 1, t[x]);
  }
  for (int i = wantB; i < justB.size(); i++) {
    int x = justB[i];
    update(pos[x] + 1, t[x]);
  }
  int extra = m - bestid - wantB - wantA;
  for (int i = 0; i < extra; i++) {
    int p = solve(1);
    ans.emplace_back(extras[p - 1]);
    update(p, 0);
  }
  return ans;
}
int main() {
  scanf("%d %d %d", &(n), &(m), &(k));
  for (int i = 1; i <= n; i++) {
    scanf("%d %d %d", &(t[i]), &(a[i]), &(b[i]));
    if (a[i] and !b[i]) justA.emplace_back(i);
    if (!a[i] and b[i]) justB.emplace_back(i);
    if (a[i] and b[i]) both.emplace_back(i);
    if (!a[i] and !b[i]) {
      extras.emplace_back(i);
      none[i] = true;
    }
  }
  sort((justA).begin(), (justA).end(),
       [&](int i, int j) { return t[i] < t[j]; });
  sort((justB).begin(), (justB).end(),
       [&](int i, int j) { return t[i] < t[j]; });
  sort((both).begin(), (both).end(), [&](int i, int j) { return t[i] < t[j]; });
  sort((extras).begin(), (extras).end(),
       [&](int i, int j) { return t[i] < t[j]; });
  extras = merge(extras, justA);
  extras = merge(extras, justB);
  len = extras.size();
  for (int i = 0; i < extras.size(); i++) {
    pos[extras[i]] = i;
  }
  build();
  vector<long long> bothP = getPrefix(both);
  vector<long long> justAP = getPrefix(justA);
  vector<long long> justBP = getPrefix(justB);
  long long best = LLONG_MAX;
  int bestid = -1;
  int posA = justA.size() - 1;
  int posB = justB.size() - 1;
  for (int i = 0; i < bothP.size(); i++) {
    int wantA = max(0, k - i);
    int wantB = max(0, k - i);
    if (wantA >= justAP.size()) continue;
    if (wantB >= justBP.size()) continue;
    while (posA >= wantA) {
      int x = justA[posA];
      update(pos[x] + 1, t[x]);
      posA -= 1;
    }
    while (posB >= wantB) {
      int x = justB[posB];
      update(pos[x] + 1, t[x]);
      posB -= 1;
    }
    int suma = i + wantA + wantB;
    if (suma > m) continue;
    int need = solve(m - suma);
    if (need == -1) continue;
    long long cur = bothP[i] + justAP[wantA] + justBP[wantB] + query(0, need);
    if (cur < best) {
      best = cur;
      bestid = i;
    }
  }
  if (best == LLONG_MAX) best = -1;
  printf("%lld\n", best);
  if (best >= 0) {
    vector<int> ans = getAnswer(bestid);
    for (int i = 0; i < ans.size(); i++) {
      printf("%d%c", ans[i], " \n"[i + 1 == ans.size()]);
    }
  }
  return 0;
}
