#include #include #include #include #include #include #include #include #include #include #include #define rep(i,n,s) for(int i = (s); i < int(n); i++) #define MM << " " << #define all(x) x.begin(), x.end() template using MinHeap = std::priority_queue, std::greater>; using ll = long long; using Pii = std::pair; using Pll = std::pair; template bool chmin(T& a, const T b) { if(a > b) { a = b; return true; } else { return false; } } template bool chmax(T& a, const T b) { if(a < b) { a = b; return true; } else { return false; } } template void vdeb(std::vector &da) { auto n = da.size(); for(size_t i = 0; i < n; i++) { if(i == n-1) std::cout << da[i] << std::endl; else std::cout << da[i] << " "; } } template<> void vdeb(std::vector &da) { auto n = da.size(); for(size_t i = 0; i < n; i++) { std::cout << da[i] << std::endl; } } using namespace std; // the class of solver for min const from problem // author: tokusakurai // refernce: https://github.com/tokusakurai/Library/blob/main/Graph/Primal-Dual-2.hpp 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; Min_Cost_Flow(int n) : es(n), d(n), h(n), pre_v(n), pre_e(n), n(n) {} 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); if (cost < 0) negative = true; } void bellman_ford(int s) { fill(begin(h), end(h), INF_T); h[s] = 0; while (true) { bool update = false; for (int i = 0; i < n; i++) { if (h[i] == INF_T) continue; for (auto &e : es[i]) { if (e.cap > 0 && h[i] + e.cost < h[e.to]) { h[e.to] = h[i] + e.cost; update = true; } } } if (!update) break; } } 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; } }; const int INF = 100; int main() { int n, k; cin >> n >> k; Min_Cost_Flow mcf(n*2+2); mcf.add_edge(0, 1, INF, 0); rep(i,n,0) { mcf.add_edge(i*2+1, i*2+3, INF, 0); ll a; cin >> a; mcf.add_edge(i*2+2, i*2+1, INF, -a); mcf.add_edge(i*2+1, i*2+2, INF, a); int m; cin >> m; rep(j,m,0) { int b; cin >> b; --b; mcf.add_edge(b*2+2, i*2+2, 1, 0); } } auto ans = mcf.min_cost_flow(0, n*2+1, k); cout << -ans << endl; }