Problem p03160 (C++)

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

#define ff first
#define ss second
#define ll long long
#define dd double
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define vll vector<long long>
#define vdd vector<double>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long, long long>>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define mii map<int, int>
#define mll map<long long, long long>
#define umii unordered_map<int, int>
#define umll unordered_map<long long, long long>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, a, b) for (int i = a; i < b; i++)
#define rev(i, n) for (int i = n; i >= 0; i--)
#define rev2(i, a, b) for (int i = a; i >= b; i--)
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define inf 9e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl '\n'
#define all(x) x.begin(), x.end()
#define d0(x) cout << x << " "
#define d1(x) cout << x << endl
#define d2(x, y) cout << x << " " << y << endl
#define d3(x, y, z) cout << x << " " << y << " " << z << endl
#define d4(x, y, z, w) cout << x << " " << y << " " << z << " " << w << endl
#define read2(x, y) cin >> x >> y
#define read3(x, y, z) cin >> x >> y >> z
#define read4(x, y, z, w) cin >> x >> y >> z >> w
#define read5(x, y, z, w, a) cin >> x >> y >> z >> w >> a
#define sz(a) (int)a.size()
const ll mod = 1e9 + 7;
const double PI = 3.14159265359;
const ll MOD = 998244353;
void i_o() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int help(vi &A) {
  int n = sz(A);
  vi dp(n);
  dp[0] = 0;
  rep2(i, 1, n) {
    int x = INT_MAX;
    if (i >= 2)
      x = min(x, dp[i - 2] + abs(A[i] - A[i - 2]));
    x = min(x, dp[i - 1] + abs(A[i] - A[i - 1]));
    dp[i] = x;
  }
  return dp[n - 1];
}
int main() {
  fio i_o();
  int n;
  cin >> n;
  vi h(n);
  rep(i, n) cin >> h[i];
  d1(help(h));
  return 0;
}


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

#define ff first
#define ss second
#define ll long long
#define dd double
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define vll vector<long long>
#define vdd vector<double>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long, long long>>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define mii map<int, int>
#define mll map<long long, long long>
#define umii unordered_map<int, int>
#define umll unordered_map<long long, long long>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, a, b) for (int i = a; i < b; i++)
#define rev(i, n) for (int i = n; i >= 0; i--)
#define rev2(i, a, b) for (int i = a; i >= b; i--)
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define inf 9e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl '\n'
#define all(x) x.begin(), x.end()
#define d0(x) cout << x << " "
#define d1(x) cout << x << endl
#define d2(x, y) cout << x << " " << y << endl
#define d3(x, y, z) cout << x << " " << y << " " << z << endl
#define d4(x, y, z, w) cout << x << " " << y << " " << z << " " << w << endl
#define read2(x, y) cin >> x >> y
#define read3(x, y, z) cin >> x >> y >> z
#define read4(x, y, z, w) cin >> x >> y >> z >> w
#define read5(x, y, z, w, a) cin >> x >> y >> z >> w >> a
#define sz(a) (int)a.size()
const ll mod = 1e9 + 7;
const double PI = 3.14159265359;
const ll MOD = 998244353;
void i_o() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int help(vi &A) {
  int n = sz(A);
  vi dp(n);
  dp[0] = 0;
  rep2(i, 1, n) {
    int x = INT_MAX;
    if (i >= 2)
      x = min(x, dp[i - 2] + abs(A[i] - A[i - 2]));
    x = min(x, dp[i - 1] + abs(A[i] - A[i - 1]));
    dp[i] = x;
  }
  return dp[n - 1];
}
int main() {
  fio
      // i_o();
      int n;
  cin >> n;
  vi h(n);
  rep(i, n) cin >> h[i];
  d1(help(h));
  return 0;
}