Problem p03160 (C++)

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 998244353
#define endl "\n"
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}
// #DEBUG
void __print(int x) { cout << x; }
void __print(char x) { cout << '\'' << x << '\''; }
void __print(const char *x) { cout << '\"' << x << '\"'; }
void __print(const string &x) { cout << '\"' << x << '\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x)
    cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cout << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cout << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif
// ###################################################################

void fast() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
vector<int> stones;
int ans1 = INT_MAX;
int ans2 = INT_MAX;
int n;
int dp[100005];
int solve(int start) {
  if (start == n - 2) {
    return abs(stones[n - 1] - stones[n - 2]);
  }
  if (start == n - 1) {
    return 0;
  }
  if (dp[start] != -1) {
    return dp[start];
  } else {
    dp[start + 1] = solve(start + 1);
    dp[start + 2] = solve(start + 2);
    ans1 = abs(stones[start] - stones[start + 1]) + solve(start + 1);
    ans2 = abs(stones[start] - stones[start + 2]) + solve(start + 2);
    dp[start] = min(ans1, ans2);
    return dp[start];
  }
}
int32_t main() {
  fast();
  cin >> n;
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    stones.push_back(a);
  }
  cout << solve(0);
}

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 998244353
#define endl "\n"
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}
// #DEBUG
void __print(int x) { cout << x; }
void __print(char x) { cout << '\'' << x << '\''; }
void __print(const char *x) { cout << '\"' << x << '\"'; }
void __print(const string &x) { cout << '\"' << x << '\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x)
    cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cout << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cout << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif
// ###################################################################

void fast() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // #ifndef ONLINE_JUDGE
  //   freopen("input.txt", "r", stdin);
  //   freopen("output.txt", "w", stdout);
  // #endif
}
vector<int> stones;
int ans1 = INT_MAX;
int ans2 = INT_MAX;
int n;
int dp[100005];
int solve(int start) {
  if (start == n - 2) {
    return abs(stones[n - 1] - stones[n - 2]);
  }
  if (start == n - 1) {
    return 0;
  }
  if (dp[start] != -1) {
    return dp[start];
  } else {
    dp[start + 1] = solve(start + 1);
    dp[start + 2] = solve(start + 2);
    ans1 = abs(stones[start] - stones[start + 1]) + solve(start + 1);
    ans2 = abs(stones[start] - stones[start + 2]) + solve(start + 2);
    dp[start] = min(ans1, ans2);
    return dp[start];
  }
}
int32_t main() {
  fast();
  cin >> n;
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    stones.push_back(a);
  }
  cout << solve(0);
}