Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// STRIVER_79
// Lets goto the next level
// AIM 6* at CC and CM at CF *__*

#include <algorithm>
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define mod (int)(1e9 + 21)
#define all(a) a.begin(), a.end()
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define bloop(i, a, b) for (int i = a; i >= b; i--)
#define tc(t)                                                                  \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define int long long
#define ll long long
#define pb emplace_back
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL)
#define in(x) scanf("%intd", &x)
#define rr return 0
#define prec(n) fixed << setprecision(n)
#define maxheap priority_queue<int>
#define minheap priority_queue<int, vector<int>, greater<int>>
#define inf (int)(1e18)
#define ini(a, i) memset(a, i, sizeof(a))
#define fi first
#define se second
#define kitna se
#define endl "\n"
#define pi pair<int, int>
#define LL long long
const int MAXN = (int)((1e5) + 10);
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}
bool isPrime(int N) {
  for (int i = 2; i * i <= N; ++i) {
    if (N % i == 0)
      return false;
  }
  return true;
}
int cbrt(int x) {
  int lo = 1, hi = min(2000000ll, x);
  while (hi - lo > 1) {
    int mid = (lo + hi) / 2;
    if (mid * mid * mid < x) {
      lo = mid;
    } else
      hi = mid;
  }
  if (hi * hi * hi <= x)
    return hi;
  else
    return lo;
}
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const int nax = (int)(1 * 1e5 + 10);

// int SPF[N+5];

// void SOF()
// {
//     SPF[1] = 1;
//     for (int i=2; i<N; i++)
//         SPF[i] = i;
//     for (int i=4; i<N; i+=2)
//         SPF[i] = 2;

//     for (int i=3; i*i<N; i++)
//         {
//         if (SPF[i] == i)
//             {
//             for (int kon=i*i; kon<N; kon+=i)
//                 if (SPF[kon]==kon)
//                    SPF[kon] = i;
//             }
//         }
// }
int a[nax];
int n;
int dp[nax];
int func(int ind) {
  if (ind == (n - 1))
    return 0;
  else if (dp[ind] != -1)
    return dp[ind];
  else {
    int ans = 1e9 + 5;
    for (int i = 1; i <= 2; i++) {
      if (ind + i < n)
        ans = min(ans, abs(a[ind + i] - a[ind]) + func(ind + i));
    }
    return dp[ind] = ans;
  }
}

signed main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
// for writing output to output.txt
// freopen("output.txt", "w", stdout);
#endif
  fio;
  int t = 1;
  // cin >> t;
  //  string s;
  //  getline(cin,s);

  while (t--) {
    cin >> n;
    for0(i, n) cin >> a[i];
    ini(dp, -1);
    cout << func(0);
  }

  rr;
}

Fixed submission:
// STRIVER_79
// Lets goto the next level
// AIM 6* at CC and CM at CF *__*

#include <algorithm>
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define mod (int)(1e9 + 21)
#define all(a) a.begin(), a.end()
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define bloop(i, a, b) for (int i = a; i >= b; i--)
#define tc(t)                                                                  \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define int long long
#define ll long long
#define pb emplace_back
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL)
#define in(x) scanf("%intd", &x)
#define rr return 0
#define prec(n) fixed << setprecision(n)
#define maxheap priority_queue<int>
#define minheap priority_queue<int, vector<int>, greater<int>>
#define inf (int)(1e18)
#define ini(a, i) memset(a, i, sizeof(a))
#define fi first
#define se second
#define kitna se
#define endl "\n"
#define pi pair<int, int>
#define LL long long
const int MAXN = (int)((1e5) + 10);
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}
bool isPrime(int N) {
  for (int i = 2; i * i <= N; ++i) {
    if (N % i == 0)
      return false;
  }
  return true;
}
int cbrt(int x) {
  int lo = 1, hi = min(2000000ll, x);
  while (hi - lo > 1) {
    int mid = (lo + hi) / 2;
    if (mid * mid * mid < x) {
      lo = mid;
    } else
      hi = mid;
  }
  if (hi * hi * hi <= x)
    return hi;
  else
    return lo;
}
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const int nax = (int)(1 * 1e5 + 10);

// int SPF[N+5];

// void SOF()
// {
//     SPF[1] = 1;
//     for (int i=2; i<N; i++)
//         SPF[i] = i;
//     for (int i=4; i<N; i+=2)
//         SPF[i] = 2;

//     for (int i=3; i*i<N; i++)
//         {
//         if (SPF[i] == i)
//             {
//             for (int kon=i*i; kon<N; kon+=i)
//                 if (SPF[kon]==kon)
//                    SPF[kon] = i;
//             }
//         }
// }
int a[nax];
int n;
int dp[nax];
int func(int ind) {
  if (ind == (n - 1))
    return 0;
  else if (dp[ind] != -1)
    return dp[ind];
  else {
    int ans = 1e9 + 5;
    for (int i = 1; i <= 2; i++) {
      if (ind + i < n)
        ans = min(ans, abs(a[ind + i] - a[ind]) + func(ind + i));
    }
    return dp[ind] = ans;
  }
}

signed main() {

  int t = 1;
  // cin >> t;
  //  string s;
  //  getline(cin,s);

  while (t--) {
    cin >> n;
    for0(i, n) cin >> a[i];
    ini(dp, -1);
    cout << func(0);
  }

  rr;
}