// Enter your code here use std::io; fn max_transform(array: Vec) -> Vec{ // for a growing window of the array // k is the width let mut b: Vec = Vec::new(); for k in 0..(array.len()){ // for all but the last k elements for i in 0..(array.len() - k ) { let j = i + k +1; // a growing window size // return the biggest element found in the window let &val = array[i..j].iter().max().expect("Empty Iter"); b.push(val); //println!("{:?}, {}, {}, {:?}", array, i, j, b.last()); } } return b } fn main(){ let stdin = io::stdin(); let mut first_line = String::new(); stdin.read_line(&mut first_line).unwrap(); let _array_length = first_line.trim().parse::().expect("Bad Array Length"); let mut second_line = String::new(); stdin.read_line(&mut second_line).unwrap(); // Potentially large do not collect let init_array: Vec = second_line.split_whitespace().map(|x| x.parse::().expect("Bad Integer")).collect(); // return sum of S(S(A)) let result:u64 = max_transform(max_transform(init_array)).iter().sum(); println!("{:?}", result); }