Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<ll>

#define sz(x) ((int)(x).size())
#define pll pair<long long, long long>
#define mp make_pair
#define F first
#define S second
#define T                                                                      \
  int tt;                                                                      \
  cin >> tt;                                                                   \
  while (tt--)
#define fastio ios_base::sync_with_stdio(false);

#define endl '\n'
#define all(a) (a).begin(), (a).end()
#define display(x)                                                             \
  for (auto i : x)                                                             \
    cout << i << " ";                                                          \
  cout << '\n';
#define input(x)                                                               \
  for (int i = 0; i < n; i++)                                                  \
    cin >> x[i];
#define make(x, y) fill(all(x), y)
#define FO(i, startLoop, n) for (int i = startLoop; i < n; i++)

#define mod 1000000007
#define Y cout << "YES" << endl;
#define N cout << "NO" << endl;
#define ITFO(myiterator, mymap)                                                \
  for (auto myiterator = mymap.begin(); myiterator != mymap.end(); ++myiterator)
using namespace std;

int main() {
  fastio cin.tie(NULL);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n;
  cin >> n;
  vi a(n, 0), dp(n, INT_MAX);
  input(a);
  dp[0] = 0;
  dp[1] = abs(a[0] - a[1]);

  for (int i = 2; i < n; ++i)
    dp[i] =
        min(abs(a[i] - a[i - 1]) + dp[i - 1], abs(a[i] - a[i - 2]) + dp[i - 2]);
  cout << dp[n - 1];
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<ll>

#define sz(x) ((int)(x).size())
#define pll pair<long long, long long>
#define mp make_pair
#define F first
#define S second
#define T                                                                      \
  int tt;                                                                      \
  cin >> tt;                                                                   \
  while (tt--)
#define fastio ios_base::sync_with_stdio(false);

#define endl '\n'
#define all(a) (a).begin(), (a).end()
#define display(x)                                                             \
  for (auto i : x)                                                             \
    cout << i << " ";                                                          \
  cout << '\n';
#define input(x)                                                               \
  for (int i = 0; i < n; i++)                                                  \
    cin >> x[i];
#define make(x, y) fill(all(x), y)
#define FO(i, startLoop, n) for (int i = startLoop; i < n; i++)

#define mod 1000000007
#define Y cout << "YES" << endl;
#define N cout << "NO" << endl;
#define ITFO(myiterator, mymap)                                                \
  for (auto myiterator = mymap.begin(); myiterator != mymap.end(); ++myiterator)
using namespace std;

int main() {
  fastio cin.tie(NULL);
  cout.tie(0);

  int n;
  cin >> n;
  vi a(n, 0), dp(n, INT_MAX);
  input(a);
  dp[0] = 0;
  dp[1] = abs(a[0] - a[1]);

  for (int i = 2; i < n; ++i)
    dp[i] =
        min(abs(a[i] - a[i - 1]) + dp[i - 1], abs(a[i] - a[i - 2]) + dp[i - 2]);
  cout << dp[n - 1];
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}