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: Time Limit Exceeded)

#include <bits/stdc++.h>

using namespace std;
using ll = long long int;
constexpr ll M = 1e9 + 7;
inline ll sum_1(const ll n) { return ((n * (n + 1)) / 2) % M; }
inline ll sum_2(const ll n) { return ((n * (n + 1) * (2 * n + 1)) / 6) % M; }
inline ll sum_3(const ll n) { return (((n * (n + 1)) / 2) % M * ((n * (n + 1)) / 2) % M) % M; }
inline ll sum_1(const ll i, const ll j) { return (sum_1(j) - sum_1(i - 1) + M) % M; }
inline ll sum_2(const ll i, const ll j) { return (sum_2(j) - sum_2(i - 1) + M) % M; }
inline ll sum_3(const ll i, const ll j) { return (sum_3(j) - sum_3(i - 1) + M) % M; }

/* clang-format off */
namespace myDebug {
template <class T> auto is_printable(T *v) -> decltype(cout << *v, 0);
template <class T> char is_printable(...);
template <class T> struct range{T begin, end;};

class Debug {
    class DebugImpl {
      public:
        ~DebugImpl() { cout << endl; }
        #define enable_cond(cmp) enable_if<sizeof(is_printable<T>(nullptr)) cmp 1, DebugImpl &>::type
        template <class T> typename enable_cond(!=) operator<<(const T v) {
            cout << v; return *this;
        }
        template <class T> typename enable_cond(==) operator<<(const T v) {
            return *this << range<decltype(begin(v))>({begin(v), end(v)});
        }
        template <class T, class U> DebugImpl& operator<<(const pair<T, U> p) {
            return *this << "(" << p.first << ", " << p.second << ")";
        }
        template <class T> DebugImpl& operator<<(const range<T> v) {
            for (auto it = v.begin; it != v.end; ++it) {
                *this << *it << " ";
            }
            return *this << "\n";
        }
        #ifdef enable_cond
        #undef enable_cond
        #endif
    } os;
  public:
    #ifdef D_LOCAL
    template<class T> Debug& operator<<(const T v) {
        os << v; cout << endl; return *this;
    }
    #else
    template <class T> Debug& operator<<(const T &) { return *this; }
    #endif
};
#define px(...) #__VA_ARGS__":" << (__VA_ARGS__)
}; // namespace myDebug
/* clang-format on */
myDebug::Debug debug;

ll pow_f(ll b, ll e, ll M = ::M) {
    ll ans = 1;
    b = b % M;
    while (e) {
        if (e & 1) {
            ans = (ans * b) % M;
        }
        b = (b * b) % M;
        e >>= 1;
    }
    return ans;
}

ll nCr(ll n, ll r) {
    if (r > n || n < 0 || r < 0) { return 0; }
    if (n == r || r == 0) { return 1; }
    r = min(r, n - r);
    ll ans = 1;
    for (ll i = 1; i <= r; ++i) {
        ans = (ans * (n - i + 1)) / (i);
    }
    return ans;
}

ll nCr(tuple<ll, ll> t) {
    auto n = get<0>(t);
    auto k = get<1>(t);
    return nCr(n + k - 1, k);
}

ll modinv(ll x, ll M = ::M) {
    return pow_f(x, M - 2, M);
}

struct line {
    ll m, c;

    ll operator()(ll x) {
        return m * x + c;
    }

    ll intersectX(line &ot) {
        return (ot.c - c) / (m - ot.m);
    }

    friend ostream& operator<<(ostream &os, const line &l) {
        os << "[" << l.m << "x + " << l.c << "]";
        return os;
    }
};
// int128
bool isGreaterEq(pair<line, line> p, ll x) {
    ll pa = p.first.c - p.second.c;
    ll pb = p.second.m - p.first.m;
    if (pb < 0) {
        pa = -pa; pb = -pb;
    }
    return pa <= x * pb;
}
bool isGreaterEq(pair<line, line> p, pair<line, line> q) {
    __int128 pa = ((__int128)p.first.c - (__int128)p.second.c); 
    __int128 pb = ((__int128)p.second.m - (__int128)p.first.m);
    __int128 qa = ((__int128)q.first.c - (__int128)q.second.c); 
    __int128 qb = ((__int128)q.second.m - (__int128)q.first.m);

    if (pb < 0) {
        pa = -pa; pb = -pb;
    }
    if (qb < 0) {
        qa = -qa; qb = -qb;
    }

    return pa * qb <= qa * pb;
}

void solve() {
    ll n, c;
    cin >> n >> c;
    vector<ll> h(n);

    for (auto &i : h) {
        cin >> i;
    }

    /**
     * 
     * dp[i] = min j (dp[j] + (h[i] - h[j])**2 + C)
     *       = dp[j] + hi**2 + hj**2 - 2hihj + C
     *       = hi**2 + C + (dp[j] + h[j]**2) - 2hj*hi
     */

    vector<ll> dp(n);
    dp[0] = 0;

    vector<line> cvxHull; cvxHull.reserve(n);
    cvxHull.push_back({-2 * h[0], dp[0] + h[0] * h[0]});

    for (ll i = 1; i < n; ++i) {
        //Calc Dp[i]
        //cvxHull already present evaluate @ hi
        ll x = h[i];
        ll lo = 0;
        ll hi = cvxHull.size() - 1;
        ll mid = lo + (hi - lo) / 2;
        ll ans = cvxHull[mid](x);
        bool lb = true, up = true;

        while (lo <= hi) {
            mid = lo + (hi - lo) / 2;
            lb = (mid - 1 >= 0) ? isGreaterEq({cvxHull[mid], cvxHull[mid - 1]}, x) : true;  //Greater than lower intersection
            up = (mid + 1 < cvxHull.size()) ? !isGreaterEq({cvxHull[mid], cvxHull[mid + 1]}, x) : true;  //Less than upper intersection

            if (lb && up) {
                ans = cvxHull[mid](x);
                break;
            } else if (!lb && up) {
                hi = mid - 1;
            } else if (lb && !up) {
                lo = mid + 1;
            } else {
                assert(false);
            }
        }

        dp[i] = x * x + c + ans;

        line l{-2 * h[i], dp[i] + h[i] * h[i]};

        //Delete lines after intersection (greater and equal)
        for (int j = cvxHull.size() - 1; j > 0; --j) {
            auto lLast = cvxHull[j];
            auto lPrev = cvxHull[j - 1];
            auto ltemp = l;
            if (isGreaterEq({lPrev, ltemp}, {lLast, lPrev})) {
                cvxHull.pop_back();
            }
        }

        //Insert line
        cvxHull.push_back(l);
    }

    cout << dp[n-1] << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef D_LOCAL
    freopen("in.in", "r", stdin);
    freopen("out.out", "w", stdout);
#endif

    solve();

    return 0;
}