Problem p03062 (C++)

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

#define N 1000
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 1000000007
#define all(cont) cont.begin(), cont.end()
#define sortcmp(n, cmpfunc) sort(all(n), cmpfunc)
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)

ll gcd(ll a, ll b);
ll lcm(ll a, ll b);
ll powm(ll a, ll b);
ll divm(ll a, ll b);
ll multm(ll a, ll b);
ll subm(ll a, ll b);
ll addm(ll a, ll b);

int main() {
  fastio;
  // #ifndef ONLINE_JUDGE
  //    	freopen("input.in", "r", stdin);
  //    	freopen("output.out", "w", stdout);
  // #endif

  int n;
  ll a[N], dp[N][2];
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> a[i];

  dp[0][0] = a[0] + a[1];
  dp[0][1] = (a[0] + a[1]) * -1;
  for (int i = 1; i < n - 1; i++) {
    for (int j = 0; j < 2; j++) {
      if (j == 0)
        dp[i][j] = max(dp[i - 1][0], dp[i - 1][1]) + a[i + 1];
      else
        dp[i][j] =
            max(dp[i - 1][0] - 2 * a[i], dp[i - 1][1] + 2 * a[i]) - a[i + 1];
    }
  }

  cout << max(dp[n - 2][0], dp[n - 2][1]) << endl;

  return 0;
}

// template function
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll powm(ll a, ll b) {
  ll x = 1, y = a;
  while (b > 0) {
    if (b % 2)
      x = (x * y) % mod;
    y = (y * y) % mod;
    b /= 2;
  }
  return x % mod;
}

ll multm(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }

ll divm(ll a, ll b) { return (a * powm(b, mod - 2)); }

ll subm(ll a, ll b) {
  a -= b;
  if (a < 0)
    a += mod;
  return a;
}

ll addm(ll a, ll b) {
  a += b;
  if (a >= mod)
    a -= mod;
  return a;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define N 100012
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 1000000007
#define all(cont) cont.begin(), cont.end()
#define sortcmp(n, cmpfunc) sort(all(n), cmpfunc)
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)

ll gcd(ll a, ll b);
ll lcm(ll a, ll b);
ll powm(ll a, ll b);
ll divm(ll a, ll b);
ll multm(ll a, ll b);
ll subm(ll a, ll b);
ll addm(ll a, ll b);

int main() {
  fastio;
  // #ifndef ONLINE_JUDGE
  //    	freopen("input.in", "r", stdin);
  //    	freopen("output.out", "w", stdout);
  // #endif

  int n;
  ll a[N], dp[N][2];
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> a[i];

  dp[0][0] = a[0] + a[1];
  dp[0][1] = (a[0] + a[1]) * -1;
  for (int i = 1; i < n - 1; i++) {
    for (int j = 0; j < 2; j++) {
      if (j == 0)
        dp[i][j] = max(dp[i - 1][0], dp[i - 1][1]) + a[i + 1];
      else
        dp[i][j] =
            max(dp[i - 1][0] - 2 * a[i], dp[i - 1][1] + 2 * a[i]) - a[i + 1];
    }
  }

  cout << max(dp[n - 2][0], dp[n - 2][1]) << endl;

  return 0;
}

// template function
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll powm(ll a, ll b) {
  ll x = 1, y = a;
  while (b > 0) {
    if (b % 2)
      x = (x * y) % mod;
    y = (y * y) % mod;
    b /= 2;
  }
  return x % mod;
}

ll multm(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }

ll divm(ll a, ll b) { return (a * powm(b, mod - 2)); }

ll subm(ll a, ll b) {
  a -= b;
  if (a < 0)
    a += mod;
  return a;
}

ll addm(ll a, ll b) {
  a += b;
  if (a >= mod)
    a -= mod;
  return a;
}