Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define lcm(a, b) (a) / __gcd((a), (b)) * (b)
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define maxn 10000000000000
#define endl '\n'
#define trace(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;
int mod(int x, int y) {
  int s;
  s = abs(x) / y;
  if (x < 0)
    s--;
  return x - y * s;
}
int dir8[2][8] = {{1, -1, 0, 0, 1, -1, 1, -1}, {0, 0, 1, -1, 1, -1, -1, 1}};
int dir4[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}};
ll m, n, row, col;
ll memo[100006];
ll ara[100006];
ll dp(int i) {
  if (i == n - 1)
    return 0;
  if (memo[i] != -1)
    return memo[i];
  ll p1 = maxn, p2 = maxn;
  if (i + 1 < n)
    p1 = dp(i + 1) + abs(ara[i] - ara[i + 1]);
  if (i + 2 < n)
    p2 = dp(i + 2) + abs(ara[i] - ara[i + 2]);
  return min(p1, p2);
}
void solve() {
  scanf("%lld", &n);
  for (int w = 0; w < n; w++)
    scanf("%lld", &ara[w]);
  ll ans = dp(0);
  printf("%lld\n", ans);
}
void test() {
  int t;
  // scanf("%d",&t);
  t = 1;
  for (int test = 1; test <= t; test++) {
    memset(memo, -1, sizeof memo);
    solve();
  }
}
int main() {
  test();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define lcm(a, b) (a) / __gcd((a), (b)) * (b)
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define maxn 10000000000000
#define endl '\n'
#define trace(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;
int mod(int x, int y) {
  int s;
  s = abs(x) / y;
  if (x < 0)
    s--;
  return x - y * s;
}
int dir8[2][8] = {{1, -1, 0, 0, 1, -1, 1, -1}, {0, 0, 1, -1, 1, -1, -1, 1}};
int dir4[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}};
ll m, n, row, col;
ll memo[100006];
ll ara[100006];
ll dp(int i) {
  if (i == n - 1)
    return 0;
  if (memo[i] != -1)
    return memo[i];
  ll p1 = maxn, p2 = maxn;
  if (i + 1 < n)
    p1 = dp(i + 1) + abs(ara[i] - ara[i + 1]);
  if (i + 2 < n)
    p2 = dp(i + 2) + abs(ara[i] - ara[i + 2]);
  memo[i] = min(p1, p2);
  return min(p1, p2);
}
void solve() {
  scanf("%lld", &n);
  for (int w = 0; w < n; w++)
    scanf("%lld", &ara[w]);
  ll ans = dp(0);
  printf("%lld\n", ans);
}
void test() {
  int t;
  // scanf("%d",&t);
  t = 1;
  for (int test = 1; test <= t; test++) {
    memset(memo, -1, sizeof memo);
    solve();
  }
}
int main() {
  test();
  return 0;
}