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>

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using ldb = long double;
using pa = pair<ll, ll>;
using vec = vector<ll>;

template <typename T>
using index_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using MinPriorityQueue = priority_queue<T, vector<T>, greater<T>>;

#define pb push_back
#define po pop_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define f(i, x, n) for (ll i = x; i < n; i++)
#define unique_sort(x)                                                         \
  sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))
#define all(c) c.begin(), c.end()
#define str string
#define edl "\n"
#define add insert
#define cot continue
#define fast()                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

const int MOD = 1e9 + 7, INF = INT_MAX, N = 1e5 + 10;
const double PI = acos(-1);
const ll LINF = LLONG_MAX;
vec dp(100002, INT_MAX);
ll q[100001], n;

int main() {

  fast();

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> n;
  f(i, 0, n) { cin >> q[i]; }
  dp[0] = 0;
  f(i, 0, n) {
    for (ll j = i + 1; j <= i + 2 && j < n; j++) {
      dp[j] = min(dp[j], dp[i] + abs(q[i] - q[j]));
    }
  }
  cout << dp[n - 1];

  cerr << "Time taken: " << int((clock() * 1000.) / CLOCKS_PER_SEC) << "ms\n";
  return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using ldb = long double;
using pa = pair<ll, ll>;
using vec = vector<ll>;

template <typename T>
using index_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using MinPriorityQueue = priority_queue<T, vector<T>, greater<T>>;

#define pb push_back
#define po pop_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define f(i, x, n) for (ll i = x; i < n; i++)
#define unique_sort(x)                                                         \
  sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))
#define all(c) c.begin(), c.end()
#define str string
#define edl "\n"
#define add insert
#define cot continue
#define fast()                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

const int MOD = 1e9 + 7, INF = INT_MAX, N = 1e5 + 10;
const double PI = acos(-1);
const ll LINF = LLONG_MAX;
vec dp(100002, INT_MAX);
ll q[100001], n;

int main() {

  fast();

  // #ifndef ONLINE_JUDGE
  //         freopen("input.txt", "r", stdin);
  //         freopen("output.txt","w",stdout);
  // #endif
  cin >> n;
  f(i, 0, n) { cin >> q[i]; }
  dp[0] = 0;
  f(i, 0, n) {
    for (ll j = i + 1; j <= i + 2 && j < n; j++) {
      dp[j] = min(dp[j], dp[i] + abs(q[i] - q[j]));
    }
  }
  cout << dp[n - 1];

  cerr << "Time taken: " << int((clock() * 1000.) / CLOCKS_PER_SEC) << "ms\n";
  return 0;
}