fn read() -> (usize, Vec, Vec) { let mut s = String::new(); use std::io::Read; std::io::stdin().read_to_string(&mut s).unwrap(); let mut it = s.trim().split_whitespace(); let mut next = || it.next().unwrap().parse::().unwrap(); let k = next() as usize; let n = next() as usize; let a = (0..k).map(|_| next()).collect::>(); let b = (0..k).map(|_| next()).collect::>(); (n, a, b) } pub trait SemiRing { fn add(&self, rhs: &Self) -> Self; fn mul(&self, rhs: &Self) -> Self; fn zero() -> Self; fn one() -> Self; } pub fn kitamasa(a: &[T], b: &[T], mut n: usize) -> T where T: Clone + SemiRing, { assert!(a.len() == b.len() && a.len() > 0); let k = a.len(); let calc = |x: &[T], y: &[T]| -> Vec { let mut z = vec![T::zero(); x.len() + y.len() - 1]; for (i, x) in x.iter().enumerate() { for (z, y) in z[i..].iter_mut().zip(y.iter()) { *z = x.mul(y).add(z); } } for i in (k..z.len()).rev() { let p = z.pop().unwrap(); for (z, b) in z[(i - k)..].iter_mut().zip(b.iter()) { *z = p.mul(&b).add(z); } } z }; let mut t = vec![T::zero()]; let mut s = vec![T::zero(); 2]; t[0] = T::one(); s[1] = T::one(); while n > 0 { if n & 1 == 1 { t = calc(&t, &s); } s = calc(&s, &s); n >>= 1; } let mut ans = T::zero(); for (t, a) in t.iter().zip(a.iter()) { ans = t.mul(a).add(&ans); } ans } impl SemiRing for i64 { fn add(&self, rhs: &Self) -> Self { std::cmp::max(*self, *rhs) } fn mul(&self, rhs: &Self) -> Self { std::cmp::min(*self, *rhs) } fn zero() -> Self { std::i64::MIN } fn one() -> Self { std::i64::MAX } } fn main() { let (n, a, b) = read(); let ans = kitamasa(&a, &b, n); println!("{}", ans); }