#include using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, x, n) for (int i = x; i <= n; i++) #define rep3(i, x, n) for (int i = x; i >= n; i--) #define each(e, v) for (auto &e : v) #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) (int)x.size() using ll = long long; using pii = pair; using pil = pair; using pli = pair; using pll = pair; const int MOD = 1000000007; // const int MOD = 998244353; const int inf = (1 << 30) - 1; const ll INF = (1LL << 60) - 1; template bool chmax(T &x, const T &y) { return (x < y) ? (x = y, true) : false; } template bool chmin(T &x, const T &y) { return (x > y) ? (x = y, true) : false; } struct io_setup { io_setup() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); } } io_setup; template struct Min_Cost_Flow { struct edge { int to; F cap; T cost; int rev; edge(int to, F cap, T cost, int rev) : to(to), cap(cap), cost(cost), rev(rev) {} }; vector> es; vector d, h; vector pre_v, pre_e; bool negative = false; const F INF_F = numeric_limits::max() / 2; const T INF_T = numeric_limits::max() / 2; const int n; vector deg; Min_Cost_Flow(int n) : es(n), d(n), h(n), pre_v(n), pre_e(n), n(n), deg(n, 0) {} void add_edge(int from, int to, F cap, T cost) { es[from].emplace_back(to, cap, cost, (int)es[to].size()); es[to].emplace_back(from, 0, -cost, (int)es[from].size() - 1); deg[to]++; if (cost < 0) negative = true; } void bellman_ford(int s) { fill(begin(h), end(h), INF_T); h[s] = 0; queue que; rep(i, n) { if (deg[i] == 0) que.emplace(i); } while (!empty(que)) { int i = que.front(); que.pop(); each(e, es[i]) { if (e.cap == 0) continue; chmin(h[e.to], h[i] + e.cost); if (--deg[e.to] == 0) que.emplace(e.to); } } } void dijkstra(int s) { fill(begin(d), end(d), INF_T); using P = pair; priority_queue, greater

> que; que.emplace(d[s] = 0, s); while (!que.empty()) { auto [p, i] = que.top(); que.pop(); if (p > d[i]) continue; for (int j = 0; j < (int)es[i].size(); j++) { edge &e = es[i][j]; if (e.cap > 0 && d[i] + e.cost + h[i] - h[e.to] < d[e.to]) { d[e.to] = d[i] + e.cost + h[i] - h[e.to]; pre_v[e.to] = i, pre_e[e.to] = j; que.emplace(d[e.to], e.to); } } } } T min_cost_flow(int s, int t, F flow) { T ret = 0; if (negative) bellman_ford(s); while (flow > 0) { dijkstra(s); if (d[t] == INF_T) return -1; for (int i = 0; i < n; i++) { if (h[i] == INF_T || d[i] == INF_T) h[i] = INF_T; else h[i] += d[i]; } F f = flow; for (int now = t; now != s; now = pre_v[now]) { f = min(f, es[pre_v[now]][pre_e[now]].cap); } ret += h[t] * f, flow -= f; for (int now = t; now != s; now = pre_v[now]) { edge &e = es[pre_v[now]][pre_e[now]]; e.cap -= f, es[now][e.rev].cap += f; } } return ret; } }; int main() { int N, K; cin >> N >> K; Min_Cost_Flow G(N + 2); vector A(N); int s = N, t = N + 1; rep(i, N) { cin >> A[i]; int M; cin >> M; rep(j, M) { int x; cin >> x; x--; G.add_edge(x, i, 1, -A[i] + A[x]); } G.add_edge(s, i, K, A[i]); } G.add_edge(s, 0, K, 0); rep(i, N - 1) G.add_edge(i, i + 1, K, 0); G.add_edge(N - 1, t, K, 0); cout << -G.min_cost_flow(s, t, K) << '\n'; }