#include using namespace std; const long long INF = 1000000000000; const long long INF2 = 1000000000000000000; template struct primal_dual{ struct edge{ int to, rev; Cap cap; Cost cost; edge(int to, int rev, Cap cap, Cost cost): to(to), rev(rev), cap(cap), cost(cost){ } }; int N; vector> G; primal_dual(){ } primal_dual(int N): N(N), G(N){ } void add_edge(int from, int to, Cap cap, Cost cost){ int id1 = G[from].size(); int id2 = G[to].size(); G[from].push_back(edge(to, id2, cap, cost)); G[to].push_back(edge(from, id1, 0, - cost)); } pair min_cost_flow(int s, int t, Cap F){ Cap flow = 0; Cost cost = 0; vector h(N, 0); while (flow < F){ vector m(N, INF2); vector d(N, INF2); vector pv(N, -1); vector pe(N, -1); vector used(N, false); priority_queue, vector>, greater>> pq; pq.push(make_pair(0, s)); d[s] = 0; while (!pq.empty()){ int v = pq.top().second; pq.pop(); if (!used[v]){ used[v] = true; if (v == t){ break; } int cnt = G[v].size(); for (int i = 0; i < cnt; i++){ int w = G[v][i].to; if (!used[w] && G[v][i].cap > 0){ Cost tmp = G[v][i].cost - h[w] + h[v]; if (d[w] > d[v] + tmp){ d[w] = d[v] + tmp; m[w] = min(m[v], G[v][i].cap); pv[w] = v; pe[w] = i; pq.push(make_pair(d[w], w)); } } } } } if (!used[t]){ break; } for (int i = 0; i < N; i++){ if (used[i]){ h[i] -= d[t] - d[i]; } } Cap c = min(m[t], F - flow); for (int i = t; i != s; i = pv[i]){ G[pv[i]][pe[i]].cap -= c; G[i][G[pv[i]][pe[i]].rev].cap += c; } flow += c; cost += c * (- h[s]); } return make_pair(flow, cost); } }; int main(){ int N, K; cin >> N >> K; vector A(N), M(N); vector> B(N); for (int i = 0; i < N; i++){ cin >> A[i] >> M[i]; B[i] = vector(M[i]); for (int j = 0; j < M[i]; j++){ cin >> B[i][j]; B[i][j]--; } } primal_dual G(N); for (int i = 0; i < N - 1; i++){ G.add_edge(i, i + 1, K, INF); } for (int i = 0; i < N; i++){ for (int j = 0; j < M[i]; j++){ if (A[i] - A[B[i][j]] > 0){ G.add_edge(B[i][j], i, 1, INF * (i - B[i][j]) - (A[i] - A[B[i][j]])); } } } cout << (N - 1) * K * INF - G.min_cost_flow(0, N - 1, K).second << endl; }