Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// Includes

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

// Defines

#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define MOD 1000000007
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define alla(a, n) a, a + n
#define mem(name, value) memset(name, value, sizeof(name))
#define f(i, a) for (int i = 0; i < a; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<string> vs;

int arr[100005], dp[100005];
int n;

int calc(int i) {
  if (i == n - 2)
    return std::abs(arr[n - 2] - arr[n - 1]);
  if (i == n - 1)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  int ans1 = std::abs(arr[i] - arr[i + 1]) + calc(i + 1);
  int ans2 = std::abs(arr[i] - arr[i + 2]) + calc(i + 2);
  return dp[i] = min(ans1, ans2);
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n;
  f(i, n) { cin >> arr[i]; }
  mem(dp, -1);

  cout << calc(0) << "\n";

  return 0;
}

Fixed submission:
// Includes

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

// Defines

#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define MOD 1000000007
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define alla(a, n) a, a + n
#define mem(name, value) memset(name, value, sizeof(name))
#define f(i, a) for (int i = 0; i < a; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<string> vs;

int arr[100005], dp[100005];
int n;

int calc(int i) {
  if (i == n - 2)
    return std::abs(arr[n - 2] - arr[n - 1]);
  if (i == n - 1)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  int ans1 = std::abs(arr[i] - arr[i + 1]) + calc(i + 1);
  int ans2 = std::abs(arr[i] - arr[i + 2]) + calc(i + 2);
  return dp[i] = min(ans1, ans2);
}

int main() {

  /*#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
  #endif*/
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n;
  f(i, n) { cin >> arr[i]; }
  mem(dp, -1);

  cout << calc(0) << "\n";

  return 0;
}