Problem p03185 (C++)

Buggy submission (status: Runtime Error):
// in the name of Allah

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

#define endl '\n'
#define pb push_back
#define ll long long
#define double long double
#define sz(x) ((int)(x.size()))

#define fr first
#define se second

#define inf 1e18
#define max_n 123456
#define mod 1000000007

ll add(ll a, ll b) {
  a += b;
  if (a >= mod)
    return a - mod;
  return a;
}

ll mul(ll a, ll b) {
  a *= b;
  if (a >= mod)
    return a % mod;
  return a;
}

ll power(ll a, ll b) {
  ll p = 1;
  while (b) {
    if (b & 1)
      p = mul(p, a);
    a = mul(a, a);
    b /= 2;
  }
  return p;
}

// -----------> copied code <------------

const ll is_query = -(1LL << 62);

struct Line {
  ll m, b;
  mutable function<const Line *()> succ;
  bool operator<(const Line &rhs) const {
    if (rhs.b != is_query)
      return m < rhs.m;
    const Line *s = succ();
    if (!s)
      return 0;
    ll x = rhs.m;
    return b - s->b < (s->m - m) * x;
  }
};

struct HullDynamic
    : public multiset<Line> { // will maintain upper hull for maximum
  bool bad(iterator y) {
    auto z = next(y);
    if (y == begin()) {
      if (z == end())
        return 0;
      return y->m == z->m && y->b <= z->b;
    }
    auto x = prev(y);
    if (z == end())
      return y->m == x->m && y->b <= x->b;
    return (double)(x->b - y->b) * (z->m - y->m) >=
           (double)(y->b - z->b) * (y->m - x->m);
  }
  void insert_line(ll m, ll b) {
    auto y = insert({m, b});
    y->succ = [=] { return next(y) == end() ? 0 : &*next(y); };
    if (bad(y)) {
      erase(y);
      return;
    }
    while (next(y) != end() && bad(next(y)))
      erase(next(y));
    while (y != begin() && bad(prev(y)))
      erase(prev(y));
  }
  ll eval(ll x) {
    auto l = *lower_bound((Line){x, is_query});
    return l.m * x + l.b;
  }
};

// -----------> end of copied code <------------

ll n, c;
ll h[123456];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> c;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  ll res = (h[1] - h[0]) * (h[1] - h[0]) + c;
  if (n == 2) {
    cout << res << endl;
    return 0;
  }
  HullDynamic cht;
  cht.insert_line(2 * h[0], -(c + h[0] * h[0]));
  cht.insert_line(2 * h[1], -(res + c + h[1] * h[1]));
  for (int i = 2; i < n; i++) {
    res = h[i] * h[i] - cht.eval(h[i]);
    cht.insert_line(2 * h[i], -(res + c + h[i] * h[i]));
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
// in the name of Allah

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

#define endl '\n'
#define pb push_back
#define ll long long
#define double long double
#define sz(x) ((int)(x.size()))

#define fr first
#define se second

#define inf 1e18
#define max_n 123456
#define mod 1000000007

ll add(ll a, ll b) {
  a += b;
  if (a >= mod)
    return a - mod;
  return a;
}

ll mul(ll a, ll b) {
  a *= b;
  if (a >= mod)
    return a % mod;
  return a;
}

ll power(ll a, ll b) {
  ll p = 1;
  while (b) {
    if (b & 1)
      p = mul(p, a);
    a = mul(a, a);
    b /= 2;
  }
  return p;
}

// -----------> copied code <------------

const ll is_query = -(1LL << 62);

struct Line {
  ll m, b;
  mutable function<const Line *()> succ;
  bool operator<(const Line &rhs) const {
    if (rhs.b != is_query)
      return m < rhs.m;
    const Line *s = succ();
    if (!s)
      return 0;
    ll x = rhs.m;
    return b - s->b < (s->m - m) * x;
  }
};

struct HullDynamic
    : public multiset<Line> { // will maintain upper hull for maximum
  bool bad(iterator y) {
    auto z = next(y);
    if (y == begin()) {
      if (z == end())
        return 0;
      return y->m == z->m && y->b <= z->b;
    }
    auto x = prev(y);
    if (z == end())
      return y->m == x->m && y->b <= x->b;
    return (double)(x->b - y->b) * (z->m - y->m) >=
           (double)(y->b - z->b) * (y->m - x->m);
  }
  void insert_line(ll m, ll b) {
    auto y = insert({m, b});
    y->succ = [=] { return next(y) == end() ? 0 : &*next(y); };
    if (bad(y)) {
      erase(y);
      return;
    }
    while (next(y) != end() && bad(next(y)))
      erase(next(y));
    while (y != begin() && bad(prev(y)))
      erase(prev(y));
  }
  ll eval(ll x) {
    auto l = *lower_bound((Line){x, is_query});
    return l.m * x + l.b;
  }
};

// -----------> end of copied code <------------

ll n, c;
ll h[223456];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> c;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  ll res = (h[1] - h[0]) * (h[1] - h[0]) + c;
  if (n == 2) {
    cout << res << endl;
    return 0;
  }
  HullDynamic cht;
  cht.insert_line(2 * h[0], -(c + h[0] * h[0]));
  cht.insert_line(2 * h[1], -(res + c + h[1] * h[1]));
  for (int i = 2; i < n; i++) {
    res = h[i] * h[i] - cht.eval(h[i]);
    cht.insert_line(2 * h[i], -(res + c + h[i] * h[i]));
  }
  cout << res << endl;
  return 0;
}