#![allow(non_snake_case)] #![allow(unused_imports)] // use itertools::Itertools; // use proconio::input; // use proconio::marker::*; use std::cmp::Reverse; use std::collections::*; fn main() { input! { T:usize, n:usize, t:[usize;n], } let mut dp = vec![n; 1 << n]; dp[0] = 0; for s in 0..1 << n { let mut time = 0; for i in 0..n { if s & (1 << i) != 0 { time += t[i]; } } if time <= T { dp[s] = 1; } } for s in 0..1 << n { let cfor = CFor::new(s, |&t| t != 0, |&t| (t - 1) & s); for t in cfor.into_iter() { dp[s] = dp[s].min(dp[t] + dp[t ^ s]); } } let ans = dp[(1 << n) - 1]; println!("{}", ans); } struct CFor where T: Copy, F: Fn(&T) -> bool, G: Fn(&T) -> T, { state: T, inited: bool, is_continue: F, update: G, } impl CFor where T: Copy, F: Fn(&T) -> bool, G: Fn(&T) -> T, { fn new(init: T, is_continue: F, update: G) -> Self { Self { state: init, inited: false, is_continue, update, } } } impl Iterator for CFor where T: Copy, F: Fn(&T) -> bool, G: Fn(&T) -> T, { type Item = T; fn next(&mut self) -> Option { if !self.inited { self.inited = true; if (self.is_continue)(&self.state) { return Some(self.state); } else { return None; } } self.state = (self.update)(&self.state); if (self.is_continue)(&self.state) { return Some(self.state); } else { return None; } } } mod input { #[macro_export] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } #[macro_export] macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } #[macro_export] macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } }