Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// https://cs.stackexchange.com/questions/84426/find-a-node-with-maximum-distance-from-given-node-in-a-tree
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp> // Including
// tree_order_statistics_node_update using namespace __gnu_pbds; #include
// <ext/rope> //header with rope using namespace __gnu_cxx; #define ordered_set
// tree<pii, null_type,less<pii>, rb_tree_tag,tree_order_statistics_node_update>
//  find_by_order tells index pr kon hai..
//  order_by_key  tells ki us element se kitne strictly smaller element hai...
#define mod 1000000007
#define INF 1000000000000000000
#define fr first
#define se second
#define ll long long
#define PI 3.1415926535
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Senky_Bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD                                                         \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
ll fact[1000007];
ll mdpower(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res % mod * a % mod) % mod;
    b /= 2;
    a = (a % mod * a % mod) % mod;
  }
  return res;
}
void factorial() {
  fact[0] = 1;
  for (int i = 1; i < 1000007; i++)
    fact[i] = (fact[i - 1] % mod * i % mod) % mod;
}
ll inverse(ll a, ll b) { return mdpower(a, b - 2); }
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b /= 2;
  }
  return res;
}
int main() {
  Senky_Bansal IIIT_ALLAHABAD

#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  ll h[n];
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  ll dp[n + 1];
  for (int i = 1; i <= n; i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    if (i + 1 < n)
      dp[i + 1] = min(dp[i + 1], dp[i] + abs(h[i + 1] - h[i]));
    if (i + 2 < n)
      dp[i + 2] = min(dp[i + 2], dp[i] + abs(h[i + 2] - h[i]));
  }
  cout << dp[n - 1] << endl;
}

Fixed submission:
// https://cs.stackexchange.com/questions/84426/find-a-node-with-maximum-distance-from-given-node-in-a-tree
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp> // Including
// tree_order_statistics_node_update using namespace __gnu_pbds; #include
// <ext/rope> //header with rope using namespace __gnu_cxx; #define ordered_set
// tree<pii, null_type,less<pii>, rb_tree_tag,tree_order_statistics_node_update>
//  find_by_order tells index pr kon hai..
//  order_by_key  tells ki us element se kitne strictly smaller element hai...
#define mod 1000000007
#define INF 1000000000000000000
#define fr first
#define se second
#define ll long long
#define PI 3.1415926535
#define pb push_back
#define mpr make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Senky_Bansal ios_base::sync_with_stdio(false);
#define IIIT_ALLAHABAD                                                         \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
ll fact[1000007];
ll mdpower(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res % mod * a % mod) % mod;
    b /= 2;
    a = (a % mod * a % mod) % mod;
  }
  return res;
}
void factorial() {
  fact[0] = 1;
  for (int i = 1; i < 1000007; i++)
    fact[i] = (fact[i - 1] % mod * i % mod) % mod;
}
ll inverse(ll a, ll b) { return mdpower(a, b - 2); }
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b /= 2;
  }
  return res;
}
int main() {
  Senky_Bansal IIIT_ALLAHABAD

      ll n;
  cin >> n;
  ll h[n];
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  ll dp[n + 1];
  for (int i = 1; i <= n; i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    if (i + 1 < n)
      dp[i + 1] = min(dp[i + 1], dp[i] + abs(h[i + 1] - h[i]));
    if (i + 2 < n)
      dp[i + 2] = min(dp[i + 2], dp[i] + abs(h[i + 2] - h[i]));
  }
  cout << dp[n - 1] << endl;
}