Problem p03160 (C++)

Buggy submission (status: Runtime Error):
/*
        ################
        # Avi Kasliwal #
        # Noob Coder:( #
        ################
*/

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(NULL)

#define deb(x) cout << "#x " << x << "\n"

#define newline cout << "\n"
#define yes                                                                    \
  cout << "Yes"                                                                \
       << "\n"
#define no                                                                     \
  cout << "No"                                                                 \
       << "\n"
#define Yes                                                                    \
  cout << "YES"                                                                \
       << "\n"
#define No                                                                     \
  cout << "NO"                                                                 \
       << "\n"

#define fixDeci(num, places)                                                   \
  std::cout << std::fixed << std::setprecision(places) << num << '\n'
#define setBits(x) __builtin_popcount(x)
#define zerobits(x) __builtint_ctzll(x)
#define MOD 1000000007

#define rep(i, s, e) for (i = s; i < e; i++)
#define repr(i, s, e) for (i = s; i >= e; i--)

// GCD Function
int GCD(int a, int b) {
  if (b == 0) {
    return a;
  } else {
    return GCD(b, a % b);
  }
}

// LCM Function
int LCM(int a, int b) {
  int gcd = GCD(a, b);
  return (max(a, b) / gcd) * (min(a, b));
}

/*int p[100000001];
void Sieve() {
        p[0] = p[1] = 0;
        p[2] = 1;
        for (int i = 4; i < 100000000; i += 2) {
                p[i] = 0;
        }
        for (int i = 3; i * i < 100000000; i++) {
                if (p[i] == 1) {
                        for (int j = i*i; j < 100000000; j += i) {
                                p[j] = 0;
                        }
                }
        }
}*/

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS;
  int i, j;

  /*######## Code Here ##########*/
  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

  int t;
  t = 1;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    rep(i, 0, n) { cin >> a[i]; }
    int dp[n];
    dp[0] = 0;
    dp[1] = abs(a[1] - a[0]);
    rep(i, 2, n) {
      dp[i] = min(dp[i - 1] + abs(a[i] - a[i - 1]),
                  dp[i - 2] + abs(a[i] - a[i - 2]));
    }
    cout << dp[n - 1] << "\n";
  }

  return 0;
}

/*Timus : 294930IX */

Fixed submission:
/*
        ################
        # Avi Kasliwal #
        # Noob Coder:( #
        ################
*/

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(NULL)

#define deb(x) cout << "#x " << x << "\n"

#define newline cout << "\n"
#define yes                                                                    \
  cout << "Yes"                                                                \
       << "\n"
#define no                                                                     \
  cout << "No"                                                                 \
       << "\n"
#define Yes                                                                    \
  cout << "YES"                                                                \
       << "\n"
#define No                                                                     \
  cout << "NO"                                                                 \
       << "\n"

#define fixDeci(num, places)                                                   \
  std::cout << std::fixed << std::setprecision(places) << num << '\n'
#define setBits(x) __builtin_popcount(x)
#define zerobits(x) __builtint_ctzll(x)
#define MOD 1000000007

#define rep(i, s, e) for (i = s; i < e; i++)
#define repr(i, s, e) for (i = s; i >= e; i--)

// GCD Function
int GCD(int a, int b) {
  if (b == 0) {
    return a;
  } else {
    return GCD(b, a % b);
  }
}

// LCM Function
int LCM(int a, int b) {
  int gcd = GCD(a, b);
  return (max(a, b) / gcd) * (min(a, b));
}

/*int p[100000001];
void Sieve() {
        p[0] = p[1] = 0;
        p[2] = 1;
        for (int i = 4; i < 100000000; i += 2) {
                p[i] = 0;
        }
        for (int i = 3; i * i < 100000000; i++) {
                if (p[i] == 1) {
                        for (int j = i*i; j < 100000000; j += i) {
                                p[j] = 0;
                        }
                }
        }
}*/

int32_t main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif
  IOS;
  int i, j;

  /*######## Code Here ##########*/
  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

  int t;
  t = 1;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    rep(i, 0, n) { cin >> a[i]; }
    int dp[n];
    dp[0] = 0;
    dp[1] = abs(a[1] - a[0]);
    rep(i, 2, n) {
      dp[i] = min(dp[i - 1] + abs(a[i] - a[i - 1]),
                  dp[i - 2] + abs(a[i] - a[i - 2]));
    }
    cout << dp[n - 1] << "\n";
  }

  return 0;
}

/*Timus : 294930IX */