Problem p03160 (C++)

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

typedef long long ll;
typedef double dd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int mx = 2e3 + 11;
const double PI = acos(-1);
#define MOD 1000000007

#define nl '\n'
#define pb push_back
#define F first
#define S second
#define I insert
#define mp make_pair

#define sz(x) x.size()
#define sa(v) sort(v.begin(), v.end())
#define sd(v) sort(v.begin(), v.end(), greater<>())
#define rev(s) reverse(s.begin(), s.end())
#define tt                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define lo(i, a, b) for (ll i = a; i < b; i++)
#define be(a) (a).begin(), (a).end()
#define rbe(a) (a).rbegin(), (a).rend()

#define sqr(a) ((a) * (a))
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * (b / gcd(a, b)))
#define mem(a, b) memset(a, b, sizeof(a))

#define yes cout << "YES" << nl;
#define no cout << "NO" << nl;

#define su                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define file                                                                   \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define frac                                                                   \
  cout.unsetf(ios::floatfield);                                                \
  cout.precision(10);                                                          \
  cout.setf(ios::fixed, ios::floatfield);

#define dbg(args...)                                                           \
  do {                                                                         \
    cerr << #args << " : ";                                                    \
    faltu(args);                                                               \
  } while (0)
void faltu() { cerr << endl; }
template <typename T, typename... hello>
void faltu(T arg, const hello &...rest) {
  cerr << arg << ' ';
  faltu(rest...);
}

int dp[mx], n, a[mx];

int solve(int i) {
  if (i >= n)
    return 0;
  if (dp[i] != -1)
    return dp[i];

  int ret1 = INT_MAX, ret2 = INT_MAX;

  if (i + 1 <= n)
    ret1 = abs(a[i] - a[i + 1]) + solve(i + 1);

  if (i + 2 <= n)
    ret2 = abs(a[i] - a[i + 2]) + solve(i + 2);
  /// dbg(i);
  dp[i] = min(ret1, ret2);

  return dp[i];
}

int main() {
  su;

  mem(dp, -1);

  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  cout << solve(1) << nl;

  return 0;
}


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

typedef long long ll;
typedef double dd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int mx = 1e5 + 11;
const double PI = acos(-1);
#define MOD 1000000007

#define nl '\n'
#define pb push_back
#define F first
#define S second
#define I insert
#define mp make_pair

#define sz(x) x.size()
#define sa(v) sort(v.begin(), v.end())
#define sd(v) sort(v.begin(), v.end(), greater<>())
#define rev(s) reverse(s.begin(), s.end())
#define tt                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define lo(i, a, b) for (ll i = a; i < b; i++)
#define be(a) (a).begin(), (a).end()
#define rbe(a) (a).rbegin(), (a).rend()

#define sqr(a) ((a) * (a))
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * (b / gcd(a, b)))
#define mem(a, b) memset(a, b, sizeof(a))

#define yes cout << "YES" << nl;
#define no cout << "NO" << nl;

#define su                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define file                                                                   \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define frac                                                                   \
  cout.unsetf(ios::floatfield);                                                \
  cout.precision(10);                                                          \
  cout.setf(ios::fixed, ios::floatfield);

#define dbg(args...)                                                           \
  do {                                                                         \
    cerr << #args << " : ";                                                    \
    faltu(args);                                                               \
  } while (0)
void faltu() { cerr << endl; }
template <typename T, typename... hello>
void faltu(T arg, const hello &...rest) {
  cerr << arg << ' ';
  faltu(rest...);
}

int dp[mx], n, a[mx];

int solve(int i) {
  if (i >= n)
    return 0;
  if (dp[i] != -1)
    return dp[i];

  int ret1 = INT_MAX, ret2 = INT_MAX;

  if (i + 1 <= n)
    ret1 = abs(a[i] - a[i + 1]) + solve(i + 1);

  if (i + 2 <= n)
    ret2 = abs(a[i] - a[i + 2]) + solve(i + 2);
  /// dbg(i);
  dp[i] = min(ret1, ret2);

  return dp[i];
}

int main() {
  su;

  mem(dp, -1);

  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  cout << solve(1) << nl;

  return 0;
}