Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)
#define pb push_back
#define digit(x) floor(log10(x)) + 1
#define mod 1000000007
#define L_MAX 1e18
using ll = long long;
using ld = long double;
using ii = pair<ll, ll>;
using iii = tuple<ll, ll, ll>;
using matrix = vector<vector<ll>>;
using arr = vector<ll>;
using vs = vector<string>;
using pv = vector<ii>;
using umap = unordered_map<ll, ll>;
#define in1(x) scanf("%lld", &x)
#define in2(x, y) scanf("%lld %lld", &x, &y)
#define in3(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " is " << x << endl;
void showArr(arr a) {
  for (auto i : a)
    cout << i << " ";
  cout << endl;
}
ll dx[] = {-1, 1, -1, 1, 0, 0, -1, 1};
ll dy[] = {-1, 1, 0, 0, -1, 1, 1, -1};
ll Solve(ll index, arr a, ll n, ll dp[]) {
  if (index >= n - 1)
    return 0;
  if (dp[index] != -1)
    return dp[index];
  ll left = L_MAX, right = L_MAX;
  if (index + 1 < n) {
    if (dp[index + 1] == -1)
      dp[index + 1] = Solve(index + 1, a, n, dp);
    left = dp[index + 1] + abs(a[index] - a[index + 1]);
  }
  if (index + 2 < n) {
    if (dp[index + 2] == -1)
      dp[index + 2] = Solve(index + 2, a, n, dp);
    right = dp[index + 2] + abs(a[index] - a[index + 2]);
  }
  return dp[index] = min(left, right);
}
int main() {
  fast;
  ll n;
  cin >> n;
  arr a;
  ll dp[100009];
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    a.pb(x);
  }
  memset(dp, -1, sizeof(dp));
  cout << Solve(0, a, n, dp) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)
#define pb push_back
#define digit(x) floor(log10(x)) + 1
#define mod 1000000007
#define L_MAX 1e18
using ll = long long;
using ld = long double;
using ii = pair<ll, ll>;
using iii = tuple<ll, ll, ll>;
using matrix = vector<vector<ll>>;
using arr = vector<ll>;
using vs = vector<string>;
using pv = vector<ii>;
using umap = unordered_map<ll, ll>;
#define in1(x) scanf("%lld", &x)
#define in2(x, y) scanf("%lld %lld", &x, &y)
#define in3(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " is " << x << endl;
void showArr(arr a) {
  for (auto i : a)
    cout << i << " ";
  cout << endl;
}
ll dx[] = {-1, 1, -1, 1, 0, 0, -1, 1};
ll dy[] = {-1, 1, 0, 0, -1, 1, 1, -1};
ll Solve(ll index, arr a, ll n, ll dp[]) {
  if (index >= n - 1)
    return 0;
  if (dp[index] != -1)
    return dp[index];
  ll left = L_MAX, right = L_MAX;
  if (index + 1 < n) {
    if (dp[index + 1] == -1)
      dp[index + 1] = Solve(index + 1, a, n, dp);
    left = dp[index + 1] + abs(a[index] - a[index + 1]);
  }
  if (index + 2 < n) {
    if (dp[index + 2] == -1)
      dp[index + 2] = Solve(index + 2, a, n, dp);
    right = dp[index + 2] + abs(a[index] - a[index + 2]);
  }
  return dp[index] = min(left, right);
}
int main() {
  fast;
  ll n;
  cin >> n;
  arr a;
  ll dp[100009];
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    a.pb(x);
  }
  // memset(dp,-1,sizeof(dp));
  // cout<<Solve(0,a,n,dp)<<endl;
  dp[n - 1] = 0;
  dp[n - 2] = abs(a[n - 1] - a[n - 2]);
  for (int i = n - 3; i >= 0; i--) {
    dp[i] =
        min(dp[i + 2] + abs(a[i] - a[i + 2]), abs(a[i] - a[i + 1]) + dp[i + 1]);
  }
  cout << dp[0] << endl;
  return 0;
}