Problem p03185 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define mp make_pair
#define inlld(x) scanf("%lld", &x)
#define ind(x) scanf("%d", &x)
#define inlld2(x, y) scanf("%lld%lld", &x, &y)
#define inlld3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define ind2(x, y) scanf("%d%d", &x, &y)
#define ind3(x, y, z) scanf("%d%d%d", &x, &y, &z)

const int N = 1e5 + 5;
const int MOD = 1e9 + 7;

typedef long long ll;
typedef long double ld;

using namespace std;

struct line {
  ll sl, cn;
  line(ll s, ll c) {
    sl = s;
    cn = c;
  }
};

// VERY IMPORTANT, CHANGE IF NEEDED:
// GETTING LINES IN DECREASING ORDER OF SLOPE
// CHANGE CHECK CONDITION FOR THE REVERSE
struct CHT {
  vector<line> lines;
  int pointer;

  void set_pointer() { pointer = 0; }

  bool check() {
    int x = lines.size();
    line lt = lines[x - 3], rt = lines[x - 1], mid = lines[x - 2];
    // CHANGE FOLLOWING CONDITION IF NEEDED
    return (rt.cn - lt.cn) * (lt.sl - mid.sl) <
           (mid.cn - lt.cn) * (lt.sl - rt.sl);
  }

  void add(line x) {
    lines.pb(x);
    while (lines.size() >= 3 && check())
      lines.erase(lines.end() - 2);
  }

  ll query(ll x) {
    if (pointer >= lines.size())
      pointer = lines.size() - 1;
    // CHANGE FOLLOWING CONDITION IF NEEDED
    while (pointer < lines.size() - 1 &&
           lines[pointer + 1].sl * x + lines[pointer + 1].cn <
               lines[pointer].sl * x + lines[pointer].cn)
      pointer++;
    return lines[pointer].sl * x + lines[pointer].cn;
  }
};

CHT cht;
ll dp[N], h[N], C, n;

int main() {
  inlld2(n, C);
  for (ll a = 1; a <= n; a++) {
    dp[a] = 1e18;
    inlld(h[a]);
  }
  dp[1] = 0;
  cht.add(line(-2 * h[1], dp[1] + h[1] * h[1]));
  for (ll a = 2; a <= n; a++) {
    dp[a] = min(dp[a], cht.query(h[a]) + h[a] * h[a] + C);
    cht.add(line(-2 * h[a], h[a] * h[a] + dp[a]));
  }
  printf("%lld\n", dp[n]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
#define mp make_pair
#define inlld(x) scanf("%lld", &x)
#define ind(x) scanf("%d", &x)
#define inlld2(x, y) scanf("%lld%lld", &x, &y)
#define inlld3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define ind2(x, y) scanf("%d%d", &x, &y)
#define ind3(x, y, z) scanf("%d%d%d", &x, &y, &z)

const int N = 2e5 + 5;
const int MOD = 1e9 + 7;

typedef long long ll;
typedef long double ld;

using namespace std;

struct line {
  ll sl, cn;
  line(ll s, ll c) {
    sl = s;
    cn = c;
  }
};

// VERY IMPORTANT, CHANGE IF NEEDED:
// GETTING LINES IN DECREASING ORDER OF SLOPE
// CHANGE CHECK CONDITION FOR THE REVERSE
struct CHT {
  vector<line> lines;
  int pointer;

  void set_pointer() { pointer = 0; }

  bool check() {
    int x = lines.size();
    line lt = lines[x - 3], rt = lines[x - 1], mid = lines[x - 2];
    // CHANGE FOLLOWING CONDITION IF NEEDED
    return (rt.cn - lt.cn) * (lt.sl - mid.sl) <
           (mid.cn - lt.cn) * (lt.sl - rt.sl);
  }

  void add(line x) {
    lines.pb(x);
    while (lines.size() >= 3 && check())
      lines.erase(lines.end() - 2);
  }

  ll query(ll x) {
    if (pointer >= lines.size())
      pointer = lines.size() - 1;
    // CHANGE FOLLOWING CONDITION IF NEEDED
    while (pointer < lines.size() - 1 &&
           lines[pointer + 1].sl * x + lines[pointer + 1].cn <
               lines[pointer].sl * x + lines[pointer].cn)
      pointer++;
    return lines[pointer].sl * x + lines[pointer].cn;
  }
};

CHT cht;
ll dp[N], h[N], C, n;

int main() {
  inlld2(n, C);
  for (ll a = 1; a <= n; a++) {
    dp[a] = 1e18;
    inlld(h[a]);
  }
  dp[1] = 0;
  cht.add(line(-2 * h[1], dp[1] + h[1] * h[1]));
  for (ll a = 2; a <= n; a++) {
    dp[a] = min(dp[a], cht.query(h[a]) + h[a] * h[a] + C);
    cht.add(line(-2 * h[a], h[a] * h[a] + dp[a]));
  }
  printf("%lld\n", dp[n]);
  return 0;
}