Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// by rko27
// One more....
//---------------------------------------------------------------------------------------------------------------------------------------------------

#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
#define ll long long
#define pb push_back
#define mp make_pair
#define fe first
#define se second
#define nl "\n"
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define god                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);

//----------------------------------------------------------------------------------------------------------------------------------------------------

int mod = 1000000000 + 7;
int modu = 998244353;

//----------------------------------------------------------------------------------------------------------------------------------------------------

ll power(ll x, ll y) {
  ll res = 1;
  x = x;
  while (y > 0) {
    if (y & 1)
      res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
ll powe(ll x, ll y) {
  x = x % mod, y = y % (mod - 1);
  ll ans = 1;
  while (y > 0) {
    if (y & 1) {
      ans = (1ll * x * ans) % mod;
    }
    y >>= 1;
    x = (1ll * x * x) % mod;
  }
  return ans;
}
void fun() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // 786
}
bool isPrime(int n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
ll ncr(ll n, ll r) {
  ll res = 1;
  if (r > n)
    return 0;
  if (r > n - r)
    r = n - r;
  for (ll i = 0; i < r; i++) {
    res *= (n - i);
    res /= (i + 1);
  }
  return res;
}

//-------------------------------------------------------------------------------------------------------------------------------------------------------

std::vector<pair<int, int>> v;

//--------------------------------------------------------------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------------------------------------------------------------
signed main() {
  god;
  // rko;
  fun();
  int tt = 1; // cin>>tt;
  while (tt--) {
    int n;
    cin >> n;
    int h[n + 1];
    h[0] = 0;
    for (int i = 1; i <= n; i++)
      cin >> h[i];
    int dp[n + 1];
    dp[0] = 0;
    dp[1] = 0;
    dp[2] = abs(h[1] - h[2]);
    for (int i = 3; i <= n; i++)
      dp[i] = min(dp[i - 2] + abs(h[i] - h[i - 2]),
                  dp[i - 1] + abs(h[i] - h[i - 1]));
    cout << dp[n];
  }
  return 0;
}

Fixed submission:
// by rko27
// One more....
//---------------------------------------------------------------------------------------------------------------------------------------------------

#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
#define ll long long
#define pb push_back
#define mp make_pair
#define fe first
#define se second
#define nl "\n"
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define god                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);

//----------------------------------------------------------------------------------------------------------------------------------------------------

int mod = 1000000000 + 7;
int modu = 998244353;

//----------------------------------------------------------------------------------------------------------------------------------------------------

ll power(ll x, ll y) {
  ll res = 1;
  x = x;
  while (y > 0) {
    if (y & 1)
      res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
ll powe(ll x, ll y) {
  x = x % mod, y = y % (mod - 1);
  ll ans = 1;
  while (y > 0) {
    if (y & 1) {
      ans = (1ll * x * ans) % mod;
    }
    y >>= 1;
    x = (1ll * x * x) % mod;
  }
  return ans;
}
void fun() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  // 786
}
bool isPrime(int n) {
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
ll ncr(ll n, ll r) {
  ll res = 1;
  if (r > n)
    return 0;
  if (r > n - r)
    r = n - r;
  for (ll i = 0; i < r; i++) {
    res *= (n - i);
    res /= (i + 1);
  }
  return res;
}

//-------------------------------------------------------------------------------------------------------------------------------------------------------

std::vector<pair<int, int>> v;

//--------------------------------------------------------------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------------------------------------------------------------
signed main() {
  god;
  // rko;
  fun();
  int tt = 1; // cin>>tt;
  while (tt--) {
    int n;
    cin >> n;
    int h[n + 1];
    h[0] = 0;
    for (int i = 1; i <= n; i++)
      cin >> h[i];
    int dp[n + 1];
    dp[0] = 0;
    dp[1] = 0;
    dp[2] = abs(h[1] - h[2]);
    for (int i = 3; i <= n; i++)
      dp[i] = min(dp[i - 2] + abs(h[i] - h[i - 2]),
                  dp[i - 1] + abs(h[i] - h[i - 1]));
    cout << dp[n];
  }
  return 0;
}