Problem:
Score : 100 points

Problem Statement

There are N stones, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), the height of Stone i is h_i.
Here, h_1 < h_2 < \cdots < h_N holds.

There is a frog who is initially on Stone 1.
He will repeat the following action some number of times to reach Stone N:

If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

All values in input are integers.

2 \leq N \leq 2 \times 10^5

1 \leq C \leq 10^{12}

1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6

Input

Input is given from Standard Input in the following format:

N C
h_1 h_2 \ldots h_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

5 6
1 2 3 4 5

Sample Output 1

20

If we follow the path 1 → 3 → 5, the total cost incurred would be ((3 - 1)^2 + 6) + ((5 - 3)^2 + 6) = 20.

Sample Input 2

2 1000000000000
500000 1000000

Sample Output 2

1250000000000

The answer may not fit into a 32-bit integer type.

Sample Input 3

8 5
1 3 4 5 10 11 12 13

Sample Output 3

62

If we follow the path 1 → 2 → 4 → 5 → 8, the total cost incurred would be ((3 - 1)^2 + 5) + ((5 - 3)^2 + 5) + ((10 - 5)^2 + 5) + ((13 - 10)^2 + 5) = 62.

Solution in Rust (1.15.1) (judge verdict: Accepted)

#[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 ( 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 , mut $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let mut $ var = read_value ! ( $ next , $ t ) ; input_inner ! { $ next $ ( $ r ) * } } ; ( $ 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 , usize1 ) => { read_value ! ( $ next , usize ) - 1 } ; ( $ next : expr , $ t : ty ) => { $ next ( ) . parse ::<$ t > ( ) . expect ( "Parse error" ) } ; }

fn main() {
    input! {
        n: usize, c: i64,
        h: [i64;n]
    }

    let mut dp = vec![0i64;n];
    dp[0] = 0;
    let mut cht = ConvexHullTrick::new(|l,r| l >= r);
    for i in 1..n {
        cht.add((-2*h[i-1], dp[i-1] + h[i-1] * h[i-1]));
        dp[i] = cht.cget(h[i]) + h[i] * h[i] + c;
    }
    println!("{}", dp[n-1]);
}

#[allow(dead_code)]
type Line = (i64, i64);
#[allow(dead_code)]
struct ConvexHullTrick {
    lines: std::collections::VecDeque<Line>,
    comp: Box<Fn(i64, i64) -> bool>,
}
#[allow(dead_code)]
impl ConvexHullTrick {
    #[doc = " min query: comp = |l,r| l >= r"]
    fn new<B>(comp: B) -> ConvexHullTrick
    where
        B: Fn(i64, i64) -> bool + 'static,
    {
        ConvexHullTrick {
            lines: std::collections::VecDeque::new(),
            comp: Box::new(comp),
        }
    }
    #[doc = " check l2 is redundant"]
    fn check(mut l1: Line, l2: Line, mut l3: Line) -> bool {
        if l1 < l3 {
            std::mem::swap(&mut l1, &mut l3);
        }
        (l3.1 - l2.1) * (l2.0 - l1.0) >= (l2.1 - l1.1) * (l3.0 - l2.0)
    }
    #[doc = " add y=ax+b, a1>=a2>=a3>=..."]
    fn add(&mut self, line: Line) {
        while self.lines.len() >= 2
            && Self::check(
                self.lines[self.lines.len() - 2],
                self.lines[self.lines.len() - 1],
                line,
            )
        {
            self.lines.pop_back();
        }
        self.lines.push_back(line);
    }
    fn f(&self, i: usize, x: i64) -> i64 {
        self.lines[i].0 * x + self.lines[i].1
    }
    #[doc = " return min value at x"]
    fn get(&self, x: i64) -> i64 {
        let mut ng = -1i64;
        let mut ok = self.lines.len() as i64 - 1;
        while (ok - ng).abs() > 1 {
            let mid = (ok + ng) / 2;
            if (self.comp)(self.f(mid as usize, x), self.f(mid as usize + 1, x)) {
                ng = mid;
            } else {
                ok = mid;
            }
        }
        self.f(ok as usize, x)
    }
    #[doc = " return min value at x, x1 <= x2 <= x3 <= ..."]
    fn cget(&mut self, x: i64) -> i64 {
        fn y(l: Line, x: i64) -> i64 {
            l.0 * x + l.1
        }
        while self.lines.len() >= 2 && (self.comp)(y(self.lines[0], x), y(self.lines[1], x)) {
            self.lines.pop_front();
        }
        y(self.lines[0], x)
    }
}