#include <bits/stdc++.h>
using namespace std;
long long N = 200000;
vector<bool> visited(N + 1, false);
bool isprime(long long x) {
  for (long long i = 2; i <= sqrt(x); i++) {
    if (x % i == 0) return false;
  }
  return true;
}
void findfact(long long x, map<long long, vector<long long>>& m) {
  for (long long i = 3; i * i <= x; i += 2) {
    if (x % i == 0) {
      if (i == x / i)
        m[x].push_back(i);
      else {
        m[x].push_back(i);
        m[x].push_back(x / i);
      }
      break;
    }
  }
  return;
}
struct cmp {
  bool operator()(const pair<int, int>& a, const pair<int, int>& b) {
    if (a.first > b.first)
      return false;
    else if (a.first < b.first)
      return true;
    if (a.second < b.second) return false;
    return true;
  }
};
bool isPalindrome(string t) {
  long long st = 0;
  long long end = t.size() - 1;
  while (st <= end) {
    if (t[st] != t[end]) return false;
    st++;
    end--;
  }
  return true;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
bool pow2(long long x) { return x && (!(x & (x - 1))); }
unsigned long long factorial(unsigned long long n) {
  return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
unsigned long long binomialCoeff(unsigned long long n, unsigned long long k) {
  unsigned long long res = 1;
  if (k > n - k) k = n - k;
  for (unsigned long long i = 0; i < k; ++i) {
    res *= (n - i);
    res /= (i + 1);
  }
  return res;
}
vector<long long> hp;
void primeFactors(long long n) {
  while (n % 2 == 0) {
    hp.push_back(2);
    n = n / 2;
  }
  for (int i = 3; i <= sqrt(n); i = i + 2) {
    while (n % i == 0) {
      hp.push_back(i);
      n = n / i;
    }
  }
  if (n > 2) hp.push_back(n);
}
long long minFlipsMonoIncr(string S) {
  int flip = 0;
  int ones = 0;
  bool flag = false;
  for (int i = 0; i < S.size(); i++) {
    if (S[i] - '0' == 1) flag = true;
    if (flag && S[i] - '0' == 0)
      flip++;
    else if (S[i] == '1')
      ones++;
    if (flip > ones) flip = ones;
  }
  return flip;
}
void dfs() {}
long long process(string s) {
  long long res = 0;
  for (long long init = 0; init <= 1e6; init++) {
    long long curr = init;
    bool ok = true;
    for (long long i = 0; i < s.size(); i++) {
      res++;
      if (s[i] == '+')
        curr++;
      else
        curr--;
      if (curr < 0) {
        ok = false;
        break;
      }
    }
    if (ok) break;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  long long T = 1;
  while (T--) {
    long long n, k;
    cin >> n >> k;
    vector<long long> v1;
    vector<long long> v2;
    vector<long long> v3;
    for (long long i = 0; i < n; i++) {
      long long t, a, b;
      cin >> t >> a >> b;
      if (a == 1 && b == 1) {
        v1.push_back(t);
      }
      if (a == 0 && b == 1) {
        v3.push_back(t);
      }
      if (a == 1 && b == 0) {
        v2.push_back(t);
      }
    }
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    sort(v3.begin(), v3.end());
    long long i = 0;
    long long j = 0;
    long long ans = 0;
    long long z = 0;
    while (z < k) {
      if (i < v1.size() && j < v2.size() && j < v3.size()) {
        z++;
        if (v1[i] < v2[j] + v3[j]) {
          ans += v1[i];
          i++;
        } else {
          ans += v2[j] + v3[j];
          j++;
        }
      } else if (i < v1.size()) {
        ans += v1[i];
        i++;
        z++;
      } else {
        ans += v2[j] + v3[j];
        j++;
        z++;
      }
    }
    cout << ans << "\n";
  }
}
