use std::collections::VecDeque; fn main() { let _n: u32 = read(); let _m: u32 = read(); let d: VecDeque = read::() .split_whitespace() .map(|x| x.parse().unwrap()) .collect(); let s: String = read(); let r: String = solve(d, s); println!("{}", r); } fn solve(d: VecDeque, s: String) -> String { let mut e = d; for c in s.chars() { if c == 'L' { let a: u32 = e.pop_front().unwrap(); let b: u32 = e.pop_front().unwrap(); e.push_front(a + b); e.push_back(0); } else { let a: u32 = e.pop_back().unwrap(); let b: u32 = e.pop_back().unwrap(); e.push_back(a + b); e.push_front(0); } } e.iter() .map(|i| i.to_string()) .into_iter() .collect::>() .join(" ") } use std::io::*; use std::str::FromStr; /// see https://qiita.com/tubo28/items/e6076e9040da57368845#%E5%85%A5%E5%87%BA%E5%8A%9B fn read() -> T { let stdin = stdin(); let stdin = stdin.lock(); let token: String = stdin .bytes() .map(|c| c.expect("failed to read char") as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect(); token.parse().ok().expect("failed to parse token") }