Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// *
// *
// *  Created By :hozaifa wahid
// * "mai maut ko takia aur kafan ko chadar banakar sota hu"
// *------------------------------------
// *  OS : Ubuntu 16.04
// *  Language : CPP14
// *  Editor : Sublime Text 3
// *  C++ compiler : g++
// *
// *
// https://www.geeksforgeeks.org/find-the-point-where-maximum-intervals-overlap/
#include <bits/stdc++.h>
#define pb push_back
// #define mp make_pair
#define inf 1000000007
#define fr first
#define sc second
#define eps 1e-9
#define clr(a) memset(a, 0, sizeof(a))
#define sz(x) x.size()
#define sni(x) scanf("%d", &x)
#define snl(x) scanf("%lld", &x)
#define snc(x) scanf("%c", &c);
#define rep(n) for (int i = 0; i < n; i++)
#define repc(i, n) for (int i = 0; i < n; i++)
#define FOR(i, x, y) for (int i = x; i < y; i++)
#define DEC(i, x, y) for (int i = x; i >= y; i--)
#define all(v) v.begin(), v.end()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define alla(a, n) a, a + n
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
ll MOD = 1000000007;
vector<ll> v[100055];
void sieve() {
  for (int n = 1; n <= 100055; n++) {
    for (int i = 1; i <= sqrt(n); i++) {
      if (n % i == 0) {
        // If divisors are equal, print only one
        if (n / i == i)
          v[n].push_back(i);

        else // Otherwise print both
        {
          v[n].push_back(i);
          v[n].push_back(n / i);
        }
      }
    }
  }
}
ll pw(ll a, ll b) {
  if (b == 0)
    return 1;
  ll x = pw(a, b / 2);
  x = (x * x) % MOD;
  if (b % 2)
    x = (x * a) % MOD;
  return x;
}
ll n, s;
ll a[100005];
int ok = 0;
// ll res=0;
ll dp[100005];
ll go(ll i) {
  if (i == n - 1) {
    return 0;
  } else if (i == n - 2) {
    return abs(a[i] - a[i + 1]);
  } else if (dp[i] > -1)
    return dp[i];
  else {
    s = min(abs(a[i] - a[i + 1]) + go(i + 1), abs(a[i + 2] - a[i]) + go(i + 2));
    dp[i] = s;
  }
  return s;
}

void solve() {
  ll t;
  t = 1;
  while (t--) {
    // ll n;
    cin >> n;
    // ll a[n];
    FOR(i, 0, n)
    cin >> a[i];
    memset(dp, -1, sizeof(dp));
    cout << go(0);
  }
}

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int start = clock();
#ifdef ONLINE_JUDGE
#else
  freopen("input.in", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  solve();
  int stop = clock();
#ifdef ONLINE_JUDGE
#else
  // cout << setprecision(6) << fixed << (stop - start) * 1000.00 /
  // double(CLOCKS_PER_SEC)<< endl;
#endif
}


Fixed submission:
// *
// *
// *  Created By :hozaifa wahid
// * "mai maut ko takia aur kafan ko chadar banakar sota hu"
// *------------------------------------
// *  OS : Ubuntu 16.04
// *  Language : CPP14
// *  Editor : Sublime Text 3
// *  C++ compiler : g++
// *
// *
// https://www.geeksforgeeks.org/find-the-point-where-maximum-intervals-overlap/
#include <bits/stdc++.h>
#define pb push_back
// #define mp make_pair
#define inf 1000000007
#define fr first
#define sc second
#define eps 1e-9
#define clr(a) memset(a, 0, sizeof(a))
#define sz(x) x.size()
#define sni(x) scanf("%d", &x)
#define snl(x) scanf("%lld", &x)
#define snc(x) scanf("%c", &c);
#define rep(n) for (int i = 0; i < n; i++)
#define repc(i, n) for (int i = 0; i < n; i++)
#define FOR(i, x, y) for (int i = x; i < y; i++)
#define DEC(i, x, y) for (int i = x; i >= y; i--)
#define all(v) v.begin(), v.end()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define alla(a, n) a, a + n
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
ll MOD = 1000000007;
vector<ll> v[100055];
void sieve() {
  for (int n = 1; n <= 100055; n++) {
    for (int i = 1; i <= sqrt(n); i++) {
      if (n % i == 0) {
        // If divisors are equal, print only one
        if (n / i == i)
          v[n].push_back(i);

        else // Otherwise print both
        {
          v[n].push_back(i);
          v[n].push_back(n / i);
        }
      }
    }
  }
}
ll pw(ll a, ll b) {
  if (b == 0)
    return 1;
  ll x = pw(a, b / 2);
  x = (x * x) % MOD;
  if (b % 2)
    x = (x * a) % MOD;
  return x;
}
ll n, s;
ll a[100005];
int ok = 0;
// ll res=0;
ll dp[100005];
ll go(ll i) {
  if (i == n - 1) {
    return 0;
  } else if (i == n - 2) {
    return abs(a[i] - a[i + 1]);
  } else if (dp[i] > -1)
    return dp[i];
  else {
    s = min(abs(a[i] - a[i + 1]) + go(i + 1), abs(a[i + 2] - a[i]) + go(i + 2));
    dp[i] = s;
  }
  return s;
}

void solve() {
  ll t;
  t = 1;
  while (t--) {
    // ll n;
    cin >> n;
    // ll a[n];
    FOR(i, 0, n)
    cin >> a[i];
    memset(dp, -1, sizeof(dp));
    cout << go(0);
  }
}

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int start = clock();
#ifdef ONLINE_JUDGE
#else
  // freopen("input.in" , "r" , stdin);
  //  freopen("output.txt", "w", stdout);
#endif
  solve();
  int stop = clock();
#ifdef ONLINE_JUDGE
#else
  // cout << setprecision(6) << fixed << (stop - start) * 1000.00 /
  // double(CLOCKS_PER_SEC)<< endl;
#endif
}