Problem p03187 (C++)

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

using namespace std;

#define INF 1e9
#define LINF 1e18
#define ld double
#define ll long long
#define ull unsigned long long

namespace io {
const int SZ = 1 << 13;
char buff[SZ], *pos = buff + SZ - 1;
char gc() {
  if (++pos == buff + SZ)
    fread(pos = buff, 1, SZ, stdin);
  return *pos;
}
} // namespace io

template <typename T> inline void read(T &x) {
  x = 0;
  int f = 1, c = io ::gc();
  for (; !isdigit(c); c = io ::gc())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = io ::gc())
    x = (x << 3) + (x << 1) + (c ^ 48);
  x *= f;
}

const int N = 2005;

int x[N];
int n, L;
ll sum[N];
ll ans;

void solve() {
  for (int i = 1; i <= n + n; ++i)
    sum[i] = sum[i - 1] + x[i];
  if (L - x[1] > ans)
    ans = L - x[1];
  if (x[n + n] - L > ans)
    ans = x[n + n] - L;
  for (int i = 2; i <= n; ++i) {
    int j = i + n - 1;
    int ed = j - n + i;
    //		printf("%d %d %d: ", i, j, ed);
    ll cur = 2 * (sum[j] - sum[n]) -
             (2 * (sum[ed - 1] - sum[i]) + x[i] + x[ed]) + abs(L - x[ed]);
    //		printf("%lld\n", cur);
    if (cur > ans)
      ans = cur;
  }
}

int main() {
  read(L);
  read(n);
  for (int i = 1; i <= n; ++i)
    read(x[i]);
  for (int i = n + 1; i <= n + n; ++i)
    x[i] = x[i - n] + L;

  solve();
  reverse(x + 1, x + n + n + 1);
  for (int i = 1; i <= n + n; ++i)
    x[i] = L + L - x[i];
  solve();

  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define INF 1e9
#define LINF 1e18
#define ld double
#define ll long long
#define ull unsigned long long

namespace io {
const int SZ = 1 << 13;
char buff[SZ], *pos = buff + SZ - 1;
char gc() {
  if (++pos == buff + SZ)
    fread(pos = buff, 1, SZ, stdin);
  return *pos;
}
} // namespace io

template <typename T> inline void read(T &x) {
  x = 0;
  int f = 1, c = io ::gc();
  for (; !isdigit(c); c = io ::gc())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = io ::gc())
    x = (x << 3) + (x << 1) + (c ^ 48);
  x *= f;
}

const int N = 400005;

int x[N];
int n, L;
ll sum[N];
ll ans;

void solve() {
  for (int i = 1; i <= n + n; ++i)
    sum[i] = sum[i - 1] + x[i];
  if (L - x[1] > ans)
    ans = L - x[1];
  if (x[n + n] - L > ans)
    ans = x[n + n] - L;
  for (int i = 2; i <= n; ++i) {
    int j = i + n - 1;
    int ed = j - n + i;
    //		printf("%d %d %d: ", i, j, ed);
    ll cur = 2 * (sum[j] - sum[n]) -
             (2 * (sum[ed - 1] - sum[i]) + x[i] + x[ed]) + abs(L - x[ed]);
    //		printf("%lld\n", cur);
    if (cur > ans)
      ans = cur;
  }
}

int main() {
  read(L);
  read(n);
  for (int i = 1; i <= n; ++i)
    read(x[i]);
  for (int i = n + 1; i <= n + n; ++i)
    x[i] = x[i - n] + L;

  solve();
  reverse(x + 1, x + n + n + 1);
  for (int i = 1; i <= n + n; ++i)
    x[i] = L + L - x[i];
  solve();

  printf("%lld\n", ans);

  return 0;
}