use std::str::FromStr; use std::io::{BufRead, stdin, Stdin}; use std::collections::{VecDeque}; struct Scanner { sep: char, inputs: VecDeque, cin: Stdin, } #[allow(unused)] impl Scanner { pub fn new(ch: char) -> Scanner { Scanner { sep: ch, inputs: VecDeque::new(), cin: stdin(), } } pub fn read(&mut self) -> T { let mut tries = 0; while bool::from(self.inputs.is_empty()) { if tries == 3 { panic!("3 Blank lines found."); } let cin_base = &mut self.cin; let cin = &mut cin_base.lock(); let mut str: String = String::new(); cin.read_line(&mut str).unwrap(); for s in str.trim_end().split(self.sep) { &self.inputs.push_back(String::from(s)); } tries += 1; } let s: String = String::from(&self.inputs.pop_front().unwrap()); s.parse::().ok() .expect("Failed to parse token.") } pub fn read_i32(&mut self) -> i32 { self.read() } pub fn read_i64(&mut self) -> i32 { self.read() } pub fn read_usize(&mut self) -> usize { self.read() } pub fn read_vec(&mut self, n: usize) -> Vec { (0..n).map(|_| self.read()).collect::>() } } fn main() { let mut sc = Scanner::new(' '); let n = sc.read_usize(); let s = sc.read::().chars().collect::>(); let a = sc.read_vec::(n); let mut score = vec![0i64; n]; score[0] = if s[0] == 'R' { a[0] } else { -a[0] }; for i in 1..n { score[i] = score[i - 1] + if s[i] == 'R' { a[i] } else { -a[i] }; } let mut current_min = 0i64; let mut current_max = 0i64; let mut ans = 0; for i in 0..n { ans = ans.max(&score[i] - current_min).max((&score[i] - current_max).abs()); // eprintln!("{}", nas); current_max = current_max.max(score[i]); current_min = current_min.min(score[i]); } println!("{}", ans); }