#include <bits/stdc++.h>
using namespace std;
char local = 'O';
template <typename T>
void read(T &a) {
  cin >> a;
}
template <typename T, typename T1>
void read(T &a, T1 &b) {
  cin >> a >> b;
}
template <typename T, typename T1, typename T2>
void read(T &a, T1 &b, T2 &c) {
  cin >> a >> b >> c;
}
template <typename T, typename T1>
void read(pair<T, T1> &p) {
  cin >> p.first >> p.second;
}
template <typename T>
void read(T arr[], long long n) {
  for (long long i = 0; i < n; i++) {
    read(arr[i]);
  }
}
template <typename T>
void read(vector<T> &arr) {
  for (long long i = 0; i < arr.size(); i++) {
    read(arr[i]);
  }
}
long long modularExponentiation(long long x, long long n, long long M) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return modularExponentiation((x * x) % M, n / 2, M);
  return (x * modularExponentiation((x * x) % M, (n - 1) / 2, M)) % M;
}
long long binaryExponentiation(long long x, long long n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return binaryExponentiation(x * x, n / 2);
  else
    return x * binaryExponentiation(x * x, (n - 1) / 2);
}
long long GCD(long long A, long long B) {
  if (B == 0)
    return A;
  else
    return GCD(B, A % B);
}
long long extended_GCD(long long a, long long b, long long &x, long long &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long x1, y1;
  long long gcd = extended_GCD(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return gcd;
}
long long modinv(long long a, long long mod) {
  long long x, y;
  extended_GCD(a, mod, x, y);
  if (x < 0) x += mod;
  return x;
}
long long modPrimeInverse(long long A, long long M) {
  return modularExponentiation(A, M - 2, M);
}
vector<long long> prime;
void sieve(long long N) {
  bool isPrime[N + !1];
  for (long long i = 0; i <= N; ++i) {
    isPrime[i] = true;
  }
  isPrime[0] = false;
  isPrime[1] = false;
  for (long long i = 2; i * i <= N; ++i) {
    if (isPrime[i] == true) {
      for (long long j = i * i; j <= N; j += i) isPrime[j] = false;
    }
  }
  for (long long i = 2; i <= N; i++) {
    if (isPrime[i] == true) {
      prime.push_back(i);
    }
  }
}
long long minPrime[1000007];
void factorSieve(long long n) {
  memset(minPrime, 0, sizeof(minPrime));
  for (long long i = 2; i * i <= n; ++i) {
    if (minPrime[i] == 0) {
      for (long long j = i * i; j <= n; j += i) {
        if (minPrime[j] == 0) {
          minPrime[j] = i;
        }
      }
    }
  }
  for (long long i = 2; i <= n; ++i) {
    if (minPrime[i] == 0) {
      minPrime[i] = i;
    }
  }
}
long long factorize(long long n) {
  map<long long, long long> m;
  m.clear();
  while (n != 1) {
    m[minPrime[n]]++;
    n /= minPrime[n];
  }
  for (auto i : m) {
    if (i.second > 1) {
      return 0;
    }
  }
  return 1;
}
void merge(long long A[], long long start, long long mid, long long end) {
  long long p = start, q = mid + 1;
  long long Arr[end - start + 1], k = 0;
  for (long long i = start; i <= end; i++) {
    if (p > mid)
      Arr[k++] = A[q++];
    else if (q > end)
      Arr[k++] = A[p++];
    else if (A[p] < A[q])
      Arr[k++] = A[p++];
    else
      Arr[k++] = A[q++];
  }
  for (long long p = 0; p < k; p++) {
    A[start++] = Arr[p];
  }
}
void merge_sort(long long A[], long long start, long long end) {
  if (start < end) {
    long long mid = (start + end) / 2;
    merge_sort(A, start, mid);
    merge_sort(A, mid + 1, end);
    merge(A, start, mid, end);
  }
}
long long binarySearch(long long a[], long long low, long long high,
                       long long key) {
  while (low <= high) {
    long long mid = (low + high) / 2;
    if (a[mid] < key) {
      low = mid + 1;
    } else if (a[mid] > key) {
      high = mid - 1;
    } else {
      return mid;
    }
  }
  return -1;
}
bool sortbysec(const pair<long long, long long> &a,
               const pair<long long, long long> &b) {
  if (a.first == b.first) {
    a.second > b.second;
  }
  return (a.first < b.first);
}
void fib(long long n, long long m, long long a[]) {
  if (n == 0) {
    a[0] = 0;
    a[1] = 1;
    return;
  }
  fib(n / 2, m, a);
  long long c, d;
  c = (a[0] * (2 * a[1] - a[0] + m)) % m;
  d = (a[0] * a[0] + a[1] * a[1]) % m;
  if (n % 2 == 0) {
    a[0] = c;
    a[1] = d;
  } else {
    a[0] = d;
    a[1] = c + d;
  }
}
struct comp {
  bool operator()(pair<long long, long long> a, pair<long long, long long> b) {
    if (a.first == b.first) {
      return a.second > b.second;
    }
    return a.first < b.first;
  }
};
bool cmp(pair<long long, long long> a, pair<long long, long long> b) {
  if (a.first == b.first) {
    return a.second > b.second;
  }
  return a.first < b.first;
}
vector<long long> va;
void get(long long i, vector<pair<long long, long long>> &v) {
  for (long long j = 0; j < i; j++) {
    va.push_back(v[j].second);
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, M, k;
  cin >> n >> M >> k;
  vector<long long> t(n);
  vector<long long> a(n);
  vector<long long> b(n);
  for (long long i = 0; i < n; i++) {
    cin >> t[i] >> a[i] >> b[i];
  }
  vector<pair<long long, long long>> v1, v2, v, v3;
  long long m = 0;
  for (long long i = 0; i < n; i++) {
    if (a[i] == 1 && b[i] == 1) {
      v.push_back({t[i], i});
    } else if (a[i] == 1) {
      v1.push_back({t[i], i});
    } else if (b[i] == 1) {
      v2.push_back({t[i], i});
    } else {
      v3.push_back({t[i], i});
    }
  }
  long long l = (long long)v1.size();
  long long r = (long long)v2.size();
  long long c = (long long)v.size();
  long long e = (long long)v.size();
  if (local == 'L') {
    cout << "l"
         << " " << l << " "
         << "r"
         << " " << r << " "
         << "c"
         << " " << c << "\n";
  }
  long long x1 = 0;
  long long x2 = 0;
  long long x = 0;
  long long x3 = 0;
  if (l != 0) sort(v1.begin(), v1.end());
  if (r != 0) sort(v2.begin(), v2.end());
  if (c != 0) sort(v.begin(), v.end());
  if (e != 0) sort(v3.begin(), v3.end());
  long long ans = 0;
  while (x != c && x1 != l && x2 != r && k) {
    long long f = v[x].first;
    long long s = v1[x1].first + v2[x2].first;
    if (f <= s) {
      ans += f;
      x++;
      m += 1;
    } else {
      x1++;
      x2++;
      ans += s;
      m += 2;
    }
    k--;
  }
  if (local == 'L') {
    cout << "ans"
         << " " << ans << " "
         << "k"
         << " " << k << "\n";
  }
  if (local == 'L') {
    cout << "x"
         << " " << x << " "
         << "x1"
         << " " << x1 << " "
         << "x2"
         << " " << x2 << "\n";
  }
  if (k != 0) {
    if (x == c) {
      while (x1 != l && x2 != r && k) {
        long long s = v1[x1].first + v2[x2].first;
        x1++;
        x2++;
        ans += s;
        k--;
        m += 2;
      }
    } else {
      while (x != c && k) {
        long long f = v[x].first;
        ans += f;
        x++;
        k--;
        m += 1;
      }
    }
  }
  if (local == 'L') {
    cout << "ans"
         << " " << ans << " "
         << "k"
         << " " << k << "\n";
  }
  if (local == 'L') {
    cout << "x"
         << " " << x << " "
         << "x1"
         << " " << x1 << " "
         << "x2"
         << " " << x2 << "\n";
  }
  if (local == 'L') {
    cout << "m"
         << " " << m << " "
         << "M"
         << " " << M << "\n";
  }
  if (k != 0) {
    cout << -1 << "\n";
    exit(0);
  } else if (m == M) {
    cout << ans << "\n";
    exit(0);
  }
  if (m > M) {
    long long f = m - M;
    while (f && x != c && x1 != -1 && x2 != -1) {
      ans += v[x].first - v[x1].first - v[x2].first;
      x++;
      x1--;
      x2--;
      f--;
    }
    if (f != 0) {
      cout << -1 << "\n";
    } else {
      get(x1, v1);
      get(x2, v2);
      get(x, v);
      cout << ans << "\n";
      for (auto i : va) {
        cout << i + 1 << " ";
      }
      cout << "\n";
    }
  } else {
    vector<pair<long long, long long>> ve;
    long long f = M - m;
    for (long long j = x1; j < l; j++) {
      pair<long long, long long> i = v1[j];
      ve.push_back(i);
    }
    for (long long j = x2; j < r; j++) {
      pair<long long, long long> i = v2[j];
      ve.push_back(i);
    }
    for (long long j = x; j < c; j++) {
      pair<long long, long long> i = v[j];
      ve.push_back(i);
    }
    for (long long j = x3; j < e; j++) {
      pair<long long, long long> i = v3[j];
      ve.push_back(i);
    }
    sort(ve.begin(), ve.end(), cmp);
    if (ve.size() < f) {
      cout << -1 << "\n";
    } else {
      long long co = 0;
      while (f > 0) {
        ans += ve[co].first;
        co++;
        f--;
      }
      get(x1, v1);
      get(x2, v2);
      get(x, v);
      get(co, ve);
      cout << ans << "\n";
      for (auto i : va) {
        cout << i + 1 << " ";
      }
      cout << "\n";
    }
  }
}
