Problem p03372 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

#define rep(i, x) for (ll i = 0; i < (ll)(x); i++)
#define rrep(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)

#define _upgrade                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

// ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll x, ll y) {return x / gcd(x, y) * y;}

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

const ll mod = 1e9 + 7;
const ll IMF = 1LL << 29;
const double PI = 3.14159265358979323846;

// ******************************CODE**********************************//

int main(void) {
  ll N, C;
  cin >> N >> C;
  vector<ll> x(N + 2), v(N + 2);
  for (ll i = 1; i <= N; i++) {
    cin >> x[i] >> v[i];
  }
  x[0] = 0;
  x[N + 1] = C;

  vector<vector<ll>> dp(2, vector<ll>(10010, 0));
  ll maxcal = 0;

  ll cal = 0;
  for (ll i = 1; i <= N; i++) {
    cal += x[i - 1] - x[i];
    cal += v[i];
    dp[0][i] = max(dp[0][i - 1], cal);
  }

  cal = 0;
  for (ll i = N; i >= 1; i--) {
    cal += x[i] - x[i + 1];
    cal += v[i];
    dp[1][i] = max(dp[1][i + 1], cal);
  }

  for (ll i = 0; i <= N; i++) {
    ll sum = 0;
    sum += dp[0][i];
    sum -= x[i];
    sum += dp[1][i + 1];
    maxcal = max(maxcal, sum);

    sum = 0;
    sum += dp[1][i + 1];
    sum -= C;
    sum += x[i + 1];
    sum += dp[0][i];
    maxcal = max(maxcal, sum);
  }

  cout << maxcal << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

#define rep(i, x) for (ll i = 0; i < (ll)(x); i++)
#define rrep(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)

#define _upgrade                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

// ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll x, ll y) {return x / gcd(x, y) * y;}

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

const ll mod = 1e9 + 7;
const ll IMF = 1LL << 29;
const double PI = 3.14159265358979323846;

// ******************************CODE**********************************//

int main(void) {
  ll N, C;
  cin >> N >> C;
  vector<ll> x(N + 2), v(N + 2);
  for (ll i = 1; i <= N; i++) {
    cin >> x[i] >> v[i];
  }
  x[0] = 0;
  x[N + 1] = C;

  vector<vector<ll>> dp(2, vector<ll>(100010, 0));
  ll maxcal = 0;

  ll cal = 0;
  for (ll i = 1; i <= N; i++) {
    cal += x[i - 1] - x[i];
    cal += v[i];
    dp[0][i] = max(dp[0][i - 1], cal);
  }

  cal = 0;
  for (ll i = N; i >= 1; i--) {
    cal += x[i] - x[i + 1];
    cal += v[i];
    dp[1][i] = max(dp[1][i + 1], cal);
  }

  for (ll i = 0; i <= N; i++) {
    ll sum = 0;
    sum += dp[0][i];
    sum -= x[i];
    sum += dp[1][i + 1];
    maxcal = max(maxcal, sum);

    sum = 0;
    sum += dp[1][i + 1];
    sum -= C;
    sum += x[i + 1];
    sum += dp[0][i];
    maxcal = max(maxcal, sum);
  }

  cout << maxcal << endl;
  return 0;
}