Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define frst first
#define sec second
#define fast cin.tie(0), ios_base ::sync_with_stdio(0)
#define db double
#define mod 1000000007
void online_judge() {
#ifndef ONLINE_JUDGE
  freopen("Input.txt", "r", stdin);
  freopen("Output.txt", "w", stdout);
#endif
}

ll lpp(string s) {
  string rev = s;
  reverse(rev.begin(), rev.end());
  s = s + '#' + rev;
  ////cout << s << " ";
  vector<ll> z(s.size(), 0);
  ll l = 0, r = 0;
  for (int i = 1; i < s.size(); ++i) {
    if (i > r) {
      l = r = i;
      while (s[r] == s[r - l]) {
        r++;
      }
      z[i] = (r - l);
      r--;
    } else {
      if (z[i - l] + i <= r)
        z[i] = z[i - l];
      else {
        l = i;
        while (s[r] == s[r - l])
          r++;
        z[i] = (r - l);
        r--;
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < s.size(); ++i) {
    if (z[i] + i == s.size()) {
      ans = max(ans, z[i]);
    }
  }
  return ans;
}

string binary(ll n) {
  string s;
  while (n) {
    if ((n & 1))
      s += '1';
    else
      s += '0';
    n >>= 1;
  }
  reverse(s.begin(), s.end());
  return s;
}

bool pos(vector<ll> &v, ll val, ll painters) {
  ll cur = 0;
  ll k = 1;
  for (int i = 0; i < v.size(); ++i) {
    if (v[i] > val)
      return 0;
    if (cur + v[i] <= val)
      cur += v[i];
    else {
      k++;
      cur = v[i];
    }
  }
  cout << k << endl;
  return (k <= painters);
}

class node {
  node *next;
  node *random;
  ll val;
  node(ll x) {
    next = random = NULL;
    val = x;
  }
};

ll dp[101][101][1001];

ll cal(ll a[], ll cur, ll n, ll sum, ll c, ll tot) {
  if (n == 0) {
    /// cout << cur << " " << sum << " " << c << endl;
    if (cur == tot / 2)
      return abs((sum - c) - c);
    return 1e8;
  }
  if (dp[n][cur][c] != 1e8) {
    /// cout << dp[n][cur][c] << " ";
    return dp[n][cur][c];
  }
  /// cout << dp[n][cur][c] << " ";
  dp[n][cur][c] = cal(a, cur, n - 1, sum, c, tot);
  dp[n][cur][c] =
      min(dp[n][cur][c], cal(a, cur + 1, n - 1, sum, c + a[n - 1], tot));
  // cout << ans << endl;
  return dp[n][cur][c];
}

void go() {
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  ll dp[3];
  // memset(dp , 0, sizeof dp);
  for (int i = 0; i < 3; ++i)
    dp[i] = 1e8;
  dp[0] = 0;
  for (int i = 1; i < n; ++i) {
    dp[i % 3] =
        min(dp[(i - 1 + 3) % 3] + abs(a[i] - a[i - 1]),
            (i >= 2) ? dp[(i - 2 + 3) % 3] + abs(a[i - 2] - a[i]) : (ll)1e8);
  }
  cout << dp[(n - 1) % 3];
}

int main() {
  fast;
  online_judge();
  go();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define frst first
#define sec second
#define fast cin.tie(0), ios_base ::sync_with_stdio(0)
#define db double
#define mod 1000000007
void online_judge() {
#ifndef ONLINE_JUDGE
  freopen("Input.txt", "r", stdin);
  freopen("Output.txt", "w", stdout);
#endif
}

ll lpp(string s) {
  string rev = s;
  reverse(rev.begin(), rev.end());
  s = s + '#' + rev;
  ////cout << s << " ";
  vector<ll> z(s.size(), 0);
  ll l = 0, r = 0;
  for (int i = 1; i < s.size(); ++i) {
    if (i > r) {
      l = r = i;
      while (s[r] == s[r - l]) {
        r++;
      }
      z[i] = (r - l);
      r--;
    } else {
      if (z[i - l] + i <= r)
        z[i] = z[i - l];
      else {
        l = i;
        while (s[r] == s[r - l])
          r++;
        z[i] = (r - l);
        r--;
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < s.size(); ++i) {
    if (z[i] + i == s.size()) {
      ans = max(ans, z[i]);
    }
  }
  return ans;
}

string binary(ll n) {
  string s;
  while (n) {
    if ((n & 1))
      s += '1';
    else
      s += '0';
    n >>= 1;
  }
  reverse(s.begin(), s.end());
  return s;
}

bool pos(vector<ll> &v, ll val, ll painters) {
  ll cur = 0;
  ll k = 1;
  for (int i = 0; i < v.size(); ++i) {
    if (v[i] > val)
      return 0;
    if (cur + v[i] <= val)
      cur += v[i];
    else {
      k++;
      cur = v[i];
    }
  }
  cout << k << endl;
  return (k <= painters);
}

class node {
  node *next;
  node *random;
  ll val;
  node(ll x) {
    next = random = NULL;
    val = x;
  }
};

ll dp[101][101][1001];

ll cal(ll a[], ll cur, ll n, ll sum, ll c, ll tot) {
  if (n == 0) {
    /// cout << cur << " " << sum << " " << c << endl;
    if (cur == tot / 2)
      return abs((sum - c) - c);
    return 1e8;
  }
  if (dp[n][cur][c] != 1e8) {
    /// cout << dp[n][cur][c] << " ";
    return dp[n][cur][c];
  }
  /// cout << dp[n][cur][c] << " ";
  dp[n][cur][c] = cal(a, cur, n - 1, sum, c, tot);
  dp[n][cur][c] =
      min(dp[n][cur][c], cal(a, cur + 1, n - 1, sum, c + a[n - 1], tot));
  // cout << ans << endl;
  return dp[n][cur][c];
}

void go() {
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  ll dp[3];
  // memset(dp , 0, sizeof dp);
  for (int i = 0; i < 3; ++i)
    dp[i] = 1e8;
  dp[0] = 0;
  for (int i = 1; i < n; ++i) {
    dp[i % 3] =
        min(dp[(i - 1 + 3) % 3] + abs(a[i] - a[i - 1]),
            (i >= 2) ? dp[(i - 2 + 3) % 3] + abs(a[i - 2] - a[i]) : (ll)1e8);
  }
  cout << dp[(n - 1) % 3];
}

int main() {
  fast;
  // online_judge();
  go();
}