# https://atcoder.jp/contests/practice2/submissions/20652625 import heapq class mcf_graph: def __init__(self, n): self.n = n self._edges = [] def add_edge(self, from_, to, cap, cost): # assert 0 <= from_ < self.n # assert 0 <= to < self.n # assert 0 <= cap # assert 0 <= cost m = len(self._edges) self._edges.append(self.__class__.edge(from_, to, cap, 0, cost)) return m class edge: def __init__(self, from_, to, cap, flow, cost): self.from_ = from_ self.to = to self.cap = cap self.flow = flow self.cost = cost def get_edge(self, i): # m = len(self._edges) # assert 0 <= i < m return self._edges[i] def edges(self): return self._edges.copy() def _dual_ref(self, s, t): self.dist = [float('inf')] * self.n self.vis = [False] * self.n que_min = [] que = [] que_push_que = [] self.dist[s] = 0 que_min.append(s) while que_min or que or que_push_que: if que_min: v = que_min.pop() else: for e in que_push_que: heapq.heappush(que, e) que_push_que.clear() _, v = heapq.heappop(que) if self.vis[v]: continue self.vis[v] = True if v == t: break dual_v = self.dual[v] dist_v = self.dist[v] for i in range(self.start[v], self.start[v + 1]): e = self.elist[i] if not e.cap: continue cost = e.cost - self.dual[e.to] + dual_v if self.dist[e.to] - dist_v > cost: dist_to = dist_v + cost self.dist[e.to] = dist_to self.prev_e[e.to] = e.rev if dist_to == dist_v: que_min.append(e.to) else: que_push_que.append((dist_to, e.to)) if not self.vis[t]: return False for v in range(self.n): if not self.vis[v]: continue self.dual[v] -= self.dist[t] - self.dist[v] return True def _csr(self): m = len(self._edges) self.edge_idx = [0] * m redge_idx = [0] * m degree = [0] * self.n edges = [] for i, e in enumerate(self._edges): self.edge_idx[i] = degree[e.from_] degree[e.from_] += 1 redge_idx[i] = degree[e.to] degree[e.to] += 1 edges.append((e.from_, self.__class__._edge( e.to, -1, e.cap - e.flow, e.cost))) edges.append((e.to, self.__class__._edge( e.from_, -1, e.flow, -e.cost))) self.start = [0] * (self.n + 1) self.elist = [0] * len(edges) for v, w in edges: self.start[v + 1] += 1 for i in range(1, self.n + 1): self.start[i] += self.start[i-1] counter = self.start.copy() for v, w in edges: self.elist[counter[v]] = w counter[v] += 1 for i, e in enumerate(self._edges): self.edge_idx[i] += self.start[e.from_] redge_idx[i] += self.start[e.to] self.elist[self.edge_idx[i]].rev = redge_idx[i] self.elist[redge_idx[i]].rev = self.edge_idx[i] def slope(self, s, t, flow_limit=float('inf')): # assert 0 <= s < self.n # assert 0 <= t < self.n # assert s != t self._csr() self.dual = [0] * self.n self.dist = [float('inf')] * self.n self.prev_e = [0] * self.n self.vis = [False] * self.n flow = 0 cost = 0 prev_cost_per_flow = -1 result = [(0, 0)] while flow < flow_limit: if not self._dual_ref(s, t): break c = flow_limit - flow v = t while v != s: c = min(c, self.elist[self.elist[self.prev_e[v]].rev].cap) v = self.elist[self.prev_e[v]].to v = t while v != s: e = self.elist[self.prev_e[v]] e.cap += c self.elist[e.rev].cap -= c v = self.elist[self.prev_e[v]].to d = -self.dual[s] flow += c cost += c * d if prev_cost_per_flow == d: result.pop() result.append((flow, cost)) prev_cost_per_flow = d for i in range(len(self._edges)): e = self.elist[self.edge_idx[i]] self._edges[i].flow = self._edges[i].cap - e.cap return result def flow(self, s, t, flow_limit=float('inf')): return self.slope(s, t, flow_limit)[-1] class _edge: def __init__(self, to, rev, cap, cost): self.to = to self.rev = rev self.cap = cap self.cost = cost def main(): BIG = 10 ** 9 import sys input = sys.stdin.readline N, K = map(int, input().split()) mcf = mcf_graph(N + 3) s = N + 2 t = N + 1 L = 10 ** 10 mcf.add_edge(s, 1, K, L) A = [0] * (N + 1) for i in range(1, N + 1): a, M = map(int, input().split()) A[i] = a B = list(map(int, input().split())) mcf.add_edge(i, i + 1, K, L) for b in B: if a - A[b] > 0: mcf.add_edge(b, i, 1, L * (i - b) - (a - A[b])) f = mcf.flow(s, t, K) print(L * (N + 1) * K - f[1]) if __name__ == "__main__": main()