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)

#include <bits/stdc++.h>
#define endl '\n'
#define int long long

using namespace std;
const int MAXN = (1 << 20);
const int64_t inf = (int64_t)1e17 + 42;
const int N = 2e5 + 10;

int n, c, h[N], dp[N];

void read()
{
   cin>>n>>c;

   for (int i = 0; i < n; ++i)
   {
      cin>>h[i];
   }
}

struct convex_hull_trick_min
{
   struct line
   {
      int64_t m, b;
      bool is_query;
      double line_x;

      line() {m = 0; b = 0; line_x = 0; is_query = 0; }
      line(int64_t _m, int64_t _b) {m = _m; b = _b; line_x = 0; is_query = 0; }

      bool operator<(const line &l2) const
      {
         if(!is_query && !l2.is_query) return m > l2.m;
         else return line_x < l2.line_x;
      }

      int64_t value_at(int64_t x) const { return m * x + b; }
   };

   bool parallel(const line &l1, const line &l2) { return l1.m == l2.m; }

   double intersect_x(const line &l1, const line &l2)
   {
      if(parallel(l1, l2)) return (double)inf;
      return (double)(l1.b - l2.b) / (double)(l2.m - l1.m);
   }

   set<line> hull;
   void clear() { hull.clear(); }
   void init() { clear(); }

   bool has_next(set<line>::iterator it) { return it != hull.end() && next(it) != hull.end(); }
   bool has_prev(set<line>::iterator it) { return it != hull.end() && it != hull.begin(); }

   bool irrelevant(const line &l1, const line &l2, const line &l3) { return intersect_x(l1, l3) <= intersect_x(l1, l2); }
   bool irrelevant(set<line>::iterator it) { return has_prev(it) && has_next(it) && irrelevant(*prev(it), *it, *next(it)); }

   set<line>::iterator update_border(set<line>::iterator it)
   {
      line tmp(*it);

      double val;
      if(has_next(it)) val = intersect_x(*it, *next(it));
      else val = inf;

      it = hull.erase(it);
      tmp.line_x = val;
      it = hull.insert(it, tmp);

      return it;
   }

   void add_line(int64_t m, int64_t b)
   {
      line curr_line = line(m, b);
      set<line>::iterator it = hull.lower_bound(curr_line);

      if(it != hull.end() && it->m == m)
      {
         if(it->b > b) it = hull.erase(it);
         else return;
      }

      it = hull.insert(it, curr_line);
      if(irrelevant(it))
      {
         hull.erase(it);
         return;
      }

      while(has_prev(it) && irrelevant(prev(it))) hull.erase(prev(it));
      while(has_next(it) && irrelevant(next(it))) hull.erase(next(it));

      it = update_border(it);
      if(has_prev(it)) update_border(prev(it));
      if(has_next(it)) update_border(next(it));
   }

   int64_t query(int64_t x)
   {
      line to_find;
      to_find.line_x = x;
      to_find.is_query = 1;

      set<line>::iterator best_line = hull.lower_bound(to_find);
      if(best_line == hull.end())
         return inf;

      return best_line->value_at(x);
   }
}s2;

void solve()
{
   dp[0] = 0;
   s2.add_line(-2*h[0], h[0]*h[0]);

   int y = 0;
   for (int i = 1; i < n; ++i)
   {
      int tt = s2.query(h[i]);
      y = h[i]*h[i] + c + s2.query(h[i]);
      s2.add_line(-2*h[i], h[i]*h[i] + y);
   }
   cout<<y;
}

int32_t main()
{
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);

   read();
   solve();
   return 0;
}