Problem p03160 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>

#define C continue;
#define R return

#define D double
#define I insert
#define ll long long
#define ld long double

#define ull unsigned long long
#define ui unsigned int

#define pb push_back
#define pf push_front

#define vi vector<int>
#define vc vector<char>
#define vs vector<string>
#define vb vector<bool>
#define vd vector<D>
#define vll vector<ll>
#define vull vector<ull>
#define vld vector<ld>
#define PQ priority_queue

#define vvi vector<vector<int>>
#define vvb vector<vector<bool>>
#define vvc vector<vector<char>>
#define vvll vector<vector<ll>>
#define vvd vector<vector<D>>
#define vvld vector<vector<ld>>

#define all(v) (v).begin(), (v).end()
#define allrev(v) (v).rbegin(), (v).rend()
#define allcomp(v) v.begin(), v.end(), comp
#define allrevcomp(v) v.rbegin(), v.rend(), comp

#define pii pair<int, int>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define pDD pair<D, D>
#define pipii pair<int, pii>
#define pcpii pair<char, pii>

#define vpld vector<pld>
#define vpii vector<pii>
#define vpll vector<pll>
#define vpDD vector<pDD>

#define vvpii vector<vector<pii>>
#define vpipii vector<pipii>

#define F first
#define S second
#define mp make_pair

#define unm unordered_map
#define unmii unm<int, int>
#define unmll unm<ll, ll>
#define unms unm<string, int>
#define unmci unm<char, int>

#define sortvia sort(a.begin(), a.end());
#define sortvib sort(b.begin(), b.end());
#define revsorta sort(a.begin(), a.end(), greater<int>());
#define revsortb sort(b.begin(), b.end(), greater<>());
#define loop(q, n) for (int i = q; i <= n; i++)
#define loopj(r, n) for (int j = r; j < n; j++)
#define loopm(s, m) for (int k = s; k < m; k++)
#define test                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define nextline "\n"
#define tab "\t"
#define space " "
// YES
// NO
// cout
// true
// false
// yes
// no

const ll mod9 = 1e9 + 7;
const ll maxsize = 2e9 + 1;
const ll mod = 998244353;
const ll mod2 = 1073741824;
const ll INF = 1e8;

using namespace std;

int modfn(int p) {
  if (p < 0)
    return (-1 * p);
  else
    return p;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vi h(n + 1);
  vi a(n + 1);
  loop(1, n) cin >> h[i];
  // loop(1,n)cout<<h[i]<<space;
  a[n] = 0;
  a[n - 1] = modfn(h[n] - h[n - 1]);
  // cout<<a[n]<<space<<a[n-1];
  // cout<<nextline;
  int term1, term2;
  for (int i = n - 2; i >= 1; i--) {
    term1 = modfn(h[i] - h[i + 1]) + a[i + 1];
    term2 = modfn(h[i] - h[i + 2]) + a[i + 2];
    // cout<<i<<space<<term1<<space<<term2<<nextline;
    a[i] = min(term1, term2);
  }
  cout << a[1];
}


Fixed submission:

#include <bits/stdc++.h>

#define C continue;
#define R return

#define D double
#define I insert
#define ll long long
#define ld long double

#define ull unsigned long long
#define ui unsigned int

#define pb push_back
#define pf push_front

#define vi vector<int>
#define vc vector<char>
#define vs vector<string>
#define vb vector<bool>
#define vd vector<D>
#define vll vector<ll>
#define vull vector<ull>
#define vld vector<ld>
#define PQ priority_queue

#define vvi vector<vector<int>>
#define vvb vector<vector<bool>>
#define vvc vector<vector<char>>
#define vvll vector<vector<ll>>
#define vvd vector<vector<D>>
#define vvld vector<vector<ld>>

#define all(v) (v).begin(), (v).end()
#define allrev(v) (v).rbegin(), (v).rend()
#define allcomp(v) v.begin(), v.end(), comp
#define allrevcomp(v) v.rbegin(), v.rend(), comp

#define pii pair<int, int>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define pDD pair<D, D>
#define pipii pair<int, pii>
#define pcpii pair<char, pii>

#define vpld vector<pld>
#define vpii vector<pii>
#define vpll vector<pll>
#define vpDD vector<pDD>

#define vvpii vector<vector<pii>>
#define vpipii vector<pipii>

#define F first
#define S second
#define mp make_pair

#define unm unordered_map
#define unmii unm<int, int>
#define unmll unm<ll, ll>
#define unms unm<string, int>
#define unmci unm<char, int>

#define sortvia sort(a.begin(), a.end());
#define sortvib sort(b.begin(), b.end());
#define revsorta sort(a.begin(), a.end(), greater<int>());
#define revsortb sort(b.begin(), b.end(), greater<>());
#define loop(q, n) for (int i = q; i <= n; i++)
#define loopj(r, n) for (int j = r; j < n; j++)
#define loopm(s, m) for (int k = s; k < m; k++)
#define test                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define nextline "\n"
#define tab "\t"
#define space " "
// YES
// NO
// cout
// true
// false
// yes
// no

const ll mod9 = 1e9 + 7;
const ll maxsize = 2e9 + 1;
const ll mod = 998244353;
const ll mod2 = 1073741824;
const ll INF = 1e8;

using namespace std;

int modfn(int p) {
  if (p < 0)
    return (-1 * p);
  else
    return p;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vi h(n + 1);
  vi a(n + 1);
  loop(1, n) cin >> h[i];
  // loop(1,n)cout<<h[i]<<space;
  a[n] = 0;
  a[n - 1] = modfn(h[n] - h[n - 1]);
  // cout<<a[n]<<space<<a[n-1];
  // cout<<nextline;
  int term1, term2;
  for (int i = n - 2; i >= 1; i--) {
    term1 = modfn(h[i] - h[i + 1]) + a[i + 1];
    term2 = modfn(h[i] - h[i + 2]) + a[i + 2];
    // cout<<i<<space<<term1<<space<<term2<<nextline;
    a[i] = min(term1, term2);
  }
  cout << a[1];
}