Problem p03160 (C++)

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define MX 200005
#define mod 1000000007
#define inf 1e18
#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 pw(b, p) pow(b, p) + 0.1
#define ull unsigned long long
#define ll long long
void codeforces() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.in", "r", stdin);
  freopen("output.in", "w", stdout);
#endif
}

int32_t main() {
  codeforces();
  // int t;cin>>t;
  // while(t--) {

  // }
  int n;
  cin >> n;
  vi a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vi dp(n, INT_MAX);
  dp[0] = 0;
  if (n == 1) {
    cout << dp[0] << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    int x = a[i];
    if (i + 1 < n) {
      int y = a[i + 1];
      y = abs(y - x);
      dp[i + 1] = min(dp[i + 1], y + dp[i]);
    }
    if (i + 2 < n) {
      int y = a[i + 2];
      y = abs(y - x);
      dp[i + 2] = min(dp[i + 2], y + dp[i]);
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define MX 200005
#define mod 1000000007
#define inf 1e18
#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 pw(b, p) pow(b, p) + 0.1
#define ull unsigned long long
#define ll long long
void codeforces() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.in", "r", stdin);
  freopen("output.in", "w", stdout);
#endif
}

int32_t main() {
  // codeforces();
  //  int t;cin>>t;
  //  while(t--) {

  // }
  int n;
  cin >> n;
  vi a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vi dp(n, INT_MAX);
  dp[0] = 0;
  if (n == 1) {
    cout << dp[0] << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    int x = a[i];
    if (i + 1 < n) {
      int y = a[i + 1];
      y = abs(y - x);
      dp[i + 1] = min(dp[i + 1], y + dp[i]);
    }
    if (i + 2 < n) {
      int y = a[i + 2];
      y = abs(y - x);
      dp[i + 2] = min(dp[i + 2], y + dp[i]);
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}