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)

//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

//

// おためしLi Chao Tree
// 最小値習得

struct LiChaoTree {
    seg: Vec<(i64, i64)>,
    bit: usize,
    z: Vec<i64>,
}

const INF_LINE: (i64, i64) = (0, 2_000_000_000_000_000_001);

impl LiChaoTree {
    fn new(z: &Vec<i64>) -> Self {
        let mut z = z.clone();
        z.sort();
        z.dedup();
        let mut k = 0;
        while (1 << k) < z.len() {
            k += 1;
        }
        let p = *z.last().unwrap();
        while z.len() < (1 << k) {
            z.push(p);
        }
        LiChaoTree {
            seg: vec![INF_LINE; 2 << k],
            bit: k,
            z: z,
        }
    }
    //直線ax + b を追加
    fn update(&mut self, a: i64, b: i64) {
        let mut state = ((a, b), 1, 0, (1 << self.bit));
        loop {
            let ((a, b), k, l, r) = state;
            let (c, d) = self.seg[k];
            let fl = a * self.z[l] + b;
            let fr = a * self.z[r - 1] + b;
            let gl = c * self.z[l] + d;
            let gr = c * self.z[r - 1] + d;
            if fl >= gl && fr >= gr {
                break;
            }
            if fl <= gl && fr <= gr {
                self.seg[k] = (a, b);
                break;
            }
            let m = (l + r) / 2;
            let fm = a * self.z[m] + b;
            let gm = c * self.z[m] + d;
            state = if fr >= gr && fm >= gm {
                ((a, b), 2 * k, l, m)
            } else if fr >= gr {
                self.seg[k] = (a, b);
                ((c, d), 2 * k + 1, m, r)
            } else if fm >= gm {
                ((a, b), 2 * k + 1, m, r)
            } else {
                self.seg[k] = (a, b);
                ((c, d), 2 * k, l, m)
            };
        }
    }
    fn find(&self, x: i64) -> i64 {
        let k = self.z.binary_search(&x).unwrap();
        let mut y = k + (1 << self.bit);
        let mut res = std::i64::MAX;
        while y >= 1 {
            let (a, b) = self.seg[y];
            res = std::cmp::min(res, a * x + b);
            y >>= 1;
        }
        res
    }
}

fn run() {
    input! {
        n: usize,
        c: i64,
        h: [i64; n],
    }
    let mut cht = LiChaoTree::new(&h);
    cht.update(-2 * h[0], h[0] * h[0]);
    let mut dp = vec![0; n];
    for i in 1..n {
        dp[i] = c + h[i] * h[i] + cht.find(h[i]);
        cht.update(-2 * h[i], dp[i] + h[i] * h[i]);
    }
    println!("{}", dp[n - 1]);
}

fn main() {
    run();
}