Problem p03160 (C++)

Buggy submission (status: Runtime Error):
/*
                        ███╗   ███╗ █████╗ ███████╗███╗   ███╗ ██████╗ ██╗
   ██╗██████╗ ███████╗ █████╗ ███╗   ███╗██████╗ ██╗   ██╗ ████╗
   ████║██╔══██╗╚════██║████╗ ████║██╔═══██╗██║ ██║██╔══██╗╚════██║██╔══██╗████╗
   ████║██╔══██╗╚██╗ ██╔╝ ██╔████╔██║███████║    ██╔╝██╔████╔██║██║   ██║██║
   ██║██║  ██║    ██╔╝███████║██╔████╔██║██║  ██║ ╚████╔╝ ██║╚██╔╝██║██╔══██║
   ██╔╝ ██║╚██╔╝██║██║   ██║██║   ██║██║  ██║   ██╔╝ ██╔══██║██║╚██╔╝██║██║  ██║
   ╚██╔╝ ██║ ╚═╝ ██║██║  ██║   ██║  ██║ ╚═╝ ██║╚██████╔╝╚██████╔╝██████╔╝██╗██║
   ██║  ██║██║ ╚═╝ ██║██████╔╝   ██║ ╚═╝     ╚═╝╚═╝  ╚═╝   ╚═╝  ╚═╝     ╚═╝
   ╚═════╝  ╚═════╝ ╚═════╝ ╚═╝╚═╝  ╚═╝  ╚═╝╚═╝     ╚═╝╚═════╝    ╚═╝

                        “Nobody but you have to believe in your dreams to make
   them a reality.” ― Germany Kent
*/
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define Ma7moud_7amdy                                                          \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define Open_Sesame Open()
#define pb push_back
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define clr(arr, x) memset(arr, x, sizeof arr)
#define endl "\n"
#define Accepted 0
#define watch(x) cout << #x << " = " << x << endl;
#define un_map unordered_map
#define RT(x) return cout << (x), 0;
#define sc(a) scanf("%d", &a)
#define scll(x) scanf("%I64d", &x)
#define scan(s) scanf("%[^\n]%*c", &s)
#define pr(s) printf("%s", s.c_str()); // print string
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double EPS = -1e-2;
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
const ll mod = 1000000007;
const int Mx = INT_MAX;
const int Mn = INT_MIN;
const ll MX = LLONG_MAX;
const ll MN = LLONG_MIN;
const int N = 1e5 + 1;
void Open() {
#ifndef ONLINE_JUDGE
  freopen("Input.txt", "r", stdin);
  freopen("Output.txt", "w", stdout);
#endif
}
int n, k;
int mem[N];
vector<int> v;
int solve(int i) {
  int &ret = mem[i];
  if (~ret)
    return ret;
  ret = Mx;
  if (i + 1 < n)
    ret = min(ret, solve(i + 1) + abs(v[i] - v[i + 1]));
  if (i + 2 < n)
    ret = min(ret, solve(i + 2) + abs(v[i] - v[i + 2]));
  ret = ret == Mx ? 0 : ret;
  return ret;
}
int main() {
  Ma7moud_7amdy;
  Open_Sesame;
  clr(mem, -1);
  cin >> n;
  v = vector<int>(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  cout << solve(0);
  return Accepted;
}

Fixed submission:
/*
                        ███╗   ███╗ █████╗ ███████╗███╗   ███╗ ██████╗ ██╗
   ██╗██████╗ ███████╗ █████╗ ███╗   ███╗██████╗ ██╗   ██╗ ████╗
   ████║██╔══██╗╚════██║████╗ ████║██╔═══██╗██║ ██║██╔══██╗╚════██║██╔══██╗████╗
   ████║██╔══██╗╚██╗ ██╔╝ ██╔████╔██║███████║    ██╔╝██╔████╔██║██║   ██║██║
   ██║██║  ██║    ██╔╝███████║██╔████╔██║██║  ██║ ╚████╔╝ ██║╚██╔╝██║██╔══██║
   ██╔╝ ██║╚██╔╝██║██║   ██║██║   ██║██║  ██║   ██╔╝ ██╔══██║██║╚██╔╝██║██║  ██║
   ╚██╔╝ ██║ ╚═╝ ██║██║  ██║   ██║  ██║ ╚═╝ ██║╚██████╔╝╚██████╔╝██████╔╝██╗██║
   ██║  ██║██║ ╚═╝ ██║██████╔╝   ██║ ╚═╝     ╚═╝╚═╝  ╚═╝   ╚═╝  ╚═╝     ╚═╝
   ╚═════╝  ╚═════╝ ╚═════╝ ╚═╝╚═╝  ╚═╝  ╚═╝╚═╝     ╚═╝╚═════╝    ╚═╝

                        “Nobody but you have to believe in your dreams to make
   them a reality.” ― Germany Kent
*/
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define Ma7moud_7amdy                                                          \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define Open_Sesame Open()
#define pb push_back
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define clr(arr, x) memset(arr, x, sizeof arr)
#define endl "\n"
#define Accepted 0
#define watch(x) cout << #x << " = " << x << endl;
#define un_map unordered_map
#define RT(x) return cout << (x), 0;
#define sc(a) scanf("%d", &a)
#define scll(x) scanf("%I64d", &x)
#define scan(s) scanf("%[^\n]%*c", &s)
#define pr(s) printf("%s", s.c_str()); // print string
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double EPS = -1e-2;
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
const ll mod = 1000000007;
const int Mx = INT_MAX;
const int Mn = INT_MIN;
const ll MX = LLONG_MAX;
const ll MN = LLONG_MIN;
const int N = 1e5 + 1;
void Open() {
#ifndef ONLINE_JUDGE
  freopen("Input.txt", "r", stdin);
  freopen("Output.txt", "w", stdout);
#endif
}
int n, k;
int mem[N];
vector<int> v;
int solve(int i) {
  int &ret = mem[i];
  if (~ret)
    return ret;
  ret = Mx;
  if (i + 1 < n)
    ret = min(ret, solve(i + 1) + abs(v[i] - v[i + 1]));
  if (i + 2 < n)
    ret = min(ret, solve(i + 2) + abs(v[i] - v[i + 2]));
  ret = ret == Mx ? 0 : ret;
  return ret;
}
int main() {
  Ma7moud_7amdy;
  // Open_Sesame;
  clr(mem, -1);
  cin >> n;
  v = vector<int>(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  cout << solve(0);
  return Accepted;
}