#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};
 
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };
 
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
 
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
 
    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };
 
    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };
 
    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}
 
use std::cmp::Ordering;
use std::cmp;
use std::cmp::min;
use std::collections::BTreeMap;
use std::process;
use std::cmp::Ord;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::collections::BTreeSet;
use std::mem;

/// Equivalent to std::lowerbound and std::upperbound in c++
pub trait BinarySearch<T> {
    fn lower_bound(&self, &T) -> usize;
    fn upper_bound(&self, &T) -> usize;
}
 
impl<T: Ord> BinarySearch<T> for [T] {
    fn lower_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();
 
        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less => {
                    low = mid + 1;
                }
                Ordering::Equal | Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }
 
    fn upper_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();
 
        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less | Ordering::Equal => {
                    low = mid + 1;
                }
                Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }
}
 
 
 
 
 
 
pub static MOD:i64 = 1000000007;
fn main(){
    input!{
        n:usize,
        a:[i64;n],
    }
    let mut a = a;

    let mut dp = vec![vec![vec![-1;2];n+1];n+1];
    fn dfs(a:&Vec<i64>, dp:&mut Vec<Vec<Vec<i64>>>, now:usize, l:i64, r:i64) -> Vec<i64>{
        if l>r{
            return vec![0, 0];
        }
        if dp[l as usize][r as usize][now] != -1{
            return dp[l as usize][r as usize].clone();
        }
        if now == 0{
            let mut diff = 0;
            let mut rv = dfs(a, dp, now^1, l+1, r);
            rv[0] += a[l as usize];
            let mut rv2 = dfs(a, dp, now^1,l, r-1);
            rv2[0] += a[r as usize];
            if rv2[0] - rv2[1]>=rv[0] - rv[1]{
                rv[0] = rv2[0];
                rv[1] = rv2[1];
            }
            dp[l as usize][r as usize][0] = rv[0];
            dp[l as usize][r as usize][1] = rv[1];
            return rv;
        }
        else{
            let mut diff = 0;
            let mut rv = dfs(a, dp, now^1, l+1, r);
            rv[1] += a[l as usize];
            let mut rv2 = dfs(a, dp, now^1,l, r-1);
            rv2[1] += a[r as usize];
            if rv2[0] - rv2[1]<=rv[0] - rv[1]{
                rv[0] = rv2[0];
                rv[1] = rv2[1];
            }
            dp[l as usize][r as usize][0] = rv[0];
            dp[l as usize][r as usize][1] = rv[1];
            return rv;
        }
    }
    dfs(&a, &mut dp, 0,0,n as i64-1);
    println!("{:?}", dp[0][n-1][0] - dp[0][n-1][1]);
}



Measured memory_bytes: 406268