use std::io::Read; use std::collections::HashSet; fn main() { let mut all_data = String::new(); std::io::stdin().read_to_string(&mut all_data).ok(); let mut all_data = all_data.trim().split('\n').map(|s| s.trim()); let nmk: Vec = all_data.next().unwrap().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nmk[0]; let m = nmk[1]; let k = nmk[2]; let a: Vec> = all_data.take(n).map(|s| s.split_whitespace().map(|t| t.parse().unwrap()).collect::>()).collect(); let mut min_sum: usize= a.iter().map(|i| i[0]).sum(); let mut max_sum: usize= a.iter().map(|i| i[m-1]).sum(); let mut result_list: Vec = vec![]; for i in 0..n { min_sum -= a[i][0]; max_sum -= a[i][m-1]; let mut temp_result_list: Vec = vec![]; let mut temp_result_set: HashSet = HashSet::new(); for j in 0..m { let adder = a[i][j]; for val in result_list.iter().rev() { if min_sum + val + adder > k { //nothing to do } else if temp_result_set.contains(&(val + adder)) { //nothing to do } else { temp_result_list.push(val + adder); temp_result_set.insert(val + adder); if max_sum + val + adder <= k { break; } } } if i == 0 { if adder > k { //nothing to do } else { temp_result_list.push(adder); temp_result_set.insert(adder); } } } temp_result_list.sort(); result_list = temp_result_list; } if result_list.is_empty() { println!("{}", -1); } else { println!("{}", k - result_list[result_list.len() - 1]); } }