Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define test()                                                                 \
  ull t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define mkp make_pair
#define nl cout << endl
#define MOD 1000000007
#define loop(i, start, end) for (int i = start; i < end; i++)
#define N 100001
#define oa(a, n)                                                               \
  for (int i = 0; i < n; i++)                                                  \
    cout << a[i] << " ";                                                       \
  nl;
#define ov(a)                                                                  \
  for (int i = 0; i < a.size(); i++)                                           \
    cout << a[i] << endl;
#define pi M_PI

const int INF = 1e9 + 5;
int main() {
  fastio();
#ifndef ONLINE_JUDGE
  auto freopenvar = freopen("in.txt", "r", stdin);
  auto freeopenvar2 = freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  int h[n];
  loop(i, 0, n) { cin >> h[i]; }
  vector<int> dp(n, INF);
  dp[0] = 0;
  loop(i, 0, n) {
    loop(j, i + 1, i + 3) {
      if (j < n)
        dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
    }
  }
  cout << dp[n - 1] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define test()                                                                 \
  ull t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define mkp make_pair
#define nl cout << endl
#define MOD 1000000007
#define loop(i, start, end) for (int i = start; i < end; i++)
#define N 100001
#define oa(a, n)                                                               \
  for (int i = 0; i < n; i++)                                                  \
    cout << a[i] << " ";                                                       \
  nl;
#define ov(a)                                                                  \
  for (int i = 0; i < a.size(); i++)                                           \
    cout << a[i] << endl;
#define pi M_PI

const int INF = 1e9 + 5;
int main() {
  fastio();
  int n;
  cin >> n;
  int h[n];
  loop(i, 0, n) { cin >> h[i]; }
  vector<int> dp(n, INF);
  dp[0] = 0;
  loop(i, 0, n) {
    loop(j, i + 1, i + 3) {
      if (j < n)
        dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
    }
  }
  cout << dp[n - 1] << endl;

  return 0;
}