#include using namespace std; using lint = long long; using pint = pair; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template vector sort_unique(vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template istream &operator>>(istream &is, vector &vec) { for (auto &v : vec) is >> v; return is; } template ostream &operator<<(ostream &os, const vector &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } #ifdef HITONANODE_LOCAL const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m"; #define dbg(x) cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl #else #define dbg(x) (x) #endif void bad() { puts("NO"); exit(0); } int main() { cin.tie(nullptr), ios::sync_with_stdio(false); int N; lint M; cin >> N >> M; vector B(N); vector AC; REP(i, N) { int a, c; cin >> a >> B[i] >> c; if (a > c) swap(a, c); AC.emplace_back(a, c); } sort(AC.begin(), AC.end(), [](const auto &l, const auto &r) { return l.second < r.second; }); sort(B.begin(), B.end()); const auto zaatsu = sort_unique(B); vector sh(N); REP(i, N) sh[i] = lower_bound(zaatsu.begin(), zaatsu.end(), AC[i].first) - zaatsu.begin(); lint ret = -1; int nadded = 0; vector cnt(zaatsu.size() + 1); vector used(N); lint bupsum = accumulate(B.begin(), B.end(), 0LL); lint csum = 0; auto try_to_solve = [&]() { int curb = nadded; REP(i, N) if (!used[i]) { if (B[curb++] <= AC[i].second) return; } chmax(ret, csum + bupsum); }; REP(i, N) { try_to_solve(); auto t = lower_bound(zaatsu.begin(), zaatsu.end(), B[i]) - zaatsu.begin(); REP(j, t + 1) cnt[j]++; int h = 0; while (cnt[h] > 0) h++; int amax = -1; IREP(j, N) if (!used[j] and sh[j] >= h) { amax = j; break; } if (amax < 0) break; used[amax] = 1; REP(j, sh[amax]) cnt[j]--; bupsum -= B[i]; nadded++; csum += AC[amax].second; } try_to_solve(); if (ret < 0) bad(); puts("YES"); if (ret < M) bad(); puts("KADOMATSU!"); }