Problem p03187 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MOD 1e9 + 7
using namespace std;
#define pp push_back
#define po pop_back
#define mp make_pair
#define clr(a) memset(a, 0, sizeof(a))
#define neg(a) memset(a, -1, sizeof(a))
#define fas(a) memset(a, false, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define debug1(x) cout << #x << ": " << x << " "
#define rev(x) reverse(x.begin(), x.end())
#define int long long
#define F first
#define S second
// int a[2000];
void printvector(std::vector<int> v) {
  for (int i = 0; i < v.size(); ++i) {
    cout << v[i] << " ";
  }
  cout << "" << endl;
}
void printarray(int a[], int n) {
  for (int i = 0; i < n; ++i) {
    cout << a[i] << " ";
  }
  cout << "" << endl;
}

int L, n;
int get(int x[], int y[]) {

  // printarray(x,n);
  // printarray(y,n);
  int px[n], py[n];
  for (int i = 0; i < n; ++i) {
    px[i] = 2 * x[i];
    if (i != 0)
      px[i] += px[i - 1];
  }
  for (int i = n - 1; i >= 0; i--) {
    py[i] = 2 * y[i];
    if (i != n - 1)
      py[i] += py[i + 1];
  }
  int fans = 0;
  for (int i = 0; i < n; i++) {
    int ans = 0;
    int left = 0, right = 0;
    if (i + 1 > n - (i + 1)) {
      left = n - (i + 1) + 1;
      right = n - (i + 1);

    } else {
      left = i + 1;
      right = i + 1;
    }
    ans += px[i] - x[i];
    if (i - left >= 0)
      ans -= px[i - left];
    if (right > 0) {
      ans += py[i + 1];
      if (i + 1 + right < n)
        ans -= py[i + 1 + right];
    }

    fans = max(fans, ans);
  }

  // debug(fans);

  return fans;
}

void solve() {
  cin >> L >> n;
  int x[n], y[n];
  for (int i = 0; i < n; i++)
    cin >> x[i];
  for (int i = 0; i < n; i++)
    y[i] = L - x[i];

  int ans1 = get(x, y);
  for (int i = 0; i < n; i++)
    swap(x[i], y[i]);
  for (int i = 0; i < n / 2; i++) {
    swap(x[i], x[n - i - 1]);
    swap(y[i], y[n - i - 1]);
  }
  int ans2 = get(x, y);

  cout << max(ans1, ans2) << endl;
}

int32_t main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  solve();

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MOD 1e9 + 7
using namespace std;
#define pp push_back
#define po pop_back
#define mp make_pair
#define clr(a) memset(a, 0, sizeof(a))
#define neg(a) memset(a, -1, sizeof(a))
#define fas(a) memset(a, false, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define debug1(x) cout << #x << ": " << x << " "
#define rev(x) reverse(x.begin(), x.end())
#define int long long
#define F first
#define S second
// int a[2000];
void printvector(std::vector<int> v) {
  for (int i = 0; i < v.size(); ++i) {
    cout << v[i] << " ";
  }
  cout << "" << endl;
}
void printarray(int a[], int n) {
  for (int i = 0; i < n; ++i) {
    cout << a[i] << " ";
  }
  cout << "" << endl;
}

int L, n;
int get(int x[], int y[]) {

  // printarray(x,n);
  // printarray(y,n);
  int px[n], py[n];
  for (int i = 0; i < n; ++i) {
    px[i] = 2 * x[i];
    if (i != 0)
      px[i] += px[i - 1];
  }
  for (int i = n - 1; i >= 0; i--) {
    py[i] = 2 * y[i];
    if (i != n - 1)
      py[i] += py[i + 1];
  }
  int fans = 0;
  for (int i = 0; i < n; i++) {
    int ans = 0;
    int left = 0, right = 0;
    if (i + 1 > n - (i + 1)) {
      left = n - (i + 1) + 1;
      right = n - (i + 1);

    } else {
      left = i + 1;
      right = i + 1;
    }
    ans += px[i] - x[i];
    if (i - left >= 0)
      ans -= px[i - left];
    if (right > 0) {
      ans += py[i + 1];
      if (i + 1 + right < n)
        ans -= py[i + 1 + right];
    }

    fans = max(fans, ans);
  }

  // debug(fans);

  return fans;
}

void solve() {
  cin >> L >> n;
  int x[n], y[n];
  for (int i = 0; i < n; i++)
    cin >> x[i];
  for (int i = 0; i < n; i++)
    y[i] = L - x[i];

  int ans1 = get(x, y);
  for (int i = 0; i < n; i++)
    swap(x[i], y[i]);
  for (int i = 0; i < n / 2; i++) {
    swap(x[i], x[n - i - 1]);
    swap(y[i], y[n - i - 1]);
  }
  int ans2 = get(x, y);

  cout << max(ans1, ans2) << endl;
}

int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  solve();

  return 0;
}