Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define f(n) for (int i = 0; i < n; i++)
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(k, n) for (int i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define ll long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define OJ                                                                     \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;

const int mod = 1'000'000'007;
const int N = 3e5, M = N;
//=======================

void solve() {
  int n, e;
  cin >> n;
  vi a, r = {0};
  for (int i = 0; i < n; i++)
    cin >> e, a.emplace_back(e);
  r.emplace_back(abs(a[0] - a[1]));
  for (int i = 2; i < n; i++)
    r.emplace_back(
        min(r[i - 2] + abs(a[i] - a[i - 2]), r[i - 1] + abs(a[i - 1] - a[i])));
  cout << r[n - 1];
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  OJ;

  // clock_t start,stop;
  // start=clock();
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  // stop=clock();
  // cout<<endl<<stop-start<<"ms";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define f(n) for (int i = 0; i < n; i++)
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(k, n) for (int i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define ll long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define OJ                                                                     \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;

const int mod = 1'000'000'007;
const int N = 3e5, M = N;
//=======================

void solve() {
  int n, e;
  cin >> n;
  vi a, r = {0};
  for (int i = 0; i < n; i++)
    cin >> e, a.emplace_back(e);
  r.emplace_back(abs(a[0] - a[1]));
  for (int i = 2; i < n; i++)
    r.emplace_back(
        min(r[i - 2] + abs(a[i] - a[i - 2]), r[i - 1] + abs(a[i - 1] - a[i])));
  cout << r[n - 1];
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  // OJ;

  // clock_t start,stop;
  // start=clock();
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  // stop=clock();
  // cout<<endl<<stop-start<<"ms";

  return 0;
}