Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define pll pair<long long, long long>
#define vll vector<long long>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define deb(x) cout << #x << "=" << x << endl
#define F(i, a, b) for (ll i = a; i <= b; i++)
#define RF(i, a, b) for (ll i = a; i >= b; i--)
#define in(a, n) F(i, 0, n - 1) cin >> a[i]
#define vi vector<int>
#define out(a, n)                                                              \
  F(i, 0, n - 1) cout << a[i] << " ";                                          \
  cout << endl
#define mod 1000000007
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl
#define int long long

const int N = (int)1e5 + 5;

int dp[N];
int a[N];

int n;
int solve(int idx) {
  int i = idx;
  if (idx == n)
    return 0;

  int &ans = dp[idx];

  ans = INT_MAX;
  int op1 = INT_MAX, op2 = INT_MAX;
  if (idx + 2 <= n)
    op1 = abs(a[i + 2] - a[i]) + solve(idx + 2);

  op2 = abs(a[i + 1] - a[i]) + solve(idx + 1);

  return ans = min(op1, op2);
}

void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  memset(dp, -1, sizeof dp);
  int ans = solve(1);
  cout << ans;
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define pll pair<long long, long long>
#define vll vector<long long>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define deb(x) cout << #x << "=" << x << endl
#define F(i, a, b) for (ll i = a; i <= b; i++)
#define RF(i, a, b) for (ll i = a; i >= b; i--)
#define in(a, n) F(i, 0, n - 1) cin >> a[i]
#define vi vector<int>
#define out(a, n)                                                              \
  F(i, 0, n - 1) cout << a[i] << " ";                                          \
  cout << endl
#define mod 1000000007
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl
#define int long long

const int N = (int)1e5 + 5;

int dp[N];
int a[N];

int n;
int solve(int idx) {
  int i = idx;
  if (idx == n)
    return 0;

  int &ans = dp[idx];
  if (ans != -1)
    return ans;
  ans = INT_MAX;
  int op1 = INT_MAX, op2 = INT_MAX;
  if (idx + 2 <= n)
    op1 = abs(a[i + 2] - a[i]) + solve(idx + 2);

  op2 = abs(a[i + 1] - a[i]) + solve(idx + 1);

  return ans = min(op1, op2);
}

void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  memset(dp, -1, sizeof dp);
  int ans = solve(1);
  cout << ans;
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}