use std::cmp; fn read() -> T { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().parse().ok().unwrap() } fn read_vec() -> Vec { read::().split_whitespace() .map(|e| e.parse().ok().unwrap()).collect() } fn read_vec2(n: usize) -> Vec> { (0..n).map(|_| read_vec()).collect() } fn main() { let nm: Vec = read_vec(); let n = nm[0]; let m = nm[1]; let a: Vec> = read_vec2(n); let mut ans: usize = 9999999; let mut total: usize = 0; for i in 0..n { for j in 0..n { total += a[i][j]; } } for i in 0..1 << (n + 2) { let mut a0 = a.clone(); let cost; let mut count = 0; for j in 0..(n + 2) { if (1 << j) & i == 0 && j < n { a0[j] = vec![0; n]; count += 1; } else if (1 << j) & i == 0 && j == n { for k in 0..n { a0[k][k] = 0; } count += 1; } else if (1 << j) & i == 0 && j > n { for k in 0..n { a0[k][n - k - 1] = 0; } count += 1; } } //println!("count={}, a = {:?}", count, a0); if count > m { //println!("count > m"); continue; } else { let mut line_sum: Vec = Vec::new(); for j in 0..n { let mut sum = 0; for k in 0..n { sum += a0[k][j]; } line_sum.push(sum); } line_sum.sort_unstable(); for j in 0..cmp::min(m - count, n) { line_sum[j] = 0 } let mut remain = 0; for j in 0..n { remain += line_sum[j]; } cost = total - remain; if ans > cost { ans = cost; } //println!("cost = {}, ans = {}", cost, ans); } } println!("{}", ans); }