Problem p03372 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;

const int maxn = 2005;
const int Mod = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e = exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;

#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout << #x << " = " << (x) << endl
ll sum1[maxn], sum2[maxn];
ll max1[maxn], max2[maxn], max3[maxn], max4[maxn];
ll x[maxn], v[maxn];
int main() {
  ios::sync_with_stdio(false);
  // freopen("a.txt","r",stdin);
  // freopen("b.txt","w",stdout);
  int n;
  ll c;
  ll ans = -LL_INF;
  cin >> n >> c;
  x[n + 1] = c;
  for (int i = 1; i <= n; i++)
    cin >> x[i] >> v[i];
  for (int i = 1; i <= n; i++)
    sum1[i] = sum1[i - 1] + v[i] - (x[i] - x[i - 1]);
  for (int i = n; i >= 1; i--)
    sum2[i] = sum2[i + 1] + v[i] - (x[i + 1] - x[i]);
  for (int i = 1; i <= n; i++)
    max1[i] = max(max1[i - 1], sum1[i] - x[i]);
  for (int i = n; i >= 1; i--)
    max2[i] = max(max2[i + 1], sum2[i] - (c - x[i]));
  for (int i = 1; i <= n; i++)
    max3[i] = max(max3[i], sum1[i]);
  for (int i = 1; i <= n; i++)
    max4[i] = max(max4[i], sum2[i]);
  ans = max(ans, max3[n]);
  ans = max(ans, max4[1]);
  for (int i = 1; i <= n; i++)
    ans = max(ans, max4[i] + max1[i - 1]);
  for (int i = 1; i <= n; i++)
    ans = max(ans, max3[i] + max2[i + 1]);
  cout << ans << endl;
  // cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" <<
  // endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;

const int maxn = 1e5 + 5;
const int Mod = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e = exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;

#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout << #x << " = " << (x) << endl
ll sum1[maxn], sum2[maxn];
ll max1[maxn], max2[maxn], max3[maxn], max4[maxn];
ll x[maxn], v[maxn];
int main() {
  ios::sync_with_stdio(false);
  // freopen("a.txt","r",stdin);
  // freopen("b.txt","w",stdout);
  int n;
  ll c;
  ll ans = -LL_INF;
  cin >> n >> c;
  x[n + 1] = c;
  for (int i = 1; i <= n; i++)
    cin >> x[i] >> v[i];
  for (int i = 1; i <= n; i++)
    sum1[i] = sum1[i - 1] + v[i] - (x[i] - x[i - 1]);
  for (int i = n; i >= 1; i--)
    sum2[i] = sum2[i + 1] + v[i] - (x[i + 1] - x[i]);
  for (int i = 1; i <= n; i++)
    max1[i] = max(max1[i - 1], sum1[i] - x[i]);
  for (int i = n; i >= 1; i--)
    max2[i] = max(max2[i + 1], sum2[i] - (c - x[i]));
  for (int i = 1; i <= n; i++)
    max3[i] = max(max3[i], sum1[i]);
  for (int i = 1; i <= n; i++)
    max4[i] = max(max4[i], sum2[i]);
  ans = max(ans, max3[n]);
  ans = max(ans, max4[1]);
  for (int i = 1; i <= n; i++)
    ans = max(ans, max4[i] + max1[i - 1]);
  for (int i = 1; i <= n; i++)
    ans = max(ans, max3[i] + max2[i + 1]);
  cout << ans << endl;
  // cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" <<
  // endl;
  return 0;
}