Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize "03"
#pragma GCC target("sse4")

#define ll long long int
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define FRE                                                                    \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define f(i, n) for (ll i = 0; i < n; i++)
#define fa(i, a, n)                                                            \
  for (ll i = a; a < n ? i < n : i >= n; a < n ? i += 1 : i -= 1)
#define loop(i, a, n) for (ll i = a; i <= n; i++)
#define loopb(i, a, n) for (ll i = a; i >= n; i--)
#define pb push_back
#define pf push_front
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define setmem(x, k) memset(x, k, sizeof(x))
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define PI 3.1415926535897932384626
#define MOD 1000000007
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef long double ld;
// const ll N = 1e6+5;
// ll a[N], b[N], dp[N], in[N], out[N], level[N], vis[N];
// vl g[N];
ll n, m;

ll dp[100005];
vector<ll> a;

ll fun(vector<ll> a, ll pos) {
  if (pos == n - 1) {
    return 0;
  }
  if (pos >= n) {
    return INT_MAX;
  }
  if (dp[pos] != -1)
    return dp[pos];
  return dp[pos] = min(abs(a[pos] - a[pos + 1]) + fun(a, pos + 1),
                       abs(a[pos] - a[pos + 2]) + fun(a, pos + 2));
}

void solve() {
  // ll k, x, y, p, q, r;
  cin >> n;
  a.resize(n);
  f(i, n) cin >> a[i];
  setmem(dp, -1);
  cout << fun(a, 0);
}

int32_t main() {
  IOS ll T;
  // cin >> T;
  // while(T--)
  solve();
  cerr << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << "ms"
       << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize "03"
#pragma GCC target("sse4")

#define ll long long int
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define FRE                                                                    \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define f(i, n) for (ll i = 0; i < n; i++)
#define fa(i, a, n)                                                            \
  for (ll i = a; a < n ? i < n : i >= n; a < n ? i += 1 : i -= 1)
#define loop(i, a, n) for (ll i = a; i <= n; i++)
#define loopb(i, a, n) for (ll i = a; i >= n; i--)
#define pb push_back
#define pf push_front
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define setmem(x, k) memset(x, k, sizeof(x))
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define PI 3.1415926535897932384626
#define MOD 1000000007
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef long double ld;
// const ll N = 1e6+5;
// ll a[N], b[N], dp[N], in[N], out[N], level[N], vis[N];
// vl g[N];
ll n, m;

ll dp[100005];
vector<ll> a;

ll fun(vector<ll> &a, ll pos) {
  if (pos == n - 1) {
    return 0;
  }
  if (pos >= n) {
    return INT_MAX;
  }
  if (dp[pos] != -1)
    return dp[pos];
  return dp[pos] = min(abs(a[pos] - a[pos + 1]) + fun(a, pos + 1),
                       abs(a[pos] - a[pos + 2]) + fun(a, pos + 2));
}

void solve() {
  // ll k, x, y, p, q, r;
  cin >> n;
  a.resize(n);
  f(i, n) cin >> a[i];
  setmem(dp, -1);
  cout << fun(a, 0);
}

int32_t main() {
  IOS ll T;
  // cin >> T;
  // while(T--)
  solve();
  cerr << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << "ms"
       << '\n';
  return 0;
}