Problem p03187 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define MAX_N 2000

using namespace std;

typedef long long lint;

int n, l;

int v[MAX_N + 1];

lint rez = 0;

lint st[MAX_N + 1];
lint dr[MAX_N + 2];

lint getSumSt(int s, int d) {
  if (s > d)
    return 0;

  return (st[d] - st[s - 1]);
}

lint getSumDr(int s, int d) {
  if (s > d)
    return 0;

  return (dr[n - d + 1] - dr[n - s + 1 + 1]);
}

void calc(int cl, int sign) {
  lint cr;
  int i, j, h;
  for (int x = 1; x <= n; x++) {
    cr = v[x];

    if (cl == 1)
      cr = l - v[n - x + 1];

    h = cl;

    i = x;
    j = 0;

    if (cl == 1)
      swap(i, j);

    cout << "AM INCEPUT " << i << " " << j << " " << h << " COST " << cr
         << "\n";

    while (i + j < n) {
      if (h == 0) {
        cr = cr + v[i] + l - v[n - j];
        j++;
      }

      if (h == 1) {
        cr = cr + l - v[n - j + 1] + v[i + 1];
        i++;
      }

      h = 1 - h;
    }

    cout << "AM TERMIANT " << i << " " << j << " " << h << " COST " << cr
         << "\n";

    rez = max(rez, cr);
  }
}

void calc2(int cl, int sign) {
  lint cr;
  int i, j, h;
  for (int x = 1; x <= n; x++) {
    cr = v[x];

    if (cl == 1)
      cr = l - v[n - x + 1];

    h = cl;

    i = x;
    j = 0;

    if (cl == 1)
      swap(i, j);

    // cout << "AM INCEPUT " << i << " " << j << " " << h << " COST " << cr <<
    // "\n";

    int oi = i;
    int oj = j;
    int inc = n - (i + j);

    if (inc > 0) {
      i += (inc >> 1);
      j += (inc >> 1);

      int ok = 0;
      if ((inc & 1) == 1) {
        if (h == 1)
          i++;

        else
          j++;

        ok = 1;
      }

      cr = cr + getSumSt(oi, i) +
           getSumDr(oj, j) /*+ (getSumSt(oi + (1 - h), i - (1 - h)) +
                              getSumDr(oj + h, j - h)) * (min(getSumSt(oi + (1 -
                              h), i - (1 - h)), getSumDr(oj + h, j - h)) > 0)*/
          ;

      if (ok == 0)
        cr = cr + getSumSt(oi + (1 - h), i - (1 - h)) + getSumDr(oj + h, j - h);

      else
        cr = cr + getSumSt(oi + (1 - h), i - h) + getSumDr(oj + h, j - (1 - h));
    }

    // cout << "AM TERMIANT " << i << " " << j << " " << h << " COST " << cr  <<
    // "\n";

    rez = max(rez, cr);
  }
}
int main() {
  cin >> l >> n;

  for (int i = 1; i <= n; i++) {
    cin >> v[i];

    st[i] = st[i - 1] + v[i];
  }

  for (int i = n; i >= 1; i--)
    dr[i] = dr[i + 1] + (l - v[i]);

  //    calc(0, 1);
  // calc(1, -1);

  //  cout << "\n";

  calc2(0, 1);
  calc2(1, -1);

  cout << rez << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define MAX_N 200000

using namespace std;

typedef long long lint;

int n, l;

int v[MAX_N + 1];

lint rez = 0;

lint st[MAX_N + 1];
lint dr[MAX_N + 2];

lint getSumSt(int s, int d) {
  if (s > d)
    return 0;

  return (st[d] - st[s - 1]);
}

lint getSumDr(int s, int d) {
  if (s > d)
    return 0;

  return (dr[n - d + 1] - dr[n - s + 1 + 1]);
}

void calc(int cl, int sign) {
  lint cr;
  int i, j, h;
  for (int x = 1; x <= n; x++) {
    cr = v[x];

    if (cl == 1)
      cr = l - v[n - x + 1];

    h = cl;

    i = x;
    j = 0;

    if (cl == 1)
      swap(i, j);

    cout << "AM INCEPUT " << i << " " << j << " " << h << " COST " << cr
         << "\n";

    while (i + j < n) {
      if (h == 0) {
        cr = cr + v[i] + l - v[n - j];
        j++;
      }

      if (h == 1) {
        cr = cr + l - v[n - j + 1] + v[i + 1];
        i++;
      }

      h = 1 - h;
    }

    cout << "AM TERMIANT " << i << " " << j << " " << h << " COST " << cr
         << "\n";

    rez = max(rez, cr);
  }
}

void calc2(int cl, int sign) {
  lint cr;
  int i, j, h;
  for (int x = 1; x <= n; x++) {
    cr = v[x];

    if (cl == 1)
      cr = l - v[n - x + 1];

    h = cl;

    i = x;
    j = 0;

    if (cl == 1)
      swap(i, j);

    // cout << "AM INCEPUT " << i << " " << j << " " << h << " COST " << cr <<
    // "\n";

    int oi = i;
    int oj = j;
    int inc = n - (i + j);

    if (inc > 0) {
      i += (inc >> 1);
      j += (inc >> 1);

      int ok = 0;
      if ((inc & 1) == 1) {
        if (h == 1)
          i++;

        else
          j++;

        ok = 1;
      }

      cr = cr + getSumSt(oi, i) +
           getSumDr(oj, j) /*+ (getSumSt(oi + (1 - h), i - (1 - h)) +
                              getSumDr(oj + h, j - h)) * (min(getSumSt(oi + (1 -
                              h), i - (1 - h)), getSumDr(oj + h, j - h)) > 0)*/
          ;

      if (ok == 0)
        cr = cr + getSumSt(oi + (1 - h), i - (1 - h)) + getSumDr(oj + h, j - h);

      else
        cr = cr + getSumSt(oi + (1 - h), i - h) + getSumDr(oj + h, j - (1 - h));
    }

    // cout << "AM TERMIANT " << i << " " << j << " " << h << " COST " << cr  <<
    // "\n";

    rez = max(rez, cr);
  }
}
int main() {
  cin >> l >> n;

  for (int i = 1; i <= n; i++) {
    cin >> v[i];

    st[i] = st[i - 1] + v[i];
  }

  for (int i = n; i >= 1; i--)
    dr[i] = dr[i + 1] + (l - v[i]);

  //    calc(0, 1);
  // calc(1, -1);

  //  cout << "\n";

  calc2(0, 1);
  calc2(1, -1);

  cout << rez << "\n";

  return 0;
}