Problem p03187 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while ('0' > ch || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while ('0' <= ch && ch <= '9');
  t *= f;
}
typedef long long ll;
const int maxn = 2010;
ll L, a[maxn], ans, s, dp[maxn][maxn][2];
int n;
ll s1[maxn], s2[maxn];
ll F(ll x) {
  if (x == 0)
    return 0;
  return L - x;
}
int main() {
  // freopen("1.txt","r",stdin);
  read(L);
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  for (int i = 1; i <= n; i++)
    s1[i] = s1[i - 1] + a[i];
  for (int i = n; i >= 1; i--)
    s2[i] = s2[i + 1] + F(a[i]);
  ans = max(a[n], F(a[1]));
  for (int i = 1; i <= n / 2; i++) {
    s = 2 * s1[i] + 2 * (s2[i + 1] - s2[i * 2 + 2]) - F(a[i + 1]);
    ans = max(ans, s);
    s = 2 * s1[i] + 2 * (s2[i + 1] - s2[i * 2 + 1]) - a[i];
    ans = max(ans, s);
  }
  for (int i = 1; i <= n; i++)
    a[i] = L - a[i];
  reverse(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    s1[i] = s1[i - 1] + a[i];
  for (int i = n; i >= 1; i--)
    s2[i] = s2[i + 1] + F(a[i]);
  for (int i = 1; i <= n / 2; i++) {
    s = 2 * s1[i] + 2 * (s2[i + 1] - s2[i * 2 + 2]) - F(a[i + 1]);
    ans = max(ans, s);
    s = 2 * s1[i] + 2 * (s2[i + 1] - s2[i * 2 + 1]) - a[i];
    ans = max(ans, s);
  }
  printf("%lld\n", ans);
  /*
  for (int i=0;i<=n;i++)
  for (int j=n+1;j>=1+i;j--) {
          dp[i+1][j][0]=max(dp[i+1][j][0],dp[i][j][0]+a[i+1]-a[i]);
          dp[i][j-1][1]=max(dp[i][j-1][1],dp[i][j][0]+a[i]+F(a[j-1]));
          dp[i][j-1][1]=max(dp[i][j-1][1],dp[i][j][1]+a[j]-a[j-1]);
          dp[i+1][j][0]=max(dp[i+1][j][0],dp[i][j][1]+F(a[j])+a[i+1]);
          if (i==2&&j==6) printf("%lld\n",dp[i][j][1]);
  }
  for (int i=0;i<=n;i++)
          ans=max(ans,dp[i][i+1][0]),
          ans=max(ans,dp[i][i+1][1]);
  printf("%lld\n",ans);
  */
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while ('0' > ch || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while ('0' <= ch && ch <= '9');
  t *= f;
}
typedef long long ll;
const int maxn = (2e5) + 10;
ll L, a[maxn], ans, s;
int n;
ll s1[maxn], s2[maxn];
ll F(ll x) {
  if (x == 0)
    return 0;
  return L - x;
}
int main() {
  // freopen("1.txt","r",stdin);
  read(L);
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  for (int i = 1; i <= n; i++)
    s1[i] = s1[i - 1] + a[i];
  for (int i = n; i >= 1; i--)
    s2[i] = s2[i + 1] + F(a[i]);
  ans = max(a[n], F(a[1]));
  for (int i = 1; i <= n / 2; i++) {
    s = 2 * s1[i] + 2 * (s2[i + 1] - s2[i * 2 + 2]) - F(a[i + 1]);
    ans = max(ans, s);
    s = 2 * s1[i] + 2 * (s2[i + 1] - s2[i * 2 + 1]) - a[i];
    ans = max(ans, s);
  }
  for (int i = 1; i <= n; i++)
    a[i] = L - a[i];
  reverse(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    s1[i] = s1[i - 1] + a[i];
  for (int i = n; i >= 1; i--)
    s2[i] = s2[i + 1] + F(a[i]);
  for (int i = 1; i <= n / 2; i++) {
    s = 2 * s1[i] + 2 * (s2[i + 1] - s2[i * 2 + 2]) - F(a[i + 1]);
    ans = max(ans, s);
    s = 2 * s1[i] + 2 * (s2[i + 1] - s2[i * 2 + 1]) - a[i];
    ans = max(ans, s);
  }
  printf("%lld\n", ans);
  /*
  for (int i=0;i<=n;i++)
  for (int j=n+1;j>=1+i;j--) {
          dp[i+1][j][0]=max(dp[i+1][j][0],dp[i][j][0]+a[i+1]-a[i]);
          dp[i][j-1][1]=max(dp[i][j-1][1],dp[i][j][0]+a[i]+F(a[j-1]));
          dp[i][j-1][1]=max(dp[i][j-1][1],dp[i][j][1]+a[j]-a[j-1]);
          dp[i+1][j][0]=max(dp[i+1][j][0],dp[i][j][1]+F(a[j])+a[i+1]);
          if (i==2&&j==6) printf("%lld\n",dp[i][j][1]);
  }
  for (int i=0;i<=n;i++)
          ans=max(ans,dp[i][i+1][0]),
          ans=max(ans,dp[i][i+1][1]);
  printf("%lld\n",ans);
  */
  return 0;
}