Problem p03187 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define inf 1050000000
#define infll 1050000000000000000ll
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mpr make_pair
#define fi first
#define se second
#define vi vector<int>
#define pb push_back
#define ad(x, y) (x = (x + (y)) % mod)
#define up(x, y) (x < (y) ? x = (y) : 0)
#define dn(x, y) (x > (y) ? x = (y) : 0)
#define N 200009
using namespace std;

int n;
ll m, a[N], s[N];
ll calc(int x, int y) {
  if (x > y)
    return 0;
  return s[y] - s[x - 1];
}
int main() {
  scanf("%lld%d", &m, &n);
  int i, tmp;
  ll ans = 0;
  for (i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  a[n + 1] = m;
  for (i = n + 2; i <= n * 2 + 1; i++)
    a[i] = a[i - n - 1] + m;
  for (i = 1; i <= n * 2 + 1; i++)
    s[i] = s[i - 1] + a[i];
  // for (i=1; i<=n*2+1; i++) cerr<<a[i]<<' ';cerr<<'\n';
  for (i = 1; i <= n; i++) {
    if (i == 1)
      ans = max(a[1], m - a[1]);
    else {
      tmp = min(i - 1, n - i + 1);
      ans = max(ans, -a[i] - a[i + tmp] - 2 * calc(i + 1, i + tmp - 1) +
                         2 * calc(n + i - tmp + 1, n + i));
      //	cerr<<i<<' '<<tmp<<'\n';
      tmp = min(i, n - i + 1);
      ans = max(ans, -a[i] - 2 * calc(i + 1, i + tmp - 1) + a[n + i - tmp + 1] +
                         2 * calc(n + i - tmp + 2, n + i));
    }
  }
  cerr << "ans:" << ans << '\n';
  for (i = 1; i <= n; i++)
    a[i] = m - a[i];
  sort(a + 1, a + n + 1);
  a[n + 1] = m;
  for (i = n + 2; i <= n * 2 + 1; i++)
    a[i] = a[i - n - 1] + m;
  for (i = 1; i <= n * 2 + 1; i++)
    s[i] = s[i - 1] + a[i];
  // for (i=1; i<=n*2+1; i++) cerr<<a[i]<<' ';cerr<<'\n';
  for (i = 1; i <= n; i++) {
    if (i == 1)
      ans = max(max(ans, a[1]), m - a[1]);
    else {
      tmp = min(i - 1, n - i + 1);
      ans = max(ans, -a[i] - a[i + tmp] - 2 * calc(i + 1, i + tmp - 1) +
                         2 * calc(n + i - tmp + 1, n + i));
      tmp = min(i, n - i + 1);
      ans = max(ans, -a[i] - 2 * calc(i + 1, i + tmp - 1) + a[n + i - tmp + 1] +
                         2 * calc(n + i - tmp + 2, n + i));
    }
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define inf 1050000000
#define infll 1050000000000000000ll
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mpr make_pair
#define fi first
#define se second
#define vi vector<int>
#define pb push_back
#define ad(x, y) (x = (x + (y)) % mod)
#define up(x, y) (x < (y) ? x = (y) : 0)
#define dn(x, y) (x > (y) ? x = (y) : 0)
#define N 2000009
using namespace std;

int n;
ll m, a[N], s[N];
ll calc(int x, int y) {
  if (x > y)
    return 0;
  return s[y] - s[x - 1];
}
int main() {
  scanf("%lld%d", &m, &n);
  int i, tmp;
  ll ans = 0;
  for (i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  a[n + 1] = m;
  for (i = n + 2; i <= n * 2 + 1; i++)
    a[i] = a[i - n - 1] + m;
  for (i = 1; i <= n * 2 + 1; i++)
    s[i] = s[i - 1] + a[i];
  // for (i=1; i<=n*2+1; i++) cerr<<a[i]<<' ';cerr<<'\n';
  for (i = 1; i <= n; i++) {
    if (i == 1)
      ans = max(a[1], m - a[1]);
    else {
      tmp = min(i - 1, n - i + 1);
      ans = max(ans, -a[i] - a[i + tmp] - 2 * calc(i + 1, i + tmp - 1) +
                         2 * calc(n + i - tmp + 1, n + i));
      //	cerr<<i<<' '<<tmp<<'\n';
      tmp = min(i, n - i + 1);
      ans = max(ans, -a[i] - 2 * calc(i + 1, i + tmp - 1) + a[n + i - tmp + 1] +
                         2 * calc(n + i - tmp + 2, n + i));
    }
  }
  cerr << "ans:" << ans << '\n';
  for (i = 1; i <= n; i++)
    a[i] = m - a[i];
  sort(a + 1, a + n + 1);
  a[n + 1] = m;
  for (i = n + 2; i <= n * 2 + 1; i++)
    a[i] = a[i - n - 1] + m;
  for (i = 1; i <= n * 2 + 1; i++)
    s[i] = s[i - 1] + a[i];
  // for (i=1; i<=n*2+1; i++) cerr<<a[i]<<' ';cerr<<'\n';
  for (i = 1; i <= n; i++) {
    if (i == 1)
      ans = max(max(ans, a[1]), m - a[1]);
    else {
      tmp = min(i - 1, n - i + 1);
      ans = max(ans, -a[i] - a[i + tmp] - 2 * calc(i + 1, i + tmp - 1) +
                         2 * calc(n + i - tmp + 1, n + i));
      tmp = min(i, n - i + 1);
      ans = max(ans, -a[i] - 2 * calc(i + 1, i + tmp - 1) + a[n + i - tmp + 1] +
                         2 * calc(n + i - tmp + 2, n + i));
    }
  }
  printf("%lld\n", ans);
  return 0;
}