Problem:
Score : 500 points

Problem Statement

We have a deck consisting of N cards. Each card has an integer written on it. The integer on the i-th card from the top is a_i.

Two people X and Y will play a game using this deck. Initially, X has a card with Z written on it in his hand, and Y has a card with W written on it in his hand. Then, starting from X, they will alternately perform the following action:

Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.

The game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.

X will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?

Constraints

All input values are integers.

1 \leq N \leq 2000

1 \leq Z, W, a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N Z W
a_1 a_2 ... a_N

Output

Print the score.

Sample Input 1

3 100 100
10 1000 100

Sample Output 1

900

If X draws two cards first, Y will draw the last card, and the score will be |1000 - 100| = 900.

Sample Input 2

3 100 1000
10 100 100

Sample Output 2

900

If X draws all the cards first, the score will be |1000 - 100| = 900.

Sample Input 3

5 1 1
1 1 1 1 1

Sample Output 3

0

Sample Input 4

1 1 1
1000000000

Sample Output 4

999999999

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

#[allow(unused_imports)]
use std::char::from_digit;
#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::fs::File;
#[allow(unused_imports)]
use std::io::prelude::*;
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufReader, BufWriter, Stdin, Stdout, Write};
#[allow(unused_imports)]
use std::mem::swap;
#[allow(unused_imports)]
use std::str::FromStr;
 
type BoxResult<T> = Result<T, Box<std::error::Error>>;
 

fn main() {
    let mut scanner = Scanner::new(stdin());
    let line = scanner.read_vector::<usize>().unwrap();
    let (n, mut z, mut w) = (line[0], line[1] as i64, line[2] as i64);
    let arr = scanner.read_vector::<i64>().unwrap();
    let (min_indices, max_indices) = calc(&arr);
    let mut index = 0;
    let mut x_turn = true;
    while index < n {
        if index == n - 1 {
            if x_turn {
                z = arr[index];
            } else {
                w = arr[index];
            }
        } else {
            if x_turn {
                let (i, val) = min_indices[index];
                if (arr[n - 1] - w).abs() > val {
                    z = arr[n - 1];
                    index = n - 1;
                } else {
                    z = arr[i];
                    index = i;
                }
            } else {
                let (i, val) = max_indices[index];
                if (arr[n - 1] - z).abs() < val {
                    w = arr[n - 1];
                    index = n - 1;
                } else {
                    w = arr[i];
                    index = i;
                }
            }
        }
        index += 1;
        x_turn = !x_turn;
    }
    println!("{}", (z - w).abs());
}

fn calc(arr: &[i64]) -> (Vec<(usize, i64)>, Vec<(usize, i64)>) {
    if arr.len() == 1 {
        return (vec![], vec![]);
    }
    let init = (arr[arr.len() - 2] - arr[arr.len() - 1]).abs();
    let mut abs_min_indices = vec![(arr.len() - 2, init)];
    let mut abs_max_indices = vec![(arr.len() - 2, init)];
    for i in (0..(arr.len() - 2)).rev() {
        let mut local_min = std::i64::MAX;
        let mut local_max = std::i64::MIN;
        for j in (i + 1)..arr.len() {
            let abs = (arr[i] - arr[j]).abs();
            local_min = min(abs, local_min);
            local_max = max(abs, local_max);
        }
        if local_min > abs_min_indices[abs_min_indices.len() - 1].1 {
            abs_min_indices.push((i, local_min));
        } else {
            let last = abs_min_indices[abs_min_indices.len() - 1];
            abs_min_indices.push(last);
        }
        if local_max < abs_max_indices[abs_max_indices.len() - 1].1 {
            abs_max_indices.push((i, local_max));
        } else {
            let last = abs_max_indices[abs_max_indices.len() - 1];
            abs_max_indices.push(last);
        }
    }
    abs_min_indices.reverse();
    abs_max_indices.reverse();
    (abs_min_indices, abs_max_indices)
}

#[allow(dead_code)]
struct Scanner<R> {
    reader: BufReader<R>,
    buffer: String,
}
impl<R> Scanner<R>
where
    R: Read,
{
    #[allow(dead_code)]
    fn new(inner: R) -> Scanner<R> {
        let reader = BufReader::new(inner);
        let buffer = String::new();
        Scanner {
            reader: reader,
            buffer: buffer,
        }
    }
    #[allow(dead_code)]
    fn with_capacity(capacity: usize, inner: R) -> Scanner<R> {
        let reader = BufReader::with_capacity(capacity, inner);
        let buffer = String::new();
        Scanner {
            reader: reader,
            buffer: buffer,
        }
    }
 
    #[allow(dead_code)]
    fn read_line(&mut self) -> BoxResult<String> {
        self.reader.read_line(&mut self.buffer)?;
        let cloned = self.buffer.trim().to_string();
        self.buffer.clear();
        Ok(cloned)
    }
 
    #[allow(dead_code)]
    fn read<T>(&mut self) -> BoxResult<T>
    where
        T: FromStr,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
    {
        self.reader.read_line(&mut self.buffer)?;
        let parsed = self.buffer.trim().parse()?;
        self.buffer.clear();
        Ok(parsed)
    }
    #[allow(dead_code)]
    fn read_vector<T>(&mut self) -> BoxResult<Vec<T>>
    where
        T: FromStr,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
    {
        self.reader.read_line(&mut self.buffer)?;
        let parsed_vec = self
            .buffer
            .trim()
            .split_whitespace()
            .map(|s| s.parse())
            .collect::<Result<Vec<_>, _>>()?;
        self.buffer.clear();
        Ok(parsed_vec)
    }
    #[allow(dead_code)]
    fn read_matrix<T>(&mut self, lines: usize) -> BoxResult<Vec<Vec<T>>>
    where
        T: FromStr,
        T::Err: std::fmt::Debug,
        T::Err: std::error::Error,
        T::Err: 'static,
    {
        let mut parsed_matrix = vec![];
        for _ in 0..lines {
            self.reader.read_line(&mut self.buffer)?;
            parsed_matrix.push(
                self.buffer
                    .trim()
                    .split_whitespace()
                    .map(|s| s.parse())
                    .collect::<Result<Vec<_>, _>>()?,
            );
            self.buffer.clear();
        }
        Ok(parsed_matrix)
    }
    #[allow(dead_code)]
    fn read_chars(&mut self) -> BoxResult<Vec<char>> {
        self.reader.read_line(&mut self.buffer)?;
        let chars = self.buffer.trim().chars().collect::<Vec<_>>();
        self.buffer.clear();
        Ok(chars)
    }
}