Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define endl "\n"
#define mod 1000000007
#define INF 1e18
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void rwf() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int xpowery(int x, int y) {
  int ans = 1;
  while (y) {
    if (y & 1)
      ans = (ans * x) % mod;
    x = (x * x) % mod;
    y = y / 2;
  }
  return ans;
}

const int N = 1e5 + 5;
int h[N], n;

int cache[N];

int solve(int m) {
  if (m > n)
    return cache[m] = 1e9;

  if (m == n)
    return cache[m] = 0;

  if (cache[m] != -1)
    return cache[m];

  return cache[m] = min(abs(h[m] - h[m + 1]) + solve(m + 1),
                        abs(h[m] - h[m + 2]) + solve(m + 2));
}

int32_t main() {
  IOS;
  rwf();

  cin >> n;

  memset(cache, -1, sizeof(cache));

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

  int ans = solve(1);

  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define endl "\n"
#define mod 1000000007
#define INF 1e18
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void rwf() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int xpowery(int x, int y) {
  int ans = 1;
  while (y) {
    if (y & 1)
      ans = (ans * x) % mod;
    x = (x * x) % mod;
    y = y / 2;
  }
  return ans;
}

const int N = 1e5 + 5;
int h[N], n;

int cache[N];

int solve(int m) {
  if (m > n)
    return cache[m] = 1e9;

  if (m == n)
    return cache[m] = 0;

  if (cache[m] != -1)
    return cache[m];

  return cache[m] = min(abs(h[m] - h[m + 1]) + solve(m + 1),
                        abs(h[m] - h[m + 2]) + solve(m + 2));
}

int32_t main() {
  IOS;
  // rwf();

  cin >> n;

  memset(cache, -1, sizeof(cache));

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

  int ans = solve(1);

  cout << ans;

  return 0;
}