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

#ifndef LOCAL
#pragma GCC optimize ("O3")
#endif
#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define mor > muu & operator << (
#define R22(r) sim > typename \
  enable_if<1 r sizeof dud<c>(0), muu&>::type operator<<(c g) {
sim > struct rge { c b, e; };
sim > rge<c> range(c h, c n) { return {h, n}; }
sim > auto dud(c* r) -> decltype(cerr << *r);
sim > char dud(...);
struct muu {
#ifdef LOCAL
stringstream a;
~muu() { cerr << a.str() << endl; }
R22(<) a << boolalpha << g; ris; }
R22(==) ris << range(begin(g), end(g)); }
sim, class b mor pair < b, c > r) { ris << "(" << r.first << ", " << r.second << ")"; }
sim mor rge<c> u) {
  a << "[";
  for (c i = u.b; i != u.e; ++i)
    *this << ", " + 2 * (i == u.b) << *i;
  ris << "]";
}
#else
sim mor const c&) { ris; }
#endif
muu & operator()() { ris; }
};
#define imie(r...) "[" #r ": " << (r) << "] "
#define imask(r...) "[" #r ": " << bitset<8 * sizeof(r)>(r) << "] "
#define arr(a, i) "[" #a imie(i) ": " << a[i] << "] "
#define arr2(a, i, j) "[" #a imie(i) imie(j) ": " << a[i][j] << "] "
#define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": ")

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ull> int128;

int128 operator +(int128 a, int128 b) {
	return int128{a.first + b.first + (a.second + b.second < a.second),
			a.second + b.second};
}
int128 operator -(int128 a, int128 b) {
	return int128{a.first - b.first - (a.second < b.second),
			a.second - b.second};
}

int128 mul(ll one, ll two) { // tested, works for any arguments
	const int K = 32;
	ll a = one >> K; // [-2^31, 2^31)
	ull b = one - (a << K); // [0, 2^32)
	ll c = two >> K;
	ull d = two - (c << K);
	int128 ans{a * c, b * d};
	for(int rep = 0; rep < 2; ++rep) {
		ll x = rep ? a * d : b * c;
		ans.first += x >> K;
		x -= (x >> K) << K;
		assert(0 <= x && x < (1LL << K));
		ans = ans + int128{0, (ull) x << K}; // works even without casting
	}
	return ans;
}

bool nonNegative(int128 x) {
    return x.first >= 0;
}

struct linie {
    vector <pair <long long, long long>> T;
    int poz = 0;
    
    void insert(long long a, long long b) {
        int sz = T.size();
        while (sz - poz > 1) {
            if (!nonNegative(mul(b - T[sz - 2].second, T[sz - 2].first - T[sz - 1].first) - 
                 mul(T[sz - 1].second - T[sz - 2].second, T[sz - 2].first - a))) {
                T.pop_back();
                sz--;
            }
            else break;
        }
        T.push_back({a, b});
    }
    
    long long query(int h) {
        long long war = 1e18;
        int sz = T.size();
        if (poz < sz) war = T[poz].first * h + T[poz].second;
        while (poz + 1 < sz) {
            long long nwar = T[poz + 1].first * h + T[poz + 1].second;
            if (nwar <= war) {
                poz++;
                war = nwar;
            }
            else break; 
        }
        return war;
    }
};

const int N = 2e5 + 7;
long long C;
int h[N];
int n;
long long dp[N];

int main() {
    scanf("%d %lld", &n, &C);
    for (int i = 1; i <= n; ++i) scanf("%d", h + i);
    
    linie X;
    
    for (int i = 1; i <= n; ++i) {
        if (i == 1) dp[i] = 0;
        else {
            dp[i] = X.query(h[i]) + C + 1ll * h[i] * h[i];
        }
        
        X.insert(-2 * h[i], dp[i] + 1ll * h[i] * h[i]);
    }
    printf("%lld\n", dp[n]);
    return 0;
}