Problem p03187 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
#define ll long long
#define REP(i, a, b) for (int i = (a), _end_ = (b); i <= _end_; i++)
#define DREP(i, a, b) for (int i = (a), _end_ = (b); i >= _end_; i--)
#define EREP(i, a) for (int i = start[(a)]; i; i = e[i].next)
template <class T> inline void chkmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> inline void chkmin(T &a, T b) {
  if (a > b)
    a = b;
}
#define fi first
#define se second
#define mkr(a, b) make_pair(a, b)
inline int read() {
  int sum = 0, p = 1;
  char ch = getchar();
  while (!(('0' <= ch && ch <= '9') || ch == '-'))
    ch = getchar();
  if (ch == '-')
    p = -1, ch = getchar();
  while ('0' <= ch && ch <= '9')
    sum = sum * 10 + ch - 48, ch = getchar();
  return sum * p;
}

const int maxn = 1e5 + 20;

int n, L, x[maxn], y[maxn];
ll sx[maxn], sy[maxn];

inline ll X(int l, int r) { return sx[r] - sx[l - 1]; }
inline ll Y(int l, int r) { return sy[r] - sy[l - 1]; }

inline void init() {
  L = read();
  n = read();
  REP(i, 1, n) sx[i] = x[i] = read(), sy[i] = y[i] = L - x[i];
  REP(i, 1, n) sx[i] += sx[i - 1], sy[i] += sy[i - 1];
}

ll ans;

inline void doing() {
  ll res;
  chkmax(ans, (ll)x[n]);
  REP(i, 1, n >> 1) {
    int A = i, B = i, now = n - A - B;
    // Right
    res = 0;
    res += X(now + 1, now + B) * 2;
    res += Y(now + B + 2, n) * 2;
    res += y[now + B + 1];
    chkmax(ans, res);

    res = x[now];
    res += X(now + 1, now + B - 1) * 2 + x[now + B];
    res += Y(now + B + 1, n) * 2;
    chkmax(ans, res);

    if (i * 2 + 1 <= n) {
      res = 0;
      A = i + 1;
      B = i;
      now = n - A - B;
      res += x[now];
      res += X(now + 1, now + B) * 2;
      res += Y(now + B + 2, n) * 2;
      res += y[now + B + 1];
      chkmax(ans, res);

      res = 0;
      A = i;
      B = i + 1;
      now = n - A - B;
      res += X(now + 1, now + B - 1) * 2;
      res += x[now + B];
      res += Y(now + B + 1, n) * 2;
      chkmax(ans, res);
    }
  }
}

int main() {
  init();
  doing();
  swap(x, y);
  reverse(x + 1, x + n + 1);
  reverse(y + 1, y + n + 1);
  REP(i, 1, n) sx[i] = sx[i - 1] + x[i], sy[i] = sy[i - 1] + y[i];
  doing();
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
#define ll long long
#define REP(i, a, b) for (int i = (a), _end_ = (b); i <= _end_; i++)
#define DREP(i, a, b) for (int i = (a), _end_ = (b); i >= _end_; i--)
#define EREP(i, a) for (int i = start[(a)]; i; i = e[i].next)
template <class T> inline void chkmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> inline void chkmin(T &a, T b) {
  if (a > b)
    a = b;
}
#define fi first
#define se second
#define mkr(a, b) make_pair(a, b)
inline int read() {
  int sum = 0, p = 1;
  char ch = getchar();
  while (!(('0' <= ch && ch <= '9') || ch == '-'))
    ch = getchar();
  if (ch == '-')
    p = -1, ch = getchar();
  while ('0' <= ch && ch <= '9')
    sum = sum * 10 + ch - 48, ch = getchar();
  return sum * p;
}

const int maxn = 5e5 + 20;

int n, L, x[maxn], y[maxn];
ll sx[maxn], sy[maxn];

inline ll X(int l, int r) { return sx[r] - sx[l - 1]; }
inline ll Y(int l, int r) { return sy[r] - sy[l - 1]; }

inline void init() {
  L = read();
  n = read();
  REP(i, 1, n) sx[i] = x[i] = read(), sy[i] = y[i] = L - x[i];
  REP(i, 1, n) sx[i] += sx[i - 1], sy[i] += sy[i - 1];
}

ll ans;

inline void doing() {
  ll res;
  chkmax(ans, (ll)x[n]);
  REP(i, 1, n >> 1) {
    int A = i, B = i, now = n - A - B;
    // Right
    res = 0;
    res += X(now + 1, now + B) * 2;
    res += Y(now + B + 2, n) * 2;
    res += y[now + B + 1];
    chkmax(ans, res);

    res = x[now];
    res += X(now + 1, now + B - 1) * 2 + x[now + B];
    res += Y(now + B + 1, n) * 2;
    chkmax(ans, res);

    if (i * 2 + 1 <= n) {
      res = 0;
      A = i + 1;
      B = i;
      now = n - A - B;
      res += x[now];
      res += X(now + 1, now + B) * 2;
      res += Y(now + B + 2, n) * 2;
      res += y[now + B + 1];
      chkmax(ans, res);

      res = 0;
      A = i;
      B = i + 1;
      now = n - A - B;
      res += X(now + 1, now + B - 1) * 2;
      res += x[now + B];
      res += Y(now + B + 1, n) * 2;
      chkmax(ans, res);
    }
  }
}

int main() {
  init();
  doing();
  swap(x, y);
  reverse(x + 1, x + n + 1);
  reverse(y + 1, y + n + 1);
  REP(i, 1, n) sx[i] = sx[i - 1] + x[i], sy[i] = sy[i - 1] + y[i];
  doing();
  printf("%lld\n", ans);
  return 0;
}