Problem p03374 (C++)

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)

#define in_range(x, y, w, h)                                                   \
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
const ll INFLL = 1e18;
typedef pair<ll, ll> PII;
typedef vector<vector<int>> Graph;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

int main() {
  ios::sync_with_stdio(false);

  int N;
  ll C;
  cin >> N >> C;
  vector<PII> sushi(N);
  map<int, PII> sushi_l, sushi_r;
  rep(i, N) { cin >> sushi[i].first >> sushi[i].second; }

  assert(N <= 100);

  sushi_l[0] = PII(C, 0);
  sushi_r[0] = PII(0, 0);
  rep(i, N) {
    sushi_l[(i + 1)] = sushi[N - i - 1];
    sushi_r[(i + 1)] = sushi[i];
  }

  ll ans = 0LL;

  vector<ll> mx_r(N + 1);
  ll sumr = 0;
  for (int r = 0; r < N; r++) {
    sumr += sushi_r[r + 1].second;
    ans = max(ans, sumr - sushi_r[r + 1].first);

    mx_r[r + 1] = sumr - sushi_r[r + 1].first;
    mx_r[r + 1] = max(mx_r[r + 1], mx_r[r]);
  }

  vector<ll> mx_l(N + 1);
  ll suml = 0;
  for (int l = 0; l < N; l++) {
    suml += sushi_l[l + 1].second;
    ans = max(ans, suml - (C - sushi_l[l + 1].first));

    mx_l[l + 1] = suml - (C - sushi_l[l + 1].first);
    mx_l[l + 1] = max(mx_l[l + 1], mx_l[l]);
  }

  suml = 0;
  for (int l = 0; l < N; l++) {
    suml += sushi_l[l + 1].second;
    ll tmp = suml - 2 * (C - sushi_l[l + 1].first) + mx_r[N - l - 1];
    ans = max(ans, tmp);
    DUMP2(l, tmp);
  }

  sumr = 0;
  for (int r = 0; r < N; r++) {
    sumr += sushi_r[r + 1].second;
    ll tmp = sumr - 2 * sushi_r[r + 1].first + mx_l[N - r - 1];
    ans = max(ans, tmp);
    DUMP2(r, tmp);
  }

  cout << ans << endl;
}

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)

#define in_range(x, y, w, h)                                                   \
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
const ll INFLL = 1e18;
typedef pair<ll, ll> PII;
typedef vector<vector<int>> Graph;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

int main() {
  ios::sync_with_stdio(false);

  int N;
  ll C;
  cin >> N >> C;
  vector<PII> sushi(N);
  map<int, PII> sushi_l, sushi_r;
  rep(i, N) { cin >> sushi[i].first >> sushi[i].second; }

  sushi_l[0] = PII(C, 0);
  sushi_r[0] = PII(0, 0);
  rep(i, N) {
    sushi_l[(i + 1)] = sushi[N - i - 1];
    sushi_r[(i + 1)] = sushi[i];
  }

  ll ans = 0LL;

  vector<ll> mx_r(N + 1);
  ll sumr = 0;
  for (int r = 0; r < N; r++) {
    sumr += sushi_r[r + 1].second;
    ans = max(ans, sumr - sushi_r[r + 1].first);

    mx_r[r + 1] = sumr - sushi_r[r + 1].first;
    mx_r[r + 1] = max(mx_r[r + 1], mx_r[r]);
  }

  vector<ll> mx_l(N + 1);
  ll suml = 0;
  for (int l = 0; l < N; l++) {
    suml += sushi_l[l + 1].second;
    ans = max(ans, suml - (C - sushi_l[l + 1].first));

    mx_l[l + 1] = suml - (C - sushi_l[l + 1].first);
    mx_l[l + 1] = max(mx_l[l + 1], mx_l[l]);
  }

  suml = 0;
  for (int l = 0; l < N; l++) {
    suml += sushi_l[l + 1].second;
    ll tmp = suml - 2 * (C - sushi_l[l + 1].first) + mx_r[N - l - 1];
    ans = max(ans, tmp);
    DUMP2(l, tmp);
  }

  sumr = 0;
  for (int r = 0; r < N; r++) {
    sumr += sushi_r[r + 1].second;
    ll tmp = sumr - 2 * sushi_r[r + 1].first + mx_l[N - r - 1];
    ans = max(ans, tmp);
    DUMP2(r, tmp);
  }

  cout << ans << endl;
}