Problem p03160 (C++)

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

string to_string(string s) { return '"' + s + '"'; }
string to_string(char ch) {
  string s(1, ch);
  return '\'' + s + '\'';
}
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifndef ONLINE_JUDGE
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

typedef long long int ll;
#define int ll
#define pb push_back
#define pll pair<int, int>
#define mll map<int, int>
#define F first
#define S second
#define lbnd(v, val) lower_bound(v.begin(), v.end(), val)
#define ubnd(v, val) upper_bound(v.begin(), v.end(), val)
#define bs(v, val) binary_search(v.begin(), v.end(), val)
#define mp make_pair
#define f(i, x, n) for (int i = x; i < n; i++)
#define isup isupper
#define islow islower
#define max3(a, b, c) max(a, max(b, c))
#define endl "\n"
#define countbits(n) __builtin_popcountll(n)
#define all(ar) ar.begin(), ar.end()
#define rall(ar) (ar).rbegin(), (ar).rend()
const ll mod = 1e9 + 7;
const ll inf = LLONG_MAX;
const ll N = 2e5 + 10;

typedef vector<vector<ll>> matrix;
typedef vector<ll> vll;

void solve() {
  int n;
  cin >> n;
  vll v(n);
  f(i, 0, n) { cin >> v[i]; }
  int sum = 0;
  vll ans(n, 0);
  if (n == 2) {
    cout << abs(v[0] - v[1]) << endl;
    return;
  }
  ans[n - 2] = abs(v[n - 2] - v[n - 1]);
  for (int i = n - 3; i >= 0; i--) {
    ans[i] = min(abs(v[i] - v[i + 1]) + ans[i + 1],
                 abs(v[i] - v[i + 2]) + ans[i + 2]);
  }
  // debug(ans);
  cout << ans[0] << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

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

  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


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

string to_string(string s) { return '"' + s + '"'; }
string to_string(char ch) {
  string s(1, ch);
  return '\'' + s + '\'';
}
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifndef ONLINE_JUDGE
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

typedef long long int ll;
#define int ll
#define pb push_back
#define pll pair<int, int>
#define mll map<int, int>
#define F first
#define S second
#define lbnd(v, val) lower_bound(v.begin(), v.end(), val)
#define ubnd(v, val) upper_bound(v.begin(), v.end(), val)
#define bs(v, val) binary_search(v.begin(), v.end(), val)
#define mp make_pair
#define f(i, x, n) for (int i = x; i < n; i++)
#define isup isupper
#define islow islower
#define max3(a, b, c) max(a, max(b, c))
#define endl "\n"
#define countbits(n) __builtin_popcountll(n)
#define all(ar) ar.begin(), ar.end()
#define rall(ar) (ar).rbegin(), (ar).rend()
const ll mod = 1e9 + 7;
const ll inf = LLONG_MAX;
const ll N = 2e5 + 10;

typedef vector<vector<ll>> matrix;
typedef vector<ll> vll;

void solve() {
  int n;
  cin >> n;
  vll v(n);
  f(i, 0, n) { cin >> v[i]; }
  int sum = 0;
  vll ans(n, 0);
  if (n == 2) {
    cout << abs(v[0] - v[1]) << endl;
    return;
  }
  ans[n - 2] = abs(v[n - 2] - v[n - 1]);
  for (int i = n - 3; i >= 0; i--) {
    ans[i] = min(abs(v[i] - v[i + 1]) + ans[i + 1],
                 abs(v[i] - v[i + 2]) + ans[i + 2]);
  }
  // debug(ans);
  cout << ans[0] << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  //    #ifndef ONLINE_JUDGE
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  //    #endif

  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}