pub mod lower_bound { pub fn lower_bound(list: &[T], val: &T) -> usize { let mut l = 0; let mut r = list.len(); while l < r { let x = (l + r) / 2; match list[x].cmp(val) { std::cmp::Ordering::Less => { l = x + 1; } std::cmp::Ordering::Equal | std::cmp::Ordering::Greater => { r = x; } } } l } pub fn upper_bound(list: &[T], val: &T) -> usize { let mut l = 0; let mut r = list.len(); while l < r { let x = (l + r) / 2; match list[x].cmp(val) { std::cmp::Ordering::Less | std::cmp::Ordering::Equal => { l = x + 1; } std::cmp::Ordering::Greater => { r = x; } } } l } } pub mod scanner { pub struct Scanner { buf: Vec, } impl Scanner { pub fn new() -> Self { Self { buf: vec![] } } pub fn new_from(source: &str) -> Self { let source = String::from(source); let buf = Self::split(source); Self { buf } } pub fn next(&mut self) -> T { loop { if let Some(x) = self.buf.pop() { return x.parse().ok().expect(""); } let mut source = String::new(); std::io::stdin().read_line(&mut source).expect(""); self.buf = Self::split(source); } } fn split(source: String) -> Vec { source .split_whitespace() .rev() .map(String::from) .collect::>() } } } use crate::{lower_bound::upper_bound, scanner::Scanner}; fn main() { let mut scanner = Scanner::new(); let t: usize = 1; for _ in 0..t { solve(&mut scanner); } } fn solve(scanner: &mut Scanner) { let n: usize = scanner.next(); let m: usize = scanner.next(); let k: usize = scanner.next(); let mut a = vec![0; n]; for i in 0..n { let x = scanner.next::(); a[i] = x; } if n == 0 || m == 0 { println!("0"); return; } let mut b = vec![vec![]; 4]; for i in 0..n { b[1].push(a[i]); for j in 0..n { b[2].push(a[i] + a[j]); for k in 0..n { b[3].push(a[i] + a[j] + a[k]); } } } for i in 0..4 { b[i].push(0); b[i].sort(); } let mut ans = 0; for ki in 1..=k { let mut bi = 0; let mut bj = ki; if bj > 3 { bi = bj - 3; bj = 3; } for &x in b[bi].iter() { if m < x { break; } let i = upper_bound(&b[bj], &(m - x)) - 1; let y = b[bj][i]; if x + y <= m { ans = ans.max(x + y); } } } println!("{}", ans); }