Problem p03187 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
using namespace std;

long long X[100000];
long long cum_sum_x[100000];
long long reverse_cum_sum_x[100000];
int main() {
  long long L;
  int N;
  cin >> L >> N;
  for (int i = 0; i < N; i++) {
    cin >> X[i];
  }

  cum_sum_x[0] = X[0];
  for (int i = 1; i < N; i++) {
    cum_sum_x[i] = cum_sum_x[i - 1] + X[i];
  }

  reverse_cum_sum_x[N - 1] = L - X[N - 1];
  for (int i = N - 2; i >= 0; i--) {
    reverse_cum_sum_x[i] = reverse_cum_sum_x[i + 1] + (L - X[i]);
  }

  long long ans = 0;
  // no turnaround
  ans = max(ans, X[N - 1]);
  ans = max(ans, L - X[0]);

  for (int i = 0; i < N - 1; i++) {
    int till = i + (N - (i + 1)) / 2;
    int reverse_till = till + 1;

    long long tmp_ans = 2 * X[i] + 2 * (cum_sum_x[till] - cum_sum_x[i] +
                                        reverse_cum_sum_x[reverse_till]);
    if ((N - (i + 1)) % 2 == 0) {
      tmp_ans -= X[till];
    } else {
      tmp_ans -= (L - X[reverse_till]);
    }

    ans = max(ans, tmp_ans);
    // cout << i << " " << till << " " << reverse_till << endl;
    // cout << tmp_ans << endl;
  }

  for (int i = N - 1; i > 0; i--) {
    int reverse_till = i - i / 2;
    int till = reverse_till - 1;
    long long tmp_ans =
        2 * (L - X[i]) + 2 * (reverse_cum_sum_x[reverse_till] -
                              reverse_cum_sum_x[i] + cum_sum_x[till]);
    if (i % 2 == 0) {
      tmp_ans -= (L - X[reverse_till]);
    } else {
      tmp_ans -= X[till];
    }

    ans = max(ans, tmp_ans);
    // cout << i << " " << reverse_till << " " << till << endl;
    // cout << tmp_ans << endl;
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
using namespace std;

long long X[200000];
long long cum_sum_x[200000];
long long reverse_cum_sum_x[200000];
int main() {
  long long L;
  int N;
  cin >> L >> N;
  for (int i = 0; i < N; i++) {
    cin >> X[i];
  }

  cum_sum_x[0] = X[0];
  for (int i = 1; i < N; i++) {
    cum_sum_x[i] = cum_sum_x[i - 1] + X[i];
  }

  reverse_cum_sum_x[N - 1] = L - X[N - 1];
  for (int i = N - 2; i >= 0; i--) {
    reverse_cum_sum_x[i] = reverse_cum_sum_x[i + 1] + (L - X[i]);
  }

  long long ans = 0;
  // no turnaround
  ans = max(ans, X[N - 1]);
  ans = max(ans, L - X[0]);

  for (int i = 0; i < N - 1; i++) {
    int till = i + (N - (i + 1)) / 2;
    int reverse_till = till + 1;

    long long tmp_ans = 2 * X[i] + 2 * (cum_sum_x[till] - cum_sum_x[i] +
                                        reverse_cum_sum_x[reverse_till]);
    if ((N - (i + 1)) % 2 == 0) {
      tmp_ans -= X[till];
    } else {
      tmp_ans -= (L - X[reverse_till]);
    }

    ans = max(ans, tmp_ans);
    // cout << i << " " << till << " " << reverse_till << endl;
    // cout << tmp_ans << endl;
  }

  for (int i = N - 1; i > 0; i--) {
    int reverse_till = i - i / 2;
    int till = reverse_till - 1;
    long long tmp_ans =
        2 * (L - X[i]) + 2 * (reverse_cum_sum_x[reverse_till] -
                              reverse_cum_sum_x[i] + cum_sum_x[till]);
    if (i % 2 == 0) {
      tmp_ans -= (L - X[reverse_till]);
    } else {
      tmp_ans -= X[till];
    }

    ans = max(ans, tmp_ans);
    // cout << i << " " << reverse_till << " " << till << endl;
    // cout << tmp_ans << endl;
  }

  cout << ans << endl;
  return 0;
}