Problem p03372 (C++)

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

using namespace std;

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

typedef int64_t ll;
typedef vector<int> VI;
typedef pair<ll, ll> pii;
const int MD = 1e9 + 7;
typedef vector<ll> VL;

void dbg() { cerr << "\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}

int main() {

  // cin.tie(0); ios_base::sync_with_stdio(false);
  int n;
  ll c;
  cin >> n >> c;
  VL x(n), v(n);
  rep(i, n) cin >> x[i] >> v[i];

  VL sv(n + 1), rs(n + 1);
  rep(i, n) sv[i + 1] = sv[i] + v[i];

  auto rx = x;
  auto rv = v;
  reverse(all(rx));
  reverse(all(rv));
  rep(i, n) rx[i] = c - rx[i];
  rep(i, n) rs[i + 1] = rs[i] + rv[i];
  rep2(i, 1, n + 1) sv[i] -= x[i - 1];
  rep2(i, 1, n + 1) rs[i] -= rx[i - 1];
  rep2(i, 1, sz(sv)) sv[i + 1] = max(sv[i + 1], sv[i]);
  rep2(i, 1, sz(rs)) rs[i + 1] = max(rs[i + 1], rs[i]);
  // for(auto x :sv) printf("%lld, ",x); puts("");
  // for(auto x :rs) printf("%lld, ",x); puts("");

  ll ans = max(sv.back(), rs.back());

  rep(i, n) {
    ll w = sv[i + 1] + rs[n - i - 1] - x[i];
    // dbg(i,w,sv[i+1],rs[n-i],x[i]);
    ans = max(ans, w);
  }
  rep(i, n) {
    ll w = rs[i + 1] + sv[n - i - 1] - rx[i];
    // dbg(i,w,rs[i+1],sv[n-i],rx[i]);
    ans = max(ans, w);
  }
  if (ans < 0)
    ans = 0;
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

typedef int64_t ll;
typedef vector<int> VI;
typedef pair<ll, ll> pii;
const int MD = 1e9 + 7;
typedef vector<ll> VL;

void dbg() { cerr << "\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}

int main() {

  // cin.tie(0); ios_base::sync_with_stdio(false);
  int n;
  ll c;
  cin >> n >> c;
  VL x(n), v(n);
  rep(i, n) cin >> x[i] >> v[i];

  VL sv(n + 1), rs(n + 1);
  rep(i, n) sv[i + 1] = sv[i] + v[i];

  auto rx = x;
  auto rv = v;
  reverse(all(rx));
  reverse(all(rv));
  rep(i, n) rx[i] = c - rx[i];
  rep(i, n) rs[i + 1] = rs[i] + rv[i];
  rep2(i, 1, sz(sv)) sv[i] -= x[i - 1];
  rep2(i, 1, sz(rs)) rs[i] -= rx[i - 1];
  rep(i, n) sv[i + 1] = max(sv[i + 1], sv[i]);
  rep(i, n) rs[i + 1] = max(rs[i + 1], rs[i]);
  // for(auto x :sv) printf("%lld, ",x); puts("");
  // for(auto x :rs) printf("%lld, ",x); puts("");

  ll ans = max(sv.back(), rs.back());

  rep(i, n) {
    ll w = sv[i + 1] + rs[n - i - 1] - x[i];
    // dbg(i,w,sv[i+1],rs[n-i],x[i]);
    ans = max(ans, w);
  }
  rep(i, n) {
    ll w = rs[i + 1] + sv[n - i - 1] - rx[i];
    // dbg(i,w,rs[i+1],sv[n-i],rx[i]);
    ans = max(ans, w);
  }
  if (ans < 0)
    ans = 0;
  cout << ans << "\n";
  return 0;
}