Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// Code by : Abhishek Tiwari
// codechef.com/users/devil_within || hackerearth.com/@devil_within
#include <bits/stdc++.h>
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define int long long int
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define pii pair<int, int>
#define ff first
#define ss second
#define mod 1000000007
#define gcd __gcd
#define lcm(a, b) ((a) * (b)) / gcd(a, b)
using namespace std;

/*******************************************************************************************/
/*******************************************************************************************/
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  for (int i = 0; i < v.size() - 1; ++i)
    os << v[i] << ", ";
  os << v[v.size() - 1] << "]\n";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{";
  for (auto it : v) {
    os << it;
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "}\n";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  os << "{";
  for (auto it : v) {
    os << "(" << it.first << " : " << it.second << ")";
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "}\n";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << "(";
  os << v.first << ", " << v.second << ")";
  return os;
}
/*******************************************************************************************/
/*******************************************************************************************/
int fpow(int x, int y, int p);

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  fio;
  int tcases = 1;
  // cin>>testcases;
  for (int tcase = 1; tcase <= tcases; ++tcase) {
    int n;
    cin >> n;
    int a[n];
    FOR(i, 0, n) cin >> a[i];
    vector<int> dp(n, mod);
    dp[0] = 0;
    for (int i = 0; i < n; ++i) {
      if (i + 1 < n)
        dp[i + 1] = min(dp[i + 1], abs(a[i] - a[i + 1]) + dp[i]);
      if (i + 2 < n)
        dp[i + 2] = min(dp[i + 2], abs(a[i] - a[i + 2]) + dp[i]);
    }
    cout << dp[n - 1] << "\n";
    // cout<<"Case #"<<tcase<<": ";
  }
  return 0;
}

// Functions

int fpow(int x, int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}


Fixed submission:
// Code by : Abhishek Tiwari
// codechef.com/users/devil_within || hackerearth.com/@devil_within
#include <bits/stdc++.h>
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define int long long int
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define pii pair<int, int>
#define ff first
#define ss second
#define mod 1000000007
#define gcd __gcd
#define lcm(a, b) ((a) * (b)) / gcd(a, b)
using namespace std;

/*******************************************************************************************/
/*******************************************************************************************/
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  for (int i = 0; i < v.size() - 1; ++i)
    os << v[i] << ", ";
  os << v[v.size() - 1] << "]\n";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{";
  for (auto it : v) {
    os << it;
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "}\n";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  os << "{";
  for (auto it : v) {
    os << "(" << it.first << " : " << it.second << ")";
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "}\n";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << "(";
  os << v.first << ", " << v.second << ")";
  return os;
}
/*******************************************************************************************/
/*******************************************************************************************/
int fpow(int x, int y, int p);

int32_t main() {
#ifndef ONLINE_JUDGE
// freopen("input.txt" , "r" , stdin);freopen("output.txt" , "w" ,
// stdout);freopen("error.txt" , "w" , stderr);
#endif
  fio;
  int tcases = 1;
  // cin>>testcases;
  for (int tcase = 1; tcase <= tcases; ++tcase) {
    int n;
    cin >> n;
    int a[n];
    FOR(i, 0, n) cin >> a[i];
    vector<int> dp(n, mod);
    dp[0] = 0;
    for (int i = 0; i < n; ++i) {
      if (i + 1 < n)
        dp[i + 1] = min(dp[i + 1], abs(a[i] - a[i + 1]) + dp[i]);
      if (i + 2 < n)
        dp[i + 2] = min(dp[i + 2], abs(a[i] - a[i + 2]) + dp[i]);
    }
    cout << dp[n - 1] << "\n";
    // cout<<"Case #"<<tcase<<": ";
  }
  return 0;
}

// Functions

int fpow(int x, int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}