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 C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;

template<typename T,bool Mono>
struct ConvexHullTrick{
    typedef function<bool(T,T)> C;
    #define F first
    #define S second
    using Line=pair<T,T>;
    C cmp;
    deque<Line> Lines;
    ConvexHullTrick(C cmp=[](T l,T r){return l>=r;})
        :cmp(cmp){}
    bool check(Line l1,Line l2,Line l3){
        if (l1<l3) swap(l1,l3);
        return (l3.S-l2.S)*(l2.F-l1.F)>=(l2.S-l1.S)*(l3.F-l2.F);
    }
    void add(T a,T b){
        Line line(a,b);
        while(Lines.size()>=2&&check(*(Lines.end()-2)
        ,Lines.back(),line)) Lines.pop_back();
        Lines.emplace_back(line);
    }
    T f(const int &i,const T &x){
        return Lines[i].F*x+Lines[i].S;
    }
    T f(const Line &line,const T &x){
        return line.F*x+line.S;
    }
    T query(T x){
        if (Mono){
            while(Lines.size()>=2
            &&cmp(f(0,x),f(1,x))) Lines.pop_front();
            return f(0,x);
        } else {
            int lb=-1,ub=Lines.size()-1;
            while(ub-lb>1){
                int mid=(lb+ub)>>1;
                (cmp(f(mid,x),f(mid+1,x))?lb:ub)=mid;
            }
            return f(ub,x);
        }
    }
    #undef F
    #undef S
};

// https://atcoder.jp/contests/colopl2018-final-open/tasks/colopl2018_final_c

void colopl2018_final_c(){
    int N; cin >> N;
    vector<long long> a(N);
    for (int i=0;i<N;++i) cin >> a[i];
    ConvexHullTrick<long long,true> CHT;
    for (long long i=0;i<N;++i) CHT.add(-2*i,a[i]+i*i);
    for (long long i=0;i<N;++i) cout << CHT.query(i)+i*i << '\n';
}

// https://atcoder.jp/contests/dp/tasks/dp_z

void dp_z(){
    int N; long long C; cin >> N >> C;
    vector<long long> h(N);
    for (int i=0;i<N;++i) cin >> h[i];
    ConvexHullTrick<long long,true> CHT;
    vector<long long> dp(N,0);
    for (int i=0;i<N;++i){
        if (i) dp[i]=CHT.query(h[i])+h[i]*h[i]+C;
        CHT.add(-2*h[i],h[i]*h[i]+dp[i]);
    }
    cout << dp[N-1] << '\n';
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    dp_z();
}