#![allow(unused)] use std::{ io::{self, prelude::*}, mem::{replace, swap}, iter, }; pub struct Input { src: R, buf: Vec, pos: usize, } impl Input { pub fn new(src: R) -> Self { Self { src, buf: Vec::new(), pos: 0, } } pub fn input_raw(&mut self) -> &[u8] { loop { self.advance_while(|b| b.is_ascii_whitespace()); if self.pos == self.buf.len() { self.buf.clear(); self.src.read_until(b'\n', &mut self.buf).expect("io error"); self.pos = 0; } else { break; } } let start = self.pos; self.advance_while(|b| !b.is_ascii_whitespace()); &self.buf[start..self.pos] } fn advance_while(&mut self, f: impl Fn(u8) -> bool) { while self.buf.get(self.pos).map_or(false, |b| f(*b)) { self.pos += 1; } } pub fn input(&mut self) -> T { T::input(self) } } pub trait InputParse { fn input(input: &mut Input) -> Self; } macro_rules! input_from_str_impls { { $($T:ty)* } => { $(impl InputParse for $T { fn input(input: &mut Input) -> Self { String::from_utf8_lossy(input.input_raw()) .parse() .expect("parse error") } })* }; } macro_rules! input_tuple_impls { { $(($($T:ident),+))* } => { $(impl<$($T: InputParse),+> InputParse for ($($T),+) { fn input(input: &mut Input) -> Self { ($(input.input::<$T>()),+) } })* }; } input_from_str_impls! { String char bool f32 f64 isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128 } input_tuple_impls! { (A, B) (A, B, C) (A, B, C, D) (A, B, C, D, E) (A, B, C, D, E, F) (A, B, C, D, E, F, G) } macro_rules! output { ($out:expr, $($args:expr),*) => { $out.write_fmt(format_args!($($args),*)) }; } macro_rules! outputln { ($out:expr, $($args:expr),*) => { output!($out, $($args),*); outputln!($out); }; ($out:expr) => { output!($out, "\n"); }; } fn main() { let stdin = io::stdin(); let mut input = Input::new(stdin.lock()); let stdout = io::stdout(); let mut out = io::BufWriter::new(stdout.lock()); let n = input.input(); let mut a: Vec = iter::repeat_with(|| { let (x, y): (i64, i64) = input.input(); x - y }) .take(n) .collect(); let mut lsum = vec![0]; let mut rsum = vec![0]; for x in &a { lsum.push(lsum.last().unwrap() + *x); } for x in a.iter().rev() { rsum.push(rsum.last().unwrap() - *x); } rsum.reverse(); dbg!(&a, &lsum, &rsum); let ans = lsum.iter().zip(rsum).fold(-(1 << 60), |cur, (x, y)| cur.max(x + y)); outputln!(&mut out, "{}", ans); }