Problem p03372 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LOOP(n) for (int _i = 0; _i < (n); _i++)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, r, n) for (int i = (r); i < (n); ++i)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
const int INF = 2100100100;
const int MOD = 1e9 + 7;

int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  ll N, C;
  cin >> N >> C;
  vector<ll> x(N + 2, 0), v(N + 1, 0);
  REP(i, N) cin >> x.at(i + 1) >> v.at(i + 1);
  x[N + 1] = C;

  vector<ll> clk(N + 1), cclk(N + 1);
  ll now = 0;
  FOR(i, 1, N + 1) {
    now += v[i] - (x[i] - x[i - 1]);
    clk[i + 1] = max(now, clk[i]);
  }
  now = 0;
  for (int i = N; i >= 1; i--) {
    now += v[i] - (x[i + 1] - x[i]);
    cclk[i - 1] = max(now, cclk[i]);
  }

  ll ans = 0;
  chmax(ans, cclk[0]);
  now = 0;
  for (int i = 1; i < N + 1; i++) {
    now += v[i] - (x[i] - x[i - 1]) * 2;
    chmax(ans, now + cclk[i]);
  }

  chmax(ans, clk[N]);
  now = 0;
  for (int i = N; i >= 1; i--) {
    now += v[i] - (x[i + 1] - x[i]) * 2;
    chmax(ans, now + clk[i - 1]);
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define LOOP(n) for (int _i = 0; _i < (n); _i++)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, r, n) for (int i = (r); i < (n); ++i)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
const int INF = 2100100100;
const int MOD = 1e9 + 7;

int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  ll N, C;
  cin >> N >> C;
  vector<ll> x(N + 2, 0), v(N + 1, 0);
  REP(i, N) cin >> x.at(i + 1) >> v.at(i + 1);
  x[N + 1] = C;

  vector<ll> clk(N + 1), cclk(N + 1);
  ll now = 0;
  FOR(i, 1, N + 1) {
    now += v[i] - (x[i] - x[i - 1]);
    clk[i] = max(now, clk[i - 1]);
  }
  now = 0;
  for (int i = N; i >= 1; i--) {
    now += v[i] - (x[i + 1] - x[i]);
    cclk[i - 1] = max(now, cclk[i]);
  }

  ll ans = 0;
  chmax(ans, cclk[0]);
  now = 0;
  for (int i = 1; i < N + 1; i++) {
    now += v[i] - (x[i] - x[i - 1]) * 2;
    chmax(ans, now + cclk[i]);
  }

  chmax(ans, clk[N]);
  now = 0;
  for (int i = N; i >= 1; i--) {
    now += v[i] - (x[i + 1] - x[i]) * 2;
    chmax(ans, now + clk[i - 1]);
  }
  cout << ans << endl;

  return 0;
}