#pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") #include using namespace std; using lint = long long; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #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 void ndarray(vector& vec, const V& val, int len) { vec.assign(len, val); } template void ndarray(vector& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); } 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; } int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); } template pair operator+(const pair &l, const pair &r) { return make_pair(l.first + r.first, l.second + r.second); } template pair operator-(const pair &l, const pair &r) { return make_pair(l.first - r.first, l.second - r.second); } 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; } template ostream &operator<<(ostream &os, const array &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; } #if __cplusplus >= 201703L template istream &operator>>(istream &is, tuple &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template ostream &operator<<(ostream &os, const tuple &tpl) { std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os; } #endif template ostream &operator<<(ostream &os, const deque &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template ostream &operator<<(ostream &os, const set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const pair &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } template ostream &operator<<(ostream &os, const map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; 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 // MaxFlow based and AtCoder Library, single class, no namespace, no private variables, compatible with C++11 // Reference: template struct mf_graph { struct simple_queue_int { std::vector payload; int pos = 0; void reserve(int n) { payload.reserve(n); } int size() const { return int(payload.size()) - pos; } bool empty() const { return pos == int(payload.size()); } void push(const int &t) { payload.push_back(t); } int &front() { return payload[pos]; } void clear() { payload.clear(); pos = 0; } void pop() { pos++; } }; mf_graph() : _n(0) {} mf_graph(int n) : _n(n), g(n) {} int add_edge(int from, int to, Cap cap) { assert(0 <= from && from < _n); assert(0 <= to && to < _n); assert(0 <= cap); int m = int(pos.size()); pos.push_back({from, int(g[from].size())}); int from_id = int(g[from].size()); int to_id = int(g[to].size()); if (from == to) to_id++; g[from].push_back(_edge{to, to_id, cap}); g[to].push_back(_edge{from, from_id, 0}); return m; } struct edge { int from, to; Cap cap, flow; }; edge get_edge(int i) { int m = int(pos.size()); assert(0 <= i && i < m); auto _e = g[pos[i].first][pos[i].second]; auto _re = g[_e.to][_e.rev]; return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap}; } std::vector edges() { int m = int(pos.size()); std::vector result; for (int i = 0; i < m; i++) { result.push_back(get_edge(i)); } return result; } void change_edge(int i, Cap new_cap, Cap new_flow) { int m = int(pos.size()); assert(0 <= i && i < m); assert(0 <= new_flow && new_flow <= new_cap); auto &_e = g[pos[i].first][pos[i].second]; auto &_re = g[_e.to][_e.rev]; _e.cap = new_cap - new_flow; _re.cap = new_flow; } std::vector level, iter; simple_queue_int que; void _bfs(int s, int t) { std::fill(level.begin(), level.end(), -1); level[s] = 0; que.clear(); que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (auto e : g[v]) { if (e.cap == 0 || level[e.to] >= 0) continue; level[e.to] = level[v] + 1; if (e.to == t) return; que.push(e.to); } } } Cap _dfs(int v, int s, Cap up) { if (v == s) return up; Cap res = 0; int level_v = level[v]; for (int &i = iter[v]; i < int(g[v].size()); i++) { _edge &e = g[v][i]; if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) continue; Cap d = _dfs(e.to, s, std::min(up - res, g[e.to][e.rev].cap)); if (d <= 0) continue; g[v][i].cap += d; g[e.to][e.rev].cap -= d; res += d; if (res == up) return res; } level[v] = _n; return res; } Cap flow(int s, int t) { return flow(s, t, std::numeric_limits::max()); } Cap flow(int s, int t, Cap flow_limit) { assert(0 <= s && s < _n); assert(0 <= t && t < _n); assert(s != t); level.assign(_n, 0), iter.assign(_n, 0); que.clear(); Cap flow = 0; while (flow < flow_limit) { _bfs(s, t); if (level[t] == -1) break; std::fill(iter.begin(), iter.end(), 0); Cap f = _dfs(t, s, flow_limit - flow); if (!f) break; flow += f; } return flow; } std::vector min_cut(int s) { std::vector visited(_n); simple_queue_int que; que.push(s); while (!que.empty()) { int p = que.front(); que.pop(); visited[p] = true; for (auto e : g[p]) { if (e.cap && !visited[e.to]) { visited[e.to] = true; que.push(e.to); } } } return visited; } int _n; struct _edge { int to, rev; Cap cap; }; std::vector> pos; std::vector> g; }; // MinCostFlow based on AtCoder Library, no namespace, no private variables, compatible with C++11 // Reference: // **NO NEGATIVE COST EDGES** template struct mcf_graph { mcf_graph() {} mcf_graph(int n) : _n(n), g(n) {} int add_edge(int from, int to, Cap cap, Cost cost) { assert(0 <= from && from < _n); assert(0 <= to && to < _n); assert(0 <= cap); assert(0 <= cost); int m = int(pos.size()); pos.push_back({from, int(g[from].size())}); int from_id = int(g[from].size()); int to_id = int(g[to].size()); if (from == to) to_id++; g[from].push_back(_edge{to, to_id, cap, cost}); g[to].push_back(_edge{from, from_id, 0, -cost}); return m; } struct edge { int from, to; Cap cap, flow; Cost cost; }; edge get_edge(int i) { int m = int(pos.size()); assert(0 <= i && i < m); auto _e = g[pos[i].first][pos[i].second]; auto _re = g[_e.to][_e.rev]; return edge{ pos[i].first, _e.to, _e.cap + _re.cap, _re.cap, _e.cost, }; } std::vector edges() { int m = int(pos.size()); std::vector result(m); for (int i = 0; i < m; i++) { result[i] = get_edge(i); } return result; } std::pair flow(int s, int t) { return flow(s, t, std::numeric_limits::max()); } std::pair flow(int s, int t, Cap flow_limit) { return slope(s, t, flow_limit).back(); } std::vector> slope(int s, int t) { return slope(s, t, std::numeric_limits::max()); } std::vector dual, dist; std::vector pv, pe; std::vector vis; struct Q { Cost key; int to; bool operator<(Q r) const { return key > r.key; } }; std::vector que; bool _dual_ref(int s, int t) { std::fill(dist.begin(), dist.end(), std::numeric_limits::max()); std::fill(vis.begin(), vis.end(), false); que.clear(); dist[s] = 0; que.push_back(Q{0, s}); std::push_heap(que.begin(), que.end()); while (!que.empty()) { int v = que.front().to; std::pop_heap(que.begin(), que.end()); que.pop_back(); if (vis[v]) continue; vis[v] = true; if (v == t) break; // dist[v] = shortest(s, v) + dual[s] - dual[v] // dist[v] >= 0 (all reduced cost are positive) // dist[v] <= (n-1)C for (int i = 0; i < int(g[v].size()); i++) { auto e = g[v][i]; if (vis[e.to] || !e.cap) continue; // |-dual[e.to] + dual[v]| <= (n-1)C // cost <= C - -(n-1)C + 0 = nC Cost cost = e.cost - dual[e.to] + dual[v]; if (dist[e.to] - dist[v] > cost) { dist[e.to] = dist[v] + cost; pv[e.to] = v; pe[e.to] = i; que.push_back(Q{dist[e.to], e.to}); std::push_heap(que.begin(), que.end()); } } } if (!vis[t]) { return false; } for (int v = 0; v < _n; v++) { if (!vis[v]) continue; // dual[v] = dual[v] - dist[t] + dist[v] // = dual[v] - (shortest(s, t) + dual[s] - dual[t]) + (shortest(s, v) + dual[s] - dual[v]) // = - shortest(s, t) + dual[t] + shortest(s, v) // = shortest(s, v) - shortest(s, t) >= 0 - (n-1)C dual[v] -= dist[t] - dist[v]; } return true; } std::vector> slope(int s, int t, Cap flow_limit) { assert(0 <= s && s < _n); assert(0 <= t && t < _n); assert(s != t); // variants (C = maxcost): // -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0 // reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge dual.assign(_n, 0), dist.assign(_n, 0); pv.assign(_n, 0), pe.assign(_n, 0); vis.assign(_n, false); Cap flow = 0; Cost cost = 0, prev_cost_per_flow = -1; std::vector> result; result.push_back({flow, cost}); while (flow < flow_limit) { if (!_dual_ref(s, t)) break; Cap c = flow_limit - flow; for (int v = t; v != s; v = pv[v]) { c = std::min(c, g[pv[v]][pe[v]].cap); } for (int v = t; v != s; v = pv[v]) { auto &e = g[pv[v]][pe[v]]; e.cap -= c; g[v][e.rev].cap += c; } Cost d = -dual[s]; flow += c; cost += c * d; if (prev_cost_per_flow == d) { result.pop_back(); } result.push_back({flow, cost}); prev_cost_per_flow = d; } return result; } struct _edge { int to, rev; Cap cap; Cost cost; }; int _n; std::vector> pos; std::vector> g; }; // https://github.com/beet-aizu/library/blob/master/bflow/capacityscaling.cpp // O(m^2 \log m \log U) // U: maximum capacity enum Objective{ MINIMIZE = +1, MAXIMIZE = -1, }; template struct MinCostFlow{ template inline void chmin(T &x,T y){x=min(x,y);} struct Edge{ int src,dst; Flow flow,cap; Cost cost; int rev; Edge(int src,int dst,Flow cap,Cost cost,int rev): src(src),dst(dst),flow(0),cap(cap),cost(cost),rev(rev){} Flow residual_cap()const{return cap-flow;} }; struct EdgePtr{ int v,e; EdgePtr(int v,int e):v(v),e(e){} }; int n; vector> G; vector b; vector p; MinCostFlow(int n):n(n),G(n),b(n,0){} EdgePtr add_edge(int src,int dst,Flow lower,Flow upper,Cost cost){ int e=G[src].size(); int r=(src==dst?e+1:G[dst].size()); assert(lower<=upper); G[src].emplace_back(src,dst,+upper,+cost*objective,r); G[dst].emplace_back(dst,src,-lower,-cost*objective,e); return EdgePtr(src,e); } const Edge &get_edge(EdgePtr ep)const{return G[ep.v][ep.e];} void push(Edge &e,Flow amount){ e.flow+=amount; G[e.dst][e.rev].flow-=amount; } void add_supply(int v,Flow amount){b[v]+=amount;} void add_demand(int v,Flow amount){b[v]-=amount;} Cost residual_cost(const Edge &e){ return e.cost+p[e.src]-p[e.dst]; } vector excess_vs,deficit_vs; void saturate_negative(const Flow delta){ for(auto &es:G){ for(auto &e:es){ Flow cap=e.residual_cap(); cap-=cap%delta; if(cap<0 or residual_cost(e)<0){ push(e,cap); b[e.src]-=cap; b[e.dst]+=cap; } } } excess_vs.clear(); deficit_vs.clear(); for(int v=0;v0) excess_vs.emplace_back(v); if(b[v]<0) deficit_vs.emplace_back(v); } } const Cost unreachable = std::numeric_limits::max(); Cost farthest; vector dist; vector parent; struct P{ Cost first; int second; P(Cost first,int second):first(first),second(second){} bool operator<(const P o)const{return first>o.first;} }; priority_queue

pq; template void eliminate(vector &vs,Predicate predicate){ vs.erase(remove_if(begin(vs),end(vs),predicate),end(vs)); } bool dual(const Flow delta){ eliminate(excess_vs, [&](int v){return b[v]<+delta;}); eliminate(deficit_vs,[&](int v){return b[v]>-delta;}); dist.assign(n,unreachable); for(int v:excess_vs) pq.emplace(dist[v]=0,v); parent.assign(n,nullptr); auto emplace=[&](Edge& e){ if(e.residual_cap()=dist[e.dst]) return; pq.emplace(dist[e.dst]=nxt,e.dst); parent[e.dst]=&e; }; farthest=0; int deficit_count=0; while(!pq.empty()){ Cost d=pq.top().first; int v=pq.top().second; pq.pop(); if(dist[v]=(int)deficit_vs.size()) break; for(auto &e:G[v]) emplace(e); } pq=decltype(pq)(); for(int v=0;v0; } void primal(const Flow delta){ for(int t:deficit_vs){ if(dist[t]>farthest) continue; Flow f=-b[t]; int v; for(v=t;parent[v];v=parent[v]->src) chmin(f,parent[v]->residual_cap()); chmin(f,b[v]); f-=f%delta; if(f<=0) continue; for(v=t;parent[v];){ auto &e=*parent[v]; push(e,f); int u=parent[v]->src; if(e.residual_cap()<=0) parent[v]=nullptr; v=u; } b[t]+=f; b[v]-=f; } } template bool build(){ p.resize(n); Flow max_flow=1; for(auto t:b) max_flow=max({max_flow,t,-t}); for(auto &es:G) for(auto &e:es) max_flow=max({max_flow,e.residual_cap(),-e.residual_cap()}); Flow delta=1; while(delta T get_cost(){ T res=0; for(auto &es:G) for(auto &e:es) res+=T(e.flow)*T(e.cost)/T(objective); return res/T(2); } template T get_gain(){return get_cost();} vector get_potential(){ fill(p.begin(),p.end(),0); for(int i=0;i0) chmin(p[e.dst],p[e.src]+e.cost); return p; } }; void bad() { puts("NO"); exit(0); } int main() { int N; lint M; cin >> N >> M; vector A(N), B(N), C(N); REP(i, N) { cin >> A[i] >> B[i] >> C[i]; if (A[i] == C[i]) bad(); if (A[i] > C[i]) swap(A[i], C[i]); } vector asort(N), csort(N); REP(i, N) asort[i] = make_pair(A[i], i); REP(i, N) csort[i] = make_pair(C[i], i); sort(ALL(asort)); sort(ALL(csort)); const int gs = N * 4, gt = gs + 1; const lint UP = 0; MinCostFlow g2(gt + 1); REP(i, N) g2.add_edge(gs, i, 0, 1, 0); REP(i, N) g2.add_edge(i + N, i + 3 * N, 0, 1, -C[i]); REP(i, N) g2.add_edge(i + 2 * N, i + 3 * N, 0, 1, UP); REP(i, N - 1) { int j = asort[i].second, k = asort[i + 1].second; g2.add_edge(N + j, N + k, 0, N - 1, 0); } REP(i, N - 1) { int j = csort[i].second, k = csort[i + 1].second; g2.add_edge(N * 2 + k, N * 2 + j, 0, N - 1, 0); } REP(i, N) g2.add_edge(i + 3 * N, gt, 0, 1, 0); REP(i, N) { int j = lower_bound(ALL(asort), pint(B[i] + 1, 0)) - asort.begin(); if (j < N) { int to = asort[j].second; g2.add_edge(i, to + N, 0, 1, UP); } j = lower_bound(ALL(csort), pint(B[i], 0)) - csort.begin() - 1; if (j >= 0) { int to = csort[j].second; g2.add_edge(i, to + N * 2, 0, 1, UP - B[i]); } } g2.add_supply(gs, N); g2.add_demand(gt, N); if (!g2.build()) bad(); else puts("YES"); // auto f2 = g2.(gs, gt, N); lint k = N * UP * 2 - g2.get_cost(); // if (f2.first < N) bad(); if (k < M) bad(); puts("KADOMATSU!"); }