Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble

// typedef tuple <int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
typedef vector<int> vi;
typedef vector<ll> vll;

#define siz(x) (int)(x).size()
#define Y second
#define X first
#define IN insert
#define PB push_back
#define P pop_back
#define PU push
#define PO pop
// #define F find
#define ED end()
#define MP(a, b) make_pair(a, b)

// #define MOD 1000000007ll
#define INF 1000000000
#define endl "\n"
#define rep(a, i, b) for (ll i = (a); i < (b); ++i)
#define reps(a, i, b) for (ll i = (a); i <= (b); ++i)
#define repd(num, i, ed) for (ll(i) = (num)-1; (i) >= ed; --(i))
#define repkt(bg, i, num, kt) for (ll(i) = (bg); (i) < (num) && (kt); ++(i))
#define repchar(i) for (char(i) = 48; (i) <= 122; (i)++)
#define in(n) scanf("%lld", &n)
#define out(n) printf("%lld\n", n)
#define outS(s) printf("%s\n", s)
#define setA(a, n)                                                             \
  for (ll i = 0, j = sizeof(a) / sizeof(a[0]); i < j; ++i)                     \
    a[i] = n;
#define repe(num, i, ed) for (ll(i) = (num); (i) >= ed; --(i))
// const int INF=0x3f3f3f3f;
const ll mod = 1e9 + 7;
const ll N = 1e5 + 5, K = 105;
const int si = (1 << 20);
const ll M = 50 + 5;
const ll oo = 1e18 + 5;
const double INFdb = 1e10;

int n;
int h[N];
int dp[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> h[i];
  h[0] = h[1];
  dp[0] = dp[1] = 0;
  for (int i = 2; i <= n; ++i) {
    dp[i] =
        min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << dp[n];
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble

// typedef tuple <int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
typedef vector<int> vi;
typedef vector<ll> vll;

#define siz(x) (int)(x).size()
#define Y second
#define X first
#define IN insert
#define PB push_back
#define P pop_back
#define PU push
#define PO pop
// #define F find
#define ED end()
#define MP(a, b) make_pair(a, b)

// #define MOD 1000000007ll
#define INF 1000000000
#define endl "\n"
#define rep(a, i, b) for (ll i = (a); i < (b); ++i)
#define reps(a, i, b) for (ll i = (a); i <= (b); ++i)
#define repd(num, i, ed) for (ll(i) = (num)-1; (i) >= ed; --(i))
#define repkt(bg, i, num, kt) for (ll(i) = (bg); (i) < (num) && (kt); ++(i))
#define repchar(i) for (char(i) = 48; (i) <= 122; (i)++)
#define in(n) scanf("%lld", &n)
#define out(n) printf("%lld\n", n)
#define outS(s) printf("%s\n", s)
#define setA(a, n)                                                             \
  for (ll i = 0, j = sizeof(a) / sizeof(a[0]); i < j; ++i)                     \
    a[i] = n;
#define repe(num, i, ed) for (ll(i) = (num); (i) >= ed; --(i))
// const int INF=0x3f3f3f3f;
const ll mod = 1e9 + 7;
const ll N = 1e5 + 5, K = 105;
const int si = (1 << 20);
const ll M = 50 + 5;
const ll oo = 1e18 + 5;
const double INFdb = 1e10;

int n;
int h[N];
int dp[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
#endif
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> h[i];
  h[0] = h[1];
  dp[0] = dp[1] = 0;
  for (int i = 2; i <= n; ++i) {
    dp[i] =
        min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << dp[n];
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}