const INF: i64 = 1_000_000_000_000_000i64 + 1; struct Graph { size: usize, edge: Vec<(usize, usize, i64, i64)>, } impl Graph { fn new(size: usize) -> Self { Graph { size: size, edge: vec![], } } fn add_edge(&mut self, src: usize, dst: usize, capa: i64, cost: i64) { assert!(src < self.size && dst < self.size && src != dst); self.edge.push((src, dst, capa, cost)); } fn flow(&self, src: usize, dst: usize, flow: i64) -> Vec<(i64, i64)> { assert!(src != dst); let size = self.size; let edge = &self.edge; let mut deg = vec![0; size]; for &(a, b, _, _) in edge.iter() { deg[a] += 1; deg[b] += 1; } let mut graph: Vec<_> = deg.into_iter().map(|d| Vec::with_capacity(d)).collect(); for &(a, b, capa, cost) in edge.iter() { let x = graph[a].len(); let y = graph[b].len(); graph[a].push((b, capa, cost, y)); graph[b].push((a, 0, -cost, x)); } let mut sum = 0; let mut res = vec![]; let mut used = 0; let mut dp = Vec::with_capacity(size); let mut pot = vec![0; size]; let mut h = std::collections::BinaryHeap::new(); while used < flow { dp.clear(); dp.resize(size, (INF, src, 0)); // コスト、親、親からの番号 dp[src] = (0, src, 0); h.push((0, src)); while let Some((d, v)) = h.pop() { let d = -d; if d > dp[v].0 { continue; } for (i, &(u, capa, cost, _)) in graph[v].iter().enumerate() { if capa == 0 { continue; } let c = d + cost - pot[u] + pot[v]; if c < dp[u].0 { dp[u] = (c, v, i); h.push((-c, u)); } } } if dp[dst].0 == INF { break; } for i in 0..size { pot[i] -= dp[dst].0 - dp[i].0; } let mut sub = 1; let mut pos = dst; while pos != src { let (_, parent, k) = dp[pos]; sub = std::cmp::min(sub, graph[parent][k].1); pos = parent; } let mut pos = dst; while pos != src { let (_, parent, k) = dp[pos]; let inv = graph[parent][k].3; graph[parent][k].1 -= sub; graph[pos][inv].1 += sub; pos = parent; } used += sub; sum += -pot[src] * sub; res.push((used, sum)); } res } } // ---------- begin scannner ---------- #[allow(dead_code)] mod scanner { use std::str::FromStr; pub struct Scanner<'a> { it: std::str::SplitWhitespace<'a>, } impl<'a> Scanner<'a> { pub fn new(s: &'a String) -> Scanner<'a> { Scanner { it: s.split_whitespace(), } } pub fn next(&mut self) -> T { self.it.next().unwrap().parse::().ok().unwrap() } pub fn next_bytes(&mut self) -> Vec { self.it.next().unwrap().bytes().collect() } pub fn next_chars(&mut self) -> Vec { self.it.next().unwrap().chars().collect() } pub fn next_vec(&mut self, len: usize) -> Vec { (0..len).map(|_| self.next()).collect() } } } // ---------- end scannner ---------- use std::io::Write; fn main() { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); let mut sc = scanner::Scanner::new(&s); let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); run(&mut sc, &mut out); } fn run(sc: &mut scanner::Scanner, out: &mut std::io::BufWriter) { let n: usize = sc.next(); let k: i64 = sc.next(); let mut g = Graph::new(n + 1); let src = 0; let dst = n; for i in 1..=n { g.add_edge(i - 1, i, k, 0); } let mut a = vec![0i64; n]; for i in 0..n { a[i] = sc.next(); let m: usize = sc.next(); for _ in 0..m { let b = sc.next::() - 1; if a[b] < a[i] { g.add_edge(b, i, 1, -(a[i] - a[b])); } } } let res = g.flow(src, dst, k); let ans = -res.iter().map(|p| p.1).min().unwrap_or(0); writeln!(out, "{}", ans).ok(); }