Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define emb emplace_back
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define ll long long
#define N 999999
#define f first
#define em emplace
#define s second
#define lint long long int
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<lint> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<string> vs;

#define fori(n) for (int i = 0; i < int(n); i++)

#define cins(s)                                                                \
  string s;                                                                    \
  cin >> s;
#define cini(i)                                                                \
  lint i;                                                                      \
  cin >> i;
#define cind(d)                                                                \
  double d;                                                                    \
  cin >> d;
#define cinai(a, n)                                                            \
  vi a(n);                                                                     \
  fori(n) { cin >> a[i]; }
#define cinas(s, n)                                                            \
  vs s(n);                                                                     \
  fori(n) { cin >> s[i]; }
#define cinad(a, n)                                                            \
  vd a(n);                                                                     \
  fori(n) { cin >> a[i]; }
#define z 1000000007
int arr[N];
vector<int> v;

int solve(int l, int r) {

  if (arr[l] != -1)
    return arr[l];
  else if (l == r - 1) {
    arr[l] = abs(v[l] - v[r]);
    return arr[l];
  } else if (l == r) {
    arr[l] = 0;
    return arr[l];
  } else if (l > r)
    return 100000;

  return arr[l] = min(

             abs(v[l] - v[l + 1]) + solve(l + 1, r),

             abs(v[l + 2] - v[l]) + solve(l + 2, r)

         );
}

int main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("hex.txt", "w", stdout);
#endif

  memset(arr, -1, sizeof(arr));
  cini(n);
  fori(n) {
    cini(a);
    v.pb(a);
  }

  int min = solve(0, n - 1);

  cout << min << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define emb emplace_back
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define ll long long
#define N 999999
#define f first
#define em emplace
#define s second
#define lint long long int
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<lint> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<string> vs;

#define fori(n) for (int i = 0; i < int(n); i++)

#define cins(s)                                                                \
  string s;                                                                    \
  cin >> s;
#define cini(i)                                                                \
  lint i;                                                                      \
  cin >> i;
#define cind(d)                                                                \
  double d;                                                                    \
  cin >> d;
#define cinai(a, n)                                                            \
  vi a(n);                                                                     \
  fori(n) { cin >> a[i]; }
#define cinas(s, n)                                                            \
  vs s(n);                                                                     \
  fori(n) { cin >> s[i]; }
#define cinad(a, n)                                                            \
  vd a(n);                                                                     \
  fori(n) { cin >> a[i]; }
#define z 1000000007
int arr[N];
vector<int> v;

int solve(int l, int r) {

  if (arr[l] != -1)
    return arr[l];
  else if (l == r - 1) {
    arr[l] = abs(v[l] - v[r]);
    return arr[l];
  } else if (l == r) {
    arr[l] = 0;
    return arr[l];
  } else if (l > r)
    return 100000;

  return arr[l] = min(

             abs(v[l] - v[l + 1]) + solve(l + 1, r),

             abs(v[l + 2] - v[l]) + solve(l + 2, r)

         );
}

int main() {
  fast;
  /* #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("hex.txt","w",stdout);
   #endif*/

  memset(arr, -1, sizeof(arr));
  cini(n);
  fori(n) {
    cini(a);
    v.pb(a);
  }

  int min = solve(0, n - 1);

  cout << min << endl;
}