Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<vector<int>> mati;
typedef vector<vector<double>> matd;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)

template <typename T> void vec_print(vector<T> VEC) {
  rep(i, 0, VEC.size()) { cout << VEC[i] << " "; }
  cout << "\n";
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    rep(i, 0, MAT.size()){rep(j, 0, MAT[i].size()){cout << MAT[i][j] << " ";
}
cout << "\n";
}
}
;

class HOGE {
public:
  ll index;
  ll value;
  HOGE(ll index, ll value) {
    this->index = index;
    this->value = value;
  };
  ~HOGE(void) { return; };

  void print(void) {
    cout << "index : " << index << ", value : " << value << "\n";
    return;
  };

  bool operator==(const HOGE &obj) { return (this->value == obj.value); };
  bool operator<(const HOGE &obj) { return (this->value < obj.value); };
  bool operator>(const HOGE &obj) { return (this->value > obj.value); };
};

constexpr int INF = (1 << 30);
constexpr ll INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;

ll min_search(int pos, const int last, const vector<ll> &h, vector<ll> dp) {
  if (pos >= last)
    return 0;
  if (dp[pos] >= 0)
    return dp[pos];
  if (last - pos == 1 || last - pos == 2) {
    dp[pos] = abs(h[pos] - h[last]);
  } else {
    ll dp2 = min_search(pos + 2, last, h, dp);
    ll dp1 = min_search(pos + 1, last, h, dp);
    dp[pos] =
        min(abs(h[pos] - h[pos + 1]) + dp1, abs(h[pos] - h[pos + 2]) + dp2);
  }
  return dp[pos];
}

int main() {
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after
              // std::cin)
  ios::sync_with_stdio(
      false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  ll N;
  cin >> N;
  vector<ll> h(N);
  rep(i, 0, N) { cin >> h[i]; }

  vector<ll> dp(N, -1);

  ll ans = min_search(0, N - 1, h, dp);

  cout << ans << "\n";

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<vector<int>> mati;
typedef vector<vector<double>> matd;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)

template <typename T> void vec_print(vector<T> VEC) {
  rep(i, 0, VEC.size()) { cout << VEC[i] << " "; }
  cout << "\n";
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    rep(i, 0, MAT.size()){rep(j, 0, MAT[i].size()){cout << MAT[i][j] << " ";
}
cout << "\n";
}
}
;

class HOGE {
public:
  ll index;
  ll value;
  HOGE(ll index, ll value) {
    this->index = index;
    this->value = value;
  };
  ~HOGE(void) { return; };

  void print(void) {
    cout << "index : " << index << ", value : " << value << "\n";
    return;
  };

  bool operator==(const HOGE &obj) { return (this->value == obj.value); };
  bool operator<(const HOGE &obj) { return (this->value < obj.value); };
  bool operator>(const HOGE &obj) { return (this->value > obj.value); };
};

constexpr int INF = (1 << 30);
constexpr ll INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;

ll min_search(int pos, const int last, const vector<ll> &h, vector<ll> &dp) {
  if (pos >= last)
    return 0;
  if (dp[pos] >= 0)
    return dp[pos];
  if (last - pos == 1 || last - pos == 2) {
    dp[pos] = abs(h[pos] - h[last]);
  } else {
    ll dp2 = min_search(pos + 2, last, h, dp);
    ll dp1 = min_search(pos + 1, last, h, dp);
    dp[pos] =
        min(abs(h[pos] - h[pos + 1]) + dp1, abs(h[pos] - h[pos + 2]) + dp2);
  }
  return dp[pos];
}

int main() {
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after
              // std::cin)
  ios::sync_with_stdio(
      false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  ll N;
  cin >> N;
  vector<ll> h(N);
  rep(i, 0, N) { cin >> h[i]; }

  vector<ll> dp(N, -1);

  ll ans = min_search(0, N - 1, h, dp);

  cout << ans << "\n";

  return 0;
}