Problem p03372 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return false;
  }
  return true;
}

template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return false;
  }
  return true;
}

template <class T> bool chmax(T &a, initializer_list<T> l) {
  return chmax(a, *max_element(l.begin(), l.end()));
}

template <class T> bool chmin(T &a, initializer_list<T> l) {
  return chmin(a, *min_element(l.begin(), l.end));
}

int main(int argc, char **argv) {
  ll N, C;
  cin >> N >> C;
  vector<ll> X(N), V(N);
  REP(i, N) cin >> X[i] >> V[i];

  vector<ll> sumr(N + 1, 0), suml(N + 1, 0);
  REP(i, N + 1) { sumr[i + 1] = sumr[i] + V[i]; }
  for (ll i = N - 1; i >= 0; --i) {
    suml[i] = suml[i + 1] + V[i];
  }

  vector<ll> dpr(N + 1, 0);
  REP(i, N)
  chmax(dpr[i + 1], {dpr[i], sumr[i + 1] - X[i]});
  vector<ll> dpl(N + 1, 0);
  for (ll i = N; i > 0; --i)
    chmax(dpl[i - 1], {dpl[i], suml[i - 1] - (C - X[i - 1])});

  ll res{max(dpr[N], dpl[0])};
  REP(i, N) {
    ll tmp{0};

    // left -> right
    tmp -= (C - X[i]);
    tmp += suml[i];
    chmax(res, tmp);

    tmp -= (C - X[i]);
    tmp += dpr[i];
    chmax(res, tmp);

    tmp = 0;
    // right -> left
    tmp -= X[i];
    tmp += sumr[i + 1];
    chmax(res, tmp);

    tmp -= X[i];
    tmp += dpl[i + 1];
    chmax(res, tmp);
  }

  std::cout << res << std::endl;
}


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

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return false;
  }
  return true;
}

template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return false;
  }
  return true;
}

template <class T> bool chmax(T &a, initializer_list<T> l) {
  return chmax(a, *max_element(l.begin(), l.end()));
}

template <class T> bool chmin(T &a, initializer_list<T> l) {
  return chmin(a, *min_element(l.begin(), l.end));
}

int main(int argc, char **argv) {
  ll N, C;
  cin >> N >> C;
  vector<ll> X(N), V(N);
  REP(i, N) cin >> X[i] >> V[i];

  vector<ll> sumr(N + 1, 0), suml(N + 1, 0);
  REP(i, N) { sumr[i + 1] = sumr[i] + V[i]; }
  for (ll i = N - 1; i >= 0; --i) {
    suml[i] = suml[i + 1] + V[i];
  }

  vector<ll> dpr(N + 1, 0);
  REP(i, N)
  chmax(dpr[i + 1], {dpr[i], sumr[i + 1] - X[i]});
  vector<ll> dpl(N + 1, 0);
  for (ll i = N; i > 0; --i)
    chmax(dpl[i - 1], {dpl[i], suml[i - 1] - (C - X[i - 1])});

  ll res{max(dpr[N], dpl[0])};
  REP(i, N) {
    ll tmp{0};

    // left -> right
    tmp -= (C - X[i]);
    tmp += suml[i];
    chmax(res, tmp);

    tmp -= (C - X[i]);
    tmp += dpr[i];
    chmax(res, tmp);

    tmp = 0;
    // right -> left
    tmp -= X[i];
    tmp += sumr[i + 1];
    chmax(res, tmp);

    tmp -= X[i];
    tmp += dpl[i + 1];
    chmax(res, tmp);
  }

  std::cout << res << std::endl;
}