#include <cstdio>
#include <cstdint>
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include <map>
#include <type_traits>
#include <limits>
#include <tuple>

template <class Tp>
class lines {
  // based on Li Chao tree
  std::map<Tp, size_t> enc;
  std::vector<Tp> dec;
  std::vector<std::pair<Tp, Tp>> c;
  size_t m = 1;
  Tp inf = std::numeric_limits<Tp>::max() / 8;

private:
  void init_resize(size_t n) {
    while (m < n+n) m <<= 1;
    c.assign(m, {0, inf});
  }

  void inspect() const {
    for (size_t i = 1; i < m; ++i) {
      std::string s = (c[i].first < inf? std::to_string(c[i].first) : "inf");
      std::string t = (c[i].second < inf? std::to_string(c[i].second) : "inf");
      fprintf(stderr, "(%s, %s)%c", s.c_str(), t.c_str(), (i&(i+1))? ' ':'\n');
    }
  }

public:
  template <class ForwardIt>
  lines(ForwardIt first, ForwardIt last): dec(first, last) {
    static_assert(std::is_same<typename ForwardIt::value_type, Tp>::value, "Type mismatch");
    std::sort(dec.begin(), dec.end());
    size_t n = dec.size();
    init_resize(n);
    while (dec.size() < m/2) dec.push_back(dec.back()+1);
    // dec.resize(m/2, dec.back());
    for (size_t i = 0; i < m/2; ++i) enc.emplace(dec[i], i);
  }

  void append(Tp a, Tp b) {
    // fprintf(stderr, "append: %jdx%+jd\n", a, b);
    size_t l = 0;
    size_t j = 1;
    size_t h = 1;
    size_t w = m/2;
    while (w > 1 && l+w-1 < dec.size()) {
      // fprintf(stderr, "l: %zu, h: %zu, w: %zu\n", l, h, w);
      Tp xl = dec.at(l);
      Tp xm = dec.at(l+w/2);
      Tp xr = dec.at(l+w-1);

      // fprintf(stderr, "xs: (%jd, %jd, %jd)\n", xl, xm, xr);

      Tp a0, b0;
      std::tie(a0, b0) = c[j];

      Tp yl0 = a0*xl + b0;
      Tp ym0 = a0*xm + b0;
      Tp yr0 = a0*xr + b0;

      Tp yl = a*xl + b;
      Tp ym = a*xm + b;
      Tp yr = a*xr + b;

      // fprintf(stderr, "(yl0, ym0, yr0): (%jd, %jd, %jd)\n", yl0, ym0, yr0);
      // fprintf(stderr, "(yl, ym, yr): (%jd, %jd, %jd)\n", yl, ym, yr);

      if (yl0 <= yl && yr0 <= yr) {
        // fprintf(stderr, "broken 0\n");
        break;
      }
      if (yl0 >= yl && yr0 >= yr) {
        c[j] = {a, b};
        // fprintf(stderr, "[%zu] <- (%jd, %jd)\n", h+l, a, b);
        // fprintf(stderr, "broken 1\n");
        break;
      }

      if (ym0 >= ym) {
        if (yr0 >= yr) {
          c[j] = {a, b};
          a = a0;
          b = b0;
          j <<= 1;
        } else {
          c[j] = {a, b};
          a = a0;
          b = b0;
          j = j << 1 | 1;
          l += w/2;
        }
      } else {
        if (yr0 >= yr) {
          j = j << 1 | 1;
          l += w/2;
        } else {
          j <<= 1;
        }
      }

      h <<= 1;
      w >>= 1;
    }
    // inspect();
    // fprintf(stderr, "\n");
  }

  Tp min_at(Tp x) const {
    size_t i = enc.at(x) + m/2;
    Tp y = inf;
    while (i > 0) {
      Tp a, b;
      std::tie(a, b) = c[i];
      y = std::min(y, a*x+b);
      i >>= 1;
    }
    // fprintf(stderr, "min_at(%jd): %jd\n", x, y);
    return y;
  }
};

#if 0

#include <set>

int main() {
  size_t Q;
  scanf("%zu", &Q);

  std::vector<std::pair<intmax_t, intmax_t>> q;
  std::set<intmax_t> x;
  for (size_t i = 0; i < Q; ++i) {
    intmax_t a, b;
    scanf("%jd %jd", &a, &b);
    if (a == 0) {
      x.insert(b);
    }
    q.emplace_back(a, b);
  }

  lines<intmax_t> cht(x.begin(), x.end());
  for (size_t i = 0; i < Q; ++i) {
    intmax_t a, b;
    std::tie(a, b) = q[i];
    if (a == 0) {
      printf("min_at(%jd): %jd\n", b, cht.min_at(b));
    } else {
      fprintf(stderr, "append(%jd, %jd)\n", a, b);
      cht.append(a, b);
    }
  }
}

#else

int main() {
  size_t N;
  intmax_t C;
  scanf("%zu %jd", &N, &C);

  std::vector<intmax_t> h(N);
  for (auto& hi: h) scanf("%jd", &hi);

  lines<intmax_t> cht(h.begin(), h.end());

  std::vector<intmax_t> dp(N);
  for (size_t i = 0; i+1 < N; ++i) {
    cht.append(-2*h[i], h[i]*h[i]+dp[i]);
    dp[i+1] = cht.min_at(h[i+1]) + h[i+1]*h[i+1] + C;
    // fprintf(stderr, "dp[%zu]: %jd\n\n", i+1, dp[i+1]);
  }

  printf("%jd\n", dp[N-1]);
}

#endif


Measured memory_bytes: 32000