@n = gets.to_i
@a = gets.split.map(&:to_i)
m = @n.odd? ? 1 : -1

@memo = Array.new(@n) {Array.new(@n)}
@n.times {|i| @memo[i][i] = @a[i] * m}

def calc(i, j)
    return @memo[i][j] if @memo[i][j]

    if (@n - j + i - 1).even?
        x = @a[i] + calc(i+1, j)
        y = @a[j] + calc(i, j-1)
        @memo[i][j] = x > y ? x : y
    else
        x = -@a[i] + calc(i+1, j)
        y = -@a[j] + calc(i, j-1)
        @memo[i][j] = x < y ? x : y
    end
end

p calc(0, @n-1)