Problem p03185 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("-O3")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
using namespace std;

#define all(c) (c).begin(), (c).end()
#define endl "\n"
#define ff first
#define ss second
#define allr(c) (c).rbegin(), (c).rend()
#define ifr(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define pof pop_front
#define pob pop_back
#define pb emplace_back
#define pf emplace_front
#define fstm(m, n, r)                                                          \
  m.reserve(n);                                                                \
  m.max_load_factor(r)
#define mp make_pair
#define mt make_tuple
#define inf LLONG_MAX
#define os                                                                     \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
const double PI = acos(-1);
typedef complex<double> cd;
typedef long long ll;
ll gcd() { return 0ll; }
template <typename T, typename... Args> T gcd(T a, Args... args) {
  return __gcd(a, (__typeof(a))gcd(args...));
}
typedef map<ll, ll> mll;
typedef map<string, ll> msll;
typedef unordered_map<ll, ll> umap;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mod 1000000007
#define N 1000050

struct Point {
  ll x, y;
  Point(ll a = 0, ll b = 0) {
    x = a;
    y = b;
  }
};

struct LineContainer {
  vector<Point> line;
  LineContainer() { line.resize(4 * N + 5, Point(0, inf)); }
  ll dot(Point a, Point b) { return (a.x * b.x + a.y * b.y); }
  ll f(Point a, ll x) { return dot(a, Point(x, 1)); }
  void add(Point nw, ll l = 0, ll r = N, ll pos = 1) {
    ll m = (l + r) / 2;
    bool lef = f(nw, l) < f(line[pos], l);
    bool mid = f(nw, m) < f(line[pos], m);
    if (mid)
      swap(nw, line[pos]);
    if (r - l == 1)
      return;
    if (lef != mid)
      add(nw, l, m, 2 * pos);
    else
      add(nw, m, r, 2 * pos + 1);
  }
  ll query(ll x, ll l = 0, ll r = N, ll pos = 1) {
    ll m = (l + r) / 2;
    if (r - l == 1)
      return f(line[pos], x);
    if (x < m)
      return min(f(line[pos], x), query(x, l, m, 2 * pos));
    return min(f(line[pos], x), query(x, m, r, 2 * pos + 1));
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  int n;
  ll c;
  cin >> n >> c;
  ll a[n];
  ifr(i, 0, n) cin >> a[i];
  LineContainer li;
  ll dp[n];
  dp[0] = 0;
  li.add({-2 * a[0], a[0] * a[0]});
  ifr(i, 1, n) {
    dp[i] = c + a[i] * a[i] + li.query(a[i]);
    li.add({-2 * a[i], a[i] * a[i] + dp[i]});
  }
  cout << dp[n - 1] << endl;
  return 0;
}


Fixed submission:
#pragma GCC optimize("-O3")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
using namespace std;

#define all(c) (c).begin(), (c).end()
#define endl "\n"
#define ff first
#define ss second
#define allr(c) (c).rbegin(), (c).rend()
#define ifr(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define pof pop_front
#define pob pop_back
#define pb emplace_back
#define pf emplace_front
#define fstm(m, n, r)                                                          \
  m.reserve(n);                                                                \
  m.max_load_factor(r)
#define mp make_pair
#define mt make_tuple
#define inf LLONG_MAX
#define os                                                                     \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
const double PI = acos(-1);
typedef complex<double> cd;
typedef long long ll;
ll gcd() { return 0ll; }
template <typename T, typename... Args> T gcd(T a, Args... args) {
  return __gcd(a, (__typeof(a))gcd(args...));
}
typedef map<ll, ll> mll;
typedef map<string, ll> msll;
typedef unordered_map<ll, ll> umap;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mod 1000000007
#define N 1000050

struct Point {
  ll x, y;
  Point(ll a = 0, ll b = 0) {
    x = a;
    y = b;
  }
};

struct LineContainer {
  vector<Point> line;
  LineContainer() { line.resize(4 * N + 5, Point(0, inf)); }
  ll dot(Point a, Point b) { return (a.x * b.x + a.y * b.y); }
  ll f(Point a, ll x) { return dot(a, Point(x, 1)); }
  void add(Point nw, ll l = 0, ll r = N, ll pos = 1) {
    ll m = (l + r) / 2;
    bool lef = f(nw, l) < f(line[pos], l);
    bool mid = f(nw, m) < f(line[pos], m);
    if (mid)
      swap(nw, line[pos]);
    if (r - l == 1)
      return;
    if (lef != mid)
      add(nw, l, m, 2 * pos);
    else
      add(nw, m, r, 2 * pos + 1);
  }
  ll query(ll x, ll l = 0, ll r = N, ll pos = 1) {
    ll m = (l + r) / 2;
    if (r - l == 1)
      return f(line[pos], x);
    if (x < m)
      return min(f(line[pos], x), query(x, l, m, 2 * pos));
    return min(f(line[pos], x), query(x, m, r, 2 * pos + 1));
  }
};

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

  int n;
  ll c;
  cin >> n >> c;
  ll a[n];
  ifr(i, 0, n) cin >> a[i];
  LineContainer li;
  ll dp[n];
  dp[0] = 0;
  li.add({-2 * a[0], a[0] * a[0]});
  ifr(i, 1, n) {
    dp[i] = c + a[i] * a[i] + li.query(a[i]);
    li.add({-2 * a[i], a[i] * a[i] + dp[i]});
  }
  cout << dp[n - 1] << endl;
  return 0;
}