#include using namespace std; #define rep(i,n) for(ll i=0;i=0;i--) #define perl(i,r,l) for(ll i=r-1;i>=l;i--) #define fi first #define se second #define pb push_back #define ins insert #define pqueue(x) priority_queue,greater> #define all(x) (x).begin(),(x).end() #define CST(x) cout<> #define rev(x) reverse(x); using ll=long long; using vl=vector; using vvl=vector>; using pl=pair; using vpl=vector; using vvpl=vector; const ll MOD=1000000007; const ll MOD9=998244353; const int inf=1e9+10; //const ll INF=4e18; const ll dy[9]={0,1,-1,0,1,1,-1,-1,0}; const ll dx[9]={1,0,0,-1,1,-1,1,-1,0}; template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } using Flow = ll; using Cost = ll; const int MAX_V = 50010; const Cost INF = std::numeric_limits::max() / 8; struct PrimalDual { struct Edge { int d; Flow c, f; Cost w; int r, is_r; Edge(int d_, Flow c_, Flow f_, Cost w_, int r_, bool is_r_) : d(d_), c(c_), f(f_), w(w_), r(r_), is_r(is_r_) {} }; int n; std::vector > g; PrimalDual(int n_) : n(n_), g(std::vector >(n_)) {} void add_edge(int src, int dst, Flow cap, Cost cost) { // 有向辺 int rsrc = g[dst].size(); int rdst = g[src].size(); g[src].emplace_back(dst, cap, 0, cost, rsrc, false); g[dst].emplace_back(src, cap, cap, -cost, rdst, true); } Cost solve(int s, int t, Flow f) { Cost res = 0; static Cost h[MAX_V + 10], dist[MAX_V]; static int prevv[MAX_V + 10], preve[MAX_V + 10]; // std::vector h(g.size()), dist(g.size()); // std::vector prevv(g.size()), preve(g.size()); using pcv = std::pair; std::priority_queue, std::greater > q; std::fill(h, h + n, 0); while (f > 0) { std::fill(dist, dist + n, INF); dist[s] = 0; q.emplace(0, s); while (q.size()) { Cost cd; int v; std::tie(cd, v) = q.top(); q.pop(); if (dist[v] < cd) continue; for (int i = 0; i < (int)(g[v].size()); ++i) { Edge &e = g[v][i]; if (residue(e) == 0) continue; if (dist[e.d] + h[e.d] > cd + h[v] + e.w) { dist[e.d] = dist[v] + e.w + h[v] - h[e.d]; prevv[e.d] = v; preve[e.d] = i; q.emplace(dist[e.d], e.d); } } } if (dist[t] == INF) return -1; // 経路が見つからなかった // s-t 間最短路に沿って目一杯流す for (int i = 0; i < n; ++i) h[i] += dist[i]; Flow d = f; for (int v = t; v != s; v = prevv[v]) { d = std::min(d, residue(g[prevv[v]][preve[v]])); } f -= d; res += d * h[t]; for (int v = t; v != s; v = prevv[v]) { Edge &e = g[prevv[v]][preve[v]]; e.f += d; g[v][e.r].f -= d; } } return res; } Flow residue(const Edge &e) { return e.c - e.f; } // 流量を表示 void show() { for (int i = 0; i < n; ++i) { for (int j = 0; j < (int)(g[i].size()); ++j) { Edge &e = g[i][j]; if (e.is_r) continue; printf("%3d->%3d (flow:%d)\n", i, e.d, e.f); } } } }; int main(){ ll n,k;cin >> n >> k; PrimalDual pd(n); vl a(n); rep(i,n){ cin >> a[i]; ll m;cin >> m; rep(j,m){ ll t;cin >> t;t--; pd.add_edge(t,i,1,a[t]-a[i]); } } rep(i,n-1)pd.add_edge(i,i+1,inf,0); cout << -pd.solve(0,n-1,k) << endl; }