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 fn kitamasa(a: &[T], b: &[T], mut n: usize, zero: T, one: T, add: F, mul: G) -> T where T: Clone, F: Fn(&T, &T) -> T, G: Fn(&T, &T) -> T, { assert!(a.len() == b.len() && a.len() > 0); let k = a.len(); let calc = |x: &[T], y: &[T]| -> Vec { let mut z = vec![zero.clone(); x.len() + y.len() - 1]; for (i, x) in x.iter().enumerate() { for (z, y) in z[i..].iter_mut().zip(y.iter()) { *z = add(z, &mul(x, y)); } } 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 = add(z, &mul(&p, b)); } } z }; let mut t = vec![zero.clone()]; let mut s = vec![zero.clone()]; t[0] = one.clone(); if k > 1 { s.push(one); } else { s[0] = b[0].clone(); } while n > 0 { if n & 1 == 1 { t = calc(&t, &s); } s = calc(&s, &s); n >>= 1; } let mut ans = zero; for (t, a) in t.iter().zip(a.iter()) { ans = add(&ans, &mul(t, a)); } ans } fn main() { let (n, a, b) = read(); let inf = 10i64.pow(18) + 1; let ans = kitamasa(&a, &b, n, -inf, inf, |a, b| std::cmp::max(*a, *b), |a, b| std::cmp::min(*a, *b)); println!("{}", ans); }