Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <vector>
#define ll long long
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mod 1000000009
using namespace std;
bool isprime(ll n) {
  // if(n<2)
  // return false;
  if (n == 1)
    return true;
  if (n == 2 || n == 3)
    return true;
  if (n % 2 == 0)
    return false;
  for (ll i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return false;
  }
  return true;
}
int gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
int lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
set<ll> fac;
void allFactors(ll n) {
  // set<ll> fac;
  for (ll i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      fac.insert(i);
      if (i != n / i) {
        fac.insert(n / i);
      }
    }
  }
  // return fac.size();
}
bool threefactors(ll n) // when you have need of a no which is divisible by
                        // 1,itself,any other one no.
{
  ll p = sqrt(n);
  if (p * p == n) {
    if (isprime(p))
      return 1;
    else
      return 0;
  } else
    return 0;
}

int main() {
  IOS
  // int w;
  // cin>>w;
  // while(w--)
  {
    int n;
    cin >> n;
    int a[n + 1];
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    ll dp[10000];
    for (int i = 0; i < 10000; i++)
      dp[i] = mod;
    dp[1] = 0;
    for (int i = 1; i < n; i++) {
      for (int j = i + 1; j <= i + 2; j++) {
        if (j <= n)
          dp[j] = min(dp[j], dp[i] + abs(a[i] - a[j]));
      }
    }
    cout << dp[n];
  }
  return 0;
}
// ascii of a=97
// ascii of A=65
// ascii of 0=48


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <vector>
#define ll long long
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mod 1000000009
using namespace std;
bool isprime(ll n) {
  // if(n<2)
  // return false;
  if (n == 1)
    return true;
  if (n == 2 || n == 3)
    return true;
  if (n % 2 == 0)
    return false;
  for (ll i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return false;
  }
  return true;
}
int gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
int lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
set<ll> fac;
void allFactors(ll n) {
  // set<ll> fac;
  for (ll i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      fac.insert(i);
      if (i != n / i) {
        fac.insert(n / i);
      }
    }
  }
  // return fac.size();
}
bool threefactors(ll n) // when you have need of a no which is divisible by
                        // 1,itself,any other one no.
{
  ll p = sqrt(n);
  if (p * p == n) {
    if (isprime(p))
      return 1;
    else
      return 0;
  } else
    return 0;
}

int main() {
  IOS
  // int w;
  // cin>>w;
  // while(w--)
  {
    int n;
    cin >> n;
    int a[n + 1];
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    ll dp[10000000];
    for (int i = 0; i < 10000000; i++)
      dp[i] = mod;
    dp[1] = 0;
    for (int i = 1; i < n; i++) {
      for (int j = i + 1; j <= i + 2; j++) {
        if (j <= n)
          dp[j] = min(dp[j], dp[i] + abs(a[i] - a[j]));
      }
    }
    cout << dp[n];
  }
  return 0;
}
// ascii of a=97
// ascii of A=65
// ascii of 0=48