#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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) { return m < q ? (m = q, true) : false; } template bool chmin(T &m, const T q) { return m > q ? (m = q, true) : 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 int arglb(const std::vector &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); } template int argub(const std::vector &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); } 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) { os << '('; 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 #define dbgif(cond, x) ((cond) ? cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl : cerr) #else #define dbg(x) (x) #define dbgif(cond, x) 0 #endif template class radix_heap_array { int sz; Uint last; std::array>, std::numeric_limits::digits + 1> v; struct smallpii { unsigned b : 7; int j : 25; }; std::vector i2bj; template ::type* = nullptr> static inline unsigned bucket(U x) noexcept { return x ? 32 - __builtin_clz(x) : 0; } template ::type* = nullptr> static inline unsigned bucket(U x) noexcept { return x ? 64 - __builtin_clzll(x) : 0; } void pull() { if (!v[0].empty()) return; int b = 1; while (v[b].empty()) ++b; last = v[b].back().first; for (int j = 0; j < int(v[b].size()); j++) last = std::min(last, v[b][j].first); for (int j = 0; j < int(v[b].size()); j++) { int i = v[b][j].second; auto bnxt = bucket(v[b][j].first ^ last); i2bj[i] = {bnxt, int(v[bnxt].size())}, v[bnxt].emplace_back(std::move(v[b][j])); } v[b].clear(); } public: radix_heap_array() : sz(0), last(0) {} bool empty() const noexcept { return sz == 0; } int argmin_pop() { pull(), --sz; int i = v[0].back().second; i2bj[i].j = -1; v[0].pop_back(); return i; } void chmin(Uint vnew, int i) { if (i >= int(i2bj.size())) i2bj.resize(i + 1, {0, -1}); if (i2bj[i].j < 0) { auto b = bucket(vnew ^ last); ++sz, i2bj[i] = {b, int(v[b].size())}, v[b].emplace_back(vnew, i); } else if (v[i2bj[i].b][i2bj[i].j].first > vnew) { auto bold = i2bj[i].b, bnew = bucket(vnew ^ last); if (bnew < bold) { int ilast = v[bold].back().second, j = i2bj[i].j; std::swap(v[bold][j], v[bold].back()); i2bj[ilast].j = j, i2bj[i] = {bnew, int(v[bnew].size())}; v[bnew].emplace_back(vnew, i), v[bold].pop_back(); } else { v[bold][i2bj[i].j].first = vnew; } } } void pop() { argmin_pop(); } std::pair top() { return pull(), v[0].back(); } [[deprecated("NOT usual emplace() opeation!")]] void emplace(Uint vnew, int i) { chmin(vnew, i); } void clear() noexcept { sz = 0, last = 0, i2bj.assign(i2bj.size(), {0, -1}); } }; // Minimum cost flow WITH NO NEGATIVE CYCLE (just negative cost edge is allowed) // Verified: // - SRM 770 Div1 Medium https://community.topcoder.com/stat?c=problem_statement&pm=15702 // - CodeChef LTIME98 Ancient Magic https://www.codechef.com/problems/ANCT template ::max() / 2> struct MinCostFlow { template struct csr { std::vector start; std::vector elist; explicit csr(int n, const std::vector> &edges) : start(n + 1), elist(edges.size()) { for (auto e : edges) { start[e.first + 1]++; } for (int i = 1; i <= n; i++) { start[i] += start[i - 1]; } auto counter = start; for (auto e : edges) { elist[counter[e.first]++] = e.second; } } }; public: MinCostFlow() {} explicit MinCostFlow(int n) : is_dual_infeasible(false), _n(n) { static_assert(std::numeric_limits::max() > 0, "max() must be greater than 0"); } 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); if (cost < 0) is_dual_infeasible = true; int m = int(_edges.size()); _edges.push_back({from, to, cap, 0, cost}); return m; } struct edge { int from, to; Cap cap, flow; Cost cost; }; edge get_edge(int i) { int m = int(_edges.size()); assert(0 <= i && i < m); return _edges[i]; } std::vector edges() { return _edges; } 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> slope(int s, int t, Cap flow_limit) { assert(0 <= s && s < _n); assert(0 <= t && t < _n); assert(s != t); int m = int(_edges.size()); std::vector edge_idx(m); auto g = [&]() { std::vector degree(_n), redge_idx(m); std::vector> elist; elist.reserve(2 * m); for (int i = 0; i < m; i++) { auto e = _edges[i]; edge_idx[i] = degree[e.from]++; redge_idx[i] = degree[e.to]++; elist.push_back({e.from, {e.to, -1, e.cap - e.flow, e.cost}}); elist.push_back({e.to, {e.from, -1, e.flow, -e.cost}}); } auto _g = csr<_edge>(_n, elist); for (int i = 0; i < m; i++) { auto e = _edges[i]; edge_idx[i] += _g.start[e.from]; redge_idx[i] += _g.start[e.to]; _g.elist[edge_idx[i]].rev = redge_idx[i]; _g.elist[redge_idx[i]].rev = edge_idx[i]; } return _g; }(); auto result = slope(g, s, t, flow_limit); for (int i = 0; i < m; i++) { auto e = g.elist[edge_idx[i]]; _edges[i].flow = _edges[i].cap - e.cap; } return result; } private: bool is_dual_infeasible; int _n; std::vector _edges; // inside edge struct _edge { int to, rev; Cap cap; Cost cost; }; std::vector> slope(csr<_edge> &g, int s, int t, Cap flow_limit) { // 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_dist[i] = (dual[i], dist[i]) std::vector> dual_dist(_n); if (is_dual_infeasible) { auto check_dag = [&]() { std::vector deg_in(_n); for (int v = 0; v < _n; v++) { for (int i = g.start[v]; i < g.start[v + 1]; i++) { deg_in[g.elist[i].to] += g.elist[i].cap > 0; } } std::vector st; st.reserve(_n); for (int i = 0; i < _n; i++) { if (!deg_in[i]) st.push_back(i); } for (int n = 0; n < _n; n++) { if (int(st.size()) == n) return false; // Not DAG int now = st[n]; for (int i = g.start[now]; i < g.start[now + 1]; i++) { const auto &e = g.elist[i]; if (!e.cap) continue; deg_in[e.to]--; if (deg_in[e.to] == 0) st.push_back(e.to); if (dual_dist[e.to].first >= dual_dist[now].first + e.cost) dual_dist[e.to].first = dual_dist[now].first + e.cost; } } return true; }(); if (!check_dag) throw; auto dt = dual_dist[t].first; for (int v = 0; v < _n; v++) dual_dist[v].first -= dt; dbg(dual_dist); is_dual_infeasible = false; } std::vector prev_e(_n); std::vector vis(_n); struct Q { Cost key; int to; bool operator<(Q r) const { return key > r.key; } }; std::vector que_min; std::vector que; auto dual_ref = [&]() { for (int i = 0; i < _n; i++) { dual_dist[i].second = std::numeric_limits::max(); } std::fill(vis.begin(), vis.end(), false); que_min.clear(); que.clear(); // que[0..heap_r) was heapified unsigned heap_r = 0; dual_dist[s].second = 0; que_min.push_back(s); while (!que_min.empty() || !que.empty()) { int v; if (!que_min.empty()) { v = que_min.back(); que_min.pop_back(); } else { while (heap_r < que.size()) { heap_r++; std::push_heap(que.begin(), que.begin() + heap_r); } v = que.front().to; std::pop_heap(que.begin(), que.end()); que.pop_back(); heap_r--; } 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 Cost dual_v = dual_dist[v].first, dist_v = dual_dist[v].second; for (int i = g.start[v]; i < g.start[v + 1]; i++) { auto e = g.elist[i]; if (!e.cap) continue; // |-dual[e.to] + dual[v]| <= (n-1)C // cost <= C - -(n-1)C + 0 = nC Cost cost = e.cost - dual_dist[e.to].first + dual_v; assert(cost >= 0); if (dual_dist[e.to].second - dist_v > cost) { Cost dist_to = dist_v + cost; dual_dist[e.to].second = dist_to; prev_e[e.to] = e.rev; if (dist_to == dist_v) { que_min.push_back(e.to); } else { que.push_back(Q{dist_to, e.to}); } } } } 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_dist[v].first -= dual_dist[t].second - dual_dist[v].second; } return true; }; Cap flow = 0; Cost cost = 0, prev_cost_per_flow = -1; std::vector> result = {{Cap(0), Cost(0)}}; while (flow < flow_limit) { if (!dual_ref()) break; Cap c = flow_limit - flow; for (int v = t; v != s; v = g.elist[prev_e[v]].to) { c = std::min(c, g.elist[g.elist[prev_e[v]].rev].cap); } for (int v = t; v != s; v = g.elist[prev_e[v]].to) { auto &e = g.elist[prev_e[v]]; e.cap += c; g.elist[e.rev].cap -= c; } Cost d = -dual_dist[s].first; 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; } }; int main() { cin.tie(nullptr), ios::sync_with_stdio(false); int N, K; cin >> N >> K; constexpr lint INF = 1LL << 60; MinCostFlow mcf(N + 1); REP(i, N) mcf.add_edge(i, i + 1, K, 0); vector A(N + 1); FOR(i, 1, N + 1) { int m; cin >> A[i] >> m; while (m--) { int b; cin >> b; mcf.add_edge(b, i, 1, -A[i] + A[b]); } } cout << -mcf.flow(0, N, K).second << '\n'; }