use std::collections::HashMap; fn main() { let (n, m) = input_tuple::(); let a_org = input_vec::(); let b = input_vec(); let c = input_vec::(); let mut a = a_org.clone(); let mut list = vec![0; n]; for i in 1..n { if a[i] - a[i - 1] != b[(i - 1) % m] { list[i] = b[(i - 1) % m] + a[i - 1] - a[i]; a[i] = b[(i - 1) % m] + a[i - 1]; } } // println!("{:?}", list); let mut counter = HashMap::new(); for i in 0..n { *counter.entry(list[i]).or_insert(0) += 1; } let (&_, &max_value) = counter.iter().max().unwrap(); let mut max_index = 0; for i in 0..n { if list[i] == max_value { max_index = i; break; } } max_index += 1; eprintln!("max {}", max_index); // let mut result_1 = 0; // let mut a = a_org.clone(); // for i in 1..n { // if a[i] - a[i - 1] != b[(i - 1) % m] { // a[i] = b[(i - 1) % m] + a[i - 1]; // result_1 += c[i]; // } // } // println!("{:?}", a); // println!("{}", result_1); // let mut result_2 = 0; // let mut a = a_org.clone(); // for i in (0..n - 1).rev() { // if a[i + 1] - a[i] != b[i % m] { // a[i] = a[i + 1] - b[i % m]; // result_2 += c[i]; // } // } // // println!("{:?}", a); // println!("{}", result_2); let mut result_3 = 0; let mut a = a_org.clone(); for i in max_index..n { if a[i] - a[i - 1] != b[(i - 1) % m] { a[i] = b[(i - 1) % m] + a[i - 1]; result_3 += c[i]; } } for i in (0..max_index - 1).rev() { if a[i + 1] - a[i] != b[i % m] { a[i] = a[i + 1] - b[i % m]; result_3 += c[i]; } } // println!("{:?}", a); println!("{}", result_3); } fn input_tuple() -> (T, T) where T: std::str::FromStr, ::Err: std::fmt::Debug, { let stdin = std::io::stdin(); let mut buf = String::new(); stdin.read_line(&mut buf).unwrap(); buf = buf.trim_end().to_owned(); let mut iter = buf.split_whitespace(); let n = iter.next().unwrap().parse().unwrap(); let m = iter.next().unwrap().parse().unwrap(); (n, m) } fn input_vec() -> Vec where T: std::str::FromStr, ::Err: std::fmt::Debug, { let stdin = std::io::stdin(); let mut buf = String::new(); stdin.read_line(&mut buf).unwrap(); buf = buf.trim_end().to_owned(); let iter = buf.split_whitespace(); let line = iter.map(|x| x.parse().unwrap()).collect(); line }