Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define stdi(in) freopen(in, "r", stdin)
#define stdo(out) freopen(out, "w", stdout)
#define stdio(in, out)                                                         \
  stdi(in);                                                                    \
  stdo(out)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define MAX 1000007
#define MOD 1000000007
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
// typedef int ittt;
// #define int ll

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

const ll inf = 1e18;

ll hit[MAX];
ll dp[MAX];
int n;

ll solve(int i) {
  if (i <= 1) {
    return 0;
  }

  if (dp[i] != -1)
    return dp[i];

  ll ans = solve(i - 1) + abs(hit[i] - hit[i - 1]);

  if (i - 2 > 0) {
    ans = min(ans, solve(i - 2) + abs(hit[i] - hit[i - 2]));
  }

  return dp[i] = ans;
}

int main() {
#ifndef ONLINE_JUDGE
  stdio("in", "out");
#endif
  fast;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> hit[i];
    dp[i] = -1;
  }

  ll ans = solve(n);
  cout << ans << '\n';

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define stdi(in) freopen(in, "r", stdin)
#define stdo(out) freopen(out, "w", stdout)
#define stdio(in, out)                                                         \
  stdi(in);                                                                    \
  stdo(out)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define MAX 1000007
#define MOD 1000000007
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
// typedef int ittt;
// #define int ll

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

const ll inf = 1e18;

ll hit[MAX];
ll dp[MAX];
int n;

ll solve(int i) {
  if (i <= 1) {
    return 0;
  }

  if (dp[i] != -1)
    return dp[i];

  ll ans = solve(i - 1) + abs(hit[i] - hit[i - 1]);

  if (i - 2 > 0) {
    ans = min(ans, solve(i - 2) + abs(hit[i] - hit[i - 2]));
  }

  return dp[i] = ans;
}

int main() {
  /*#ifndef ONLINE_JUDGE
      stdio("in", "out");
  #endif*/
  fast;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> hit[i];
    dp[i] = -1;
  }

  ll ans = solve(n);
  cout << ans << '\n';

  return 0;
}