Problem p03187 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
const ll mod = 1000000007;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head

const int N = 101000;
ll L, s[N], t[N];
int n, l[N];
ll dist(ll a, ll b) {
  if (a > b)
    swap(a, b);
  return max(b - a, L - (b - a));
}
ll gao1(int l, int r) { return s[min(r, n)] - s[max(l - 1, 0)]; }
ll gao2(int l, int r) { return t[min(r, n)] - t[max(l - 1, 0)]; }
int main() {
  scanf("%lld%d", &L, &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", l + i), s[i] = s[i - 1] + l[i], t[i] = t[i - 1] + L - l[i];
  if (n == 1) {
    printf("%lld\n", dist(0, l[1]));
    return 0;
  }
  ll ans = 0;
  for (int s = 1; s <= n; s++) {
    int fl = s - 1, r = n - s;
    if (fl > 0) {
      // ting zai s-1
      int len = min(fl - 1, r);
      ll w = 2 * (gao1(s - len - 1, s - 1) + gao2(s + 1, s + len)) - l[s - 1] +
             dist(l[s - 1], l[s]);
      ans = max(ans, w);
      len = min(fl, r);
      w = 2 * (gao1(s - len, s - 1) + gao2(s + 1, s + len)) - l[s - 1] +
          dist(l[s - 1], l[s]);
      ans = max(ans, w);
    }
    if (r > 0) {
      int len = min(r - 1, fl);
      ll w = 2 * (gao2(s + 1, s + len + 1) + gao1(s - len, s - 1)) -
             (L - l[s + 1]) + dist(l[s], l[s + 1]);
      ans = max(ans, w);
      //			printf("%d %d %d %d %lld\n",s,s+1,len,len+1,w);
      len = min(r, fl);
      w = 2 * (gao2(s + 1, s + len) + gao1(s - len, s - 1)) - (L - l[s + 1]) +
          dist(l[s], l[s + 1]);
      ans = max(ans, w);
      //			printf("%d %d %d %d %lld\n",s,s+1,len,len,w);
    }
  }
  printf("%lld\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
const ll mod = 1000000007;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head

const int N = 501000;
ll L, s[N], t[N];
int n, l[N];
ll dist(ll a, ll b) {
  if (a > b)
    swap(a, b);
  return max(b - a, L - (b - a));
}
ll gao1(int l, int r) { return s[min(r, n)] - s[max(l - 1, 0)]; }
ll gao2(int l, int r) { return t[min(r, n)] - t[max(l - 1, 0)]; }
int main() {
  scanf("%lld%d", &L, &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", l + i), s[i] = s[i - 1] + l[i], t[i] = t[i - 1] + L - l[i];
  if (n == 1) {
    printf("%lld\n", dist(0, l[1]));
    return 0;
  }
  ll ans = 0;
  for (int s = 1; s <= n; s++) {
    int fl = s - 1, r = n - s;
    if (fl > 0) {
      // ting zai s-1
      int len = min(fl - 1, r);
      ll w = 2 * (gao1(s - len - 1, s - 1) + gao2(s + 1, s + len)) - l[s - 1] +
             dist(l[s - 1], l[s]);
      ans = max(ans, w);
      len = min(fl, r);
      w = 2 * (gao1(s - len, s - 1) + gao2(s + 1, s + len)) - l[s - 1] +
          dist(l[s - 1], l[s]);
      ans = max(ans, w);
    }
    if (r > 0) {
      int len = min(r - 1, fl);
      ll w = 2 * (gao2(s + 1, s + len + 1) + gao1(s - len, s - 1)) -
             (L - l[s + 1]) + dist(l[s], l[s + 1]);
      ans = max(ans, w);
      //			printf("%d %d %d %d %lld\n",s,s+1,len,len+1,w);
      len = min(r, fl);
      w = 2 * (gao2(s + 1, s + len) + gao1(s - len, s - 1)) - (L - l[s + 1]) +
          dist(l[s], l[s + 1]);
      ans = max(ans, w);
      //			printf("%d %d %d %d %lld\n",s,s+1,len,len,w);
    }
  }
  printf("%lld\n", ans);
}