Problem p03160 (C++)

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

#define fi first
#define se second
#define endl '\n'
#define lc (rt << 1)
#define rc (rt << 1 | 1)
#define mp make_pair
#define pb push_back
#define lowbit(a) (a & (-a))
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define mst(a, v) memset(a, v, sizeof(a))
#define debug(x) cout << #x ": " << x << '\n';
#define IOS                                                                    \
  {                                                                            \
    ios::sync_with_stdio(false);                                               \
    cin.tie(0);                                                                \
    cout.tie(0);                                                               \
  }

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

const int N = 1000010;
const int mod = 1e9 + 7;
const ld PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;

template <typename T> inline void gmax(T &A, T B) { (A < B) && (A = B); }
template <typename T> inline void gmin(T &A, T B) { (A > B) && (A = B); }

template <class T> inline bool re(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF)
    return 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}

int h[11111], dp[11111];

int main() {
#ifdef local
  // freopen("in.txt", "r", stdin);
#endif
  IOS;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  dp[2] = abs(h[1] - h[2]);
  for (int i = 3; 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];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define endl '\n'
#define lc (rt << 1)
#define rc (rt << 1 | 1)
#define mp make_pair
#define pb push_back
#define lowbit(a) (a & (-a))
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define mst(a, v) memset(a, v, sizeof(a))
#define debug(x) cout << #x ": " << x << '\n';
#define IOS                                                                    \
  {                                                                            \
    ios::sync_with_stdio(false);                                               \
    cin.tie(0);                                                                \
    cout.tie(0);                                                               \
  }

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

const int N = 1000010;
const int mod = 1e9 + 7;
const ld PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;

template <typename T> inline void gmax(T &A, T B) { (A < B) && (A = B); }
template <typename T> inline void gmin(T &A, T B) { (A > B) && (A = B); }

template <class T> inline bool re(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF)
    return 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}

int h[111111], dp[111111];

int main() {
#ifdef local
  // freopen("in.txt", "r", stdin);
#endif
  IOS;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  dp[2] = abs(h[1] - h[2]);
  for (int i = 3; 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];
  return 0;
}