Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

#define int long long
#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
// rep(i,1,10) is from 1 to 9 and rep(I,10,1) is from 9 to 1
#define F first
#define S second
#define sz(x) ((int)x.size())
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define pf push_front
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define mod 1000000007
#define mi multiset<int>
#define mii multiset<pii>
#define what_is(x) cerr << #x << " is " << x << "\n";
#define sortA(v) sort(v.begin(), v.end())
#define sortD(v) sort(v.rbegin(), v.rend())
#define PI 3.14159265358979323846
#define vout(a)                                                                \
  for (auto x : a)                                                             \
    cout << x << " ";

// #define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag,
// tree_order_statistics_node_update>

// using namespace __gnu_pbds;
using namespace std;

int lcm(int a, int b) { return (a * (b / __gcd(a, b))); }

int modp(int a, int n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a;
  int res = modp(a, n / 2);
  if (n % 2 == 0)
    return (res * res);
  else
    return (a * res * res);
}

vi adj[1000005], ans;
vector<bool> vis(1000005, false);
map<pii, int> problem, elected;
int check = 0;

bool dfs(int s) {
  bool y = 0;
  vis[s] = true;
  for (int u : adj[s]) {
    if (!vis[u]) {
      vis[u] = true;
      if (problem[{u, s}] || problem[{s, u}]) {
        y = 1;
        if (!dfs(u)) {
          ans.pb(u);
        }
      } else {
        if (dfs(u)) {
          y = 1;
        }
      }
    }
  }
  return y;
}

void solve() {
  int t, n, m, i, k, r, l, u, v, w, j, s, sum = 0, pos, flag = 0, count = 0;
  string S, T, U;
  // int f,g,d;
  cin >> n;
  vi a(n);
  rep(i, 0, n) cin >> a[i];
  vi dp(n, 1000000);
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  rep(i, 2, n) {
    dp[i] =
        min(dp[i - 2] + abs(a[i - 2] - a[i]), dp[i - 1] + abs(a[i] - a[i - 1]));
  }
  cout << dp[n - 1];
}

signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
    cout << "\n";
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

#define int long long
#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
// rep(i,1,10) is from 1 to 9 and rep(I,10,1) is from 9 to 1
#define F first
#define S second
#define sz(x) ((int)x.size())
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define pf push_front
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define mod 1000000007
#define mi multiset<int>
#define mii multiset<pii>
#define what_is(x) cerr << #x << " is " << x << "\n";
#define sortA(v) sort(v.begin(), v.end())
#define sortD(v) sort(v.rbegin(), v.rend())
#define PI 3.14159265358979323846
#define vout(a)                                                                \
  for (auto x : a)                                                             \
    cout << x << " ";

// #define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag,
// tree_order_statistics_node_update>

// using namespace __gnu_pbds;
using namespace std;

int lcm(int a, int b) { return (a * (b / __gcd(a, b))); }

int modp(int a, int n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a;
  int res = modp(a, n / 2);
  if (n % 2 == 0)
    return (res * res);
  else
    return (a * res * res);
}

vi adj[1000005], ans;
vector<bool> vis(1000005, false);
map<pii, int> problem, elected;
int check = 0;

bool dfs(int s) {
  bool y = 0;
  vis[s] = true;
  for (int u : adj[s]) {
    if (!vis[u]) {
      vis[u] = true;
      if (problem[{u, s}] || problem[{s, u}]) {
        y = 1;
        if (!dfs(u)) {
          ans.pb(u);
        }
      } else {
        if (dfs(u)) {
          y = 1;
        }
      }
    }
  }
  return y;
}

void solve() {
  int t, n, m, i, k, r, l, u, v, w, j, s, sum = 0, pos, flag = 0, count = 0;
  string S, T, U;
  // int f,g,d;
  cin >> n;
  vi a(n);
  rep(i, 0, n) cin >> a[i];
  vi dp(n, 1000000);
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  rep(i, 2, n) {
    dp[i] =
        min(dp[i - 2] + abs(a[i - 2] - a[i]), dp[i - 1] + abs(a[i] - a[i - 1]));
  }
  cout << dp[n - 1];
}

signed main() {
  // #ifndef ONLINE_JUDGE
  //    freopen("input.txt","r",stdin);
  //     freopen("output.txt","w",stdout);
  //    #endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
    cout << "\n";
  }
  return 0;
}