Problem p03160 (C++)

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

#define int long long int
#define ld long double
#define mod 1000000007
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define forn(i, s, e) for (int i = s; i <= e; i++)
#define fornd(i, s, e) for (int i = s; i >= e; i--)
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define print(x)                                                               \
  for (auto el : x)                                                            \
    cout << el << " ";                                                         \
  cout << "\n"
#define PSET(x, y) fixed << setprecision(y) << x
// #define bits(x) __builtin_popcount(x)
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
void fileio() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

template <typename T> void swap(T &a, T &b) {
  T temp = a;
  a = b;
  b = temp;
}

const int N = 1000001;

vector<int> h;
vector<int> dp;

int rec(int i) {

  if (i == 0)
    return 0;

  if (i < 0)
    return INT_MAX;

  if (dp[i] != -1)
    return dp[i];

  return dp[i] =
             min((i - 1) >= 0 ? rec(i - 1) + abs(h[i] - h[i - 1]) : INT_MAX,
                 (i - 2) >= 0 ? rec(i - 2) + abs(h[i] - h[i - 2]) : INT_MAX);
}

void solve() {
  int n;
  cin >> n;

  h = vector<int>(n);
  dp = vector<int>(n, -1);
  forn(i, 0, n - 1) cin >> h[i];

  cout << rec(n - 1) << endl;
}

int32_t main() {
  fileio();
  fast;
  // int t;
  // cin>>t;
  // for(int y=1;y<=t;y++)
  {
    // cout<<"Case #"<<y<<": ";
    solve();
  }
  return 0;
}

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

#define int long long int
#define ld long double
#define mod 1000000007
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define forn(i, s, e) for (int i = s; i <= e; i++)
#define fornd(i, s, e) for (int i = s; i >= e; i--)
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define print(x)                                                               \
  for (auto el : x)                                                            \
    cout << el << " ";                                                         \
  cout << "\n"
#define PSET(x, y) fixed << setprecision(y) << x
// #define bits(x) __builtin_popcount(x)
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
void fileio() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

template <typename T> void swap(T &a, T &b) {
  T temp = a;
  a = b;
  b = temp;
}

const int N = 1000001;

vector<int> h;
vector<int> dp;

int rec(int i) {

  if (i == 0)
    return 0;

  if (i < 0)
    return INT_MAX;

  if (dp[i] != -1)
    return dp[i];

  return dp[i] =
             min((i - 1) >= 0 ? rec(i - 1) + abs(h[i] - h[i - 1]) : INT_MAX,
                 (i - 2) >= 0 ? rec(i - 2) + abs(h[i] - h[i - 2]) : INT_MAX);
}

void solve() {
  int n;
  cin >> n;

  h = vector<int>(n);
  dp = vector<int>(n, -1);
  forn(i, 0, n - 1) cin >> h[i];

  cout << rec(n - 1) << endl;
}

int32_t main() {

  fast;
  // int t;
  // cin>>t;
  // for(int y=1;y<=t;y++)
  {
    // cout<<"Case #"<<y<<": ";
    solve();
  }
  return 0;
}