Problem p03187 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define f first
#define s second
#define MAXN 2510
ll OFF;

ll L[MAXN];
ll R[MAXN];
ll sum_left[MAXN];
ll sum_right[MAXN];
ll tri_left[MAXN];
ll tri_right[MAXN];
ll len, N;
ll ans = 0;

ll range_left(ll x, ll y) {
  return sum_left[y] - sum_left[x - 1];
  // cout<<x<< ' '<<y<<' '<<tri_left[y] - tri_left[x-1] - (x-1) * (sum_left[y] -
  // sum_left[x-1])<<'\n'; return tri_left[y] - tri_left[x-1] - (x-1) *
  // (sum_left[y] - sum_left[x-1]);
}

ll range_right(ll x, ll y) {
  return sum_right[x] - sum_right[y + 1];
  // cout<<x<< ' '<<y<<' '<<tri_right[x] - tri_right[y+1] - (N-x+1) *
  // (sum_right[x] - sum_right[y-1])<<'\n'; return tri_right[x] - tri_right[y+1]
  // - (N-x+1) * (sum_right[x] - sum_right[y-1]);
}

int main() {
  cin >> len >> N;
  for (ll i = 1; i <= N; ++i) {
    cin >> L[i];
    R[i] = len - L[i];
    tri_left[i] = tri_left[i - 1] + L[i] * i;
    sum_left[i] = sum_left[i - 1] + L[i];
  }
  for (ll i = N; i > 0; --i) {
    sum_right[i] = sum_right[i + 1] + R[i];
  }
  // for (ll i=1;i<=N;++i)cout<<tri_left[i]<< ' ';cout<<'\n';
  // for (ll i=1;i<=N;++i)cout<<sum_left[i]<< ' ';cout<<'\n';
  // Firstly consider if we ending on the left
  for (ll lend = 1; lend < N; ++lend) {
    ll lstart = 1;
    ll rstart = lend + 1;
    ll rend = N;
    ll offset = 0;
    ll dif =
        (rend - rstart) - (lend - lstart) + 1; // -1 if we ending on the right
    if (dif > 0) {
      // Go to right first
      rend -= dif;
      offset = R[rend + 1];
    } else if (dif < 0) {
      lstart -= dif;
      offset = L[lstart - 1];
    }
    ll tot = 0;
    // First bit is the left
    tot = tot + range_left(lstart, lend) * 2 - (L[lend]);
    // cout<<tot<<'\n';
    tot = tot + range_right(rstart, rend) * 2;
    // cout<<tot<<'\n';
    if (dif > 0)
      tot = tot + offset * 2;
    ans = max(ans, tot);
    // cout<<tot<<'\n';

    // cout<<"Left "<<lstart<<' '<<lend<<'\n';
    // cout<<"Right "<<rstart<<' '<<rend<<'\n';
    // cout<<"Offset "<<offset<<'\n';
  }
  // return 0;
  // Firstly consider if we ending on the right
  for (ll lend = 1; lend < N; ++lend) {
    ll lstart = 1;
    ll rstart = lend + 1;
    ll rend = N;
    ll offset = 0;
    ll dif =
        (rend - rstart) - (lend - lstart) - 1; // -1 if we ending on the right
    // cout<<"Dif "<<dif<<'\n';
    if (dif > 0) {
      // Go to right first
      rend -= dif;
      offset = R[rend + 1];
    } else if (dif < 0) {
      lstart -= dif;
      offset = L[lstart - 1];
    }
    ll tot = 0;
    // First bit is the left
    tot = tot + range_left(lstart, lend) * 2;
    tot = tot + range_right(rstart, rend) * 2 - R[rstart];
    if (dif < 0)
      tot = tot + offset * 2;

    ans = max(ans, tot);
    // cout<<tot<<'\n';

    // cout<<"Left "<<lstart<<' '<<lend<<'\n';
    // cout<<"Right "<<rstart<<' '<<rend<<'\n';
    // cout<<"Offset "<<offset<<'\n';
  }
  ans = max({ans, L[N], R[1]});
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define f first
#define s second
#define MAXN 200100
ll OFF;

ll L[MAXN];
ll R[MAXN];
ll sum_left[MAXN];
ll sum_right[MAXN];
ll tri_left[MAXN];
ll tri_right[MAXN];
ll len, N;
ll ans = 0;

ll range_left(ll x, ll y) {
  return sum_left[y] - sum_left[x - 1];
  // cout<<x<< ' '<<y<<' '<<tri_left[y] - tri_left[x-1] - (x-1) * (sum_left[y] -
  // sum_left[x-1])<<'\n'; return tri_left[y] - tri_left[x-1] - (x-1) *
  // (sum_left[y] - sum_left[x-1]);
}

ll range_right(ll x, ll y) {
  return sum_right[x] - sum_right[y + 1];
  // cout<<x<< ' '<<y<<' '<<tri_right[x] - tri_right[y+1] - (N-x+1) *
  // (sum_right[x] - sum_right[y-1])<<'\n'; return tri_right[x] - tri_right[y+1]
  // - (N-x+1) * (sum_right[x] - sum_right[y-1]);
}

int main() {
  cin >> len >> N;
  for (ll i = 1; i <= N; ++i) {
    cin >> L[i];
    R[i] = len - L[i];
    tri_left[i] = tri_left[i - 1] + L[i] * i;
    sum_left[i] = sum_left[i - 1] + L[i];
  }
  for (ll i = N; i > 0; --i) {
    sum_right[i] = sum_right[i + 1] + R[i];
  }
  // for (ll i=1;i<=N;++i)cout<<tri_left[i]<< ' ';cout<<'\n';
  // for (ll i=1;i<=N;++i)cout<<sum_left[i]<< ' ';cout<<'\n';
  // Firstly consider if we ending on the left
  for (ll lend = 1; lend < N; ++lend) {
    ll lstart = 1;
    ll rstart = lend + 1;
    ll rend = N;
    ll offset = 0;
    ll dif =
        (rend - rstart) - (lend - lstart) + 1; // -1 if we ending on the right
    if (dif > 0) {
      // Go to right first
      rend -= dif;
      offset = R[rend + 1];
    } else if (dif < 0) {
      lstart -= dif;
      offset = L[lstart - 1];
    }
    ll tot = 0;
    // First bit is the left
    tot = tot + range_left(lstart, lend) * 2 - (L[lend]);
    // cout<<tot<<'\n';
    tot = tot + range_right(rstart, rend) * 2;
    // cout<<tot<<'\n';
    if (dif > 0)
      tot = tot + offset * 2;
    ans = max(ans, tot);
    // cout<<tot<<'\n';

    // cout<<"Left "<<lstart<<' '<<lend<<'\n';
    // cout<<"Right "<<rstart<<' '<<rend<<'\n';
    // cout<<"Offset "<<offset<<'\n';
  }
  // return 0;
  // Firstly consider if we ending on the right
  for (ll lend = 1; lend < N; ++lend) {
    ll lstart = 1;
    ll rstart = lend + 1;
    ll rend = N;
    ll offset = 0;
    ll dif =
        (rend - rstart) - (lend - lstart) - 1; // -1 if we ending on the right
    // cout<<"Dif "<<dif<<'\n';
    if (dif > 0) {
      // Go to right first
      rend -= dif;
      offset = R[rend + 1];
    } else if (dif < 0) {
      lstart -= dif;
      offset = L[lstart - 1];
    }
    ll tot = 0;
    // First bit is the left
    tot = tot + range_left(lstart, lend) * 2;
    tot = tot + range_right(rstart, rend) * 2 - R[rstart];
    if (dif < 0)
      tot = tot + offset * 2;

    ans = max(ans, tot);
    // cout<<tot<<'\n';

    // cout<<"Left "<<lstart<<' '<<lend<<'\n';
    // cout<<"Right "<<rstart<<' '<<rend<<'\n';
    // cout<<"Offset "<<offset<<'\n';
  }
  ans = max({ans, L[N], R[1]});
  cout << ans;
}