Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// Charan Sriramula

#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long int ll;

const ll lg = 14;
const ll N = 5e3 + 5;
const ll INF = 1e9;
const ll mod = 1e9 + 7;
const double PI = 3.14159265358979323846;

#define ints(n) scanf("%d", &n)
#define intp(n) printf("%d\n", n)
#define longs(n) scanf("%lld", &n)
#define longp(n) printf("%lld\n", n)

#define f first
#define s second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x, y) make_pair(x, y)
#define GCD(a, b) __gcd((a), (b))
#define all(v) v.begin(), v.end()
#define bits(x) __builtin_popcount(x)
#define LCM(a, b) ((a) * (b)) / GCD((a), (b))
#define ms(dp, val) memset(dp, val, sizeof(dp))
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
template <typename T> T power(T x, T y, ll m) {
  T ans = 1;
  while (y > 0) {
    if (y & 1LL)
      ans = (ans * x) % m;
    y >>= 1LL;
    x = (x * x) % m;
  }
  return ans % m;
}

ll n, a[N], dp[N][2];

int main() {
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  dp[1][0] = 0;
  dp[1][1] = INF;
  dp[2][0] = abs(a[2] - a[1]);
  dp[2][1] = INF;
  for (ll i = 3; i <= n; i++) {
    dp[i][0] = abs(a[i] - a[i - 1]) + min(dp[i - 1][0], dp[i - 1][1]);
    dp[i][1] = abs(a[i] - a[i - 2]) + min(dp[i - 2][1], dp[i - 2][0]);
  }
  // cout<<endl;
  // for(ll i=1;i<=n;i++){cout<<dp[i][0]<<" "<<dp[i][1]<<endl;}
  // cout<<endl;
  cout << min(dp[n][0], dp[n][1]) << endl;
}


Fixed submission:
// Charan Sriramula

#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long int ll;

const ll lg = 14;
const ll N = 5e5 + 5;
const ll INF = 1e9;
const ll mod = 1e9 + 7;
const double PI = 3.14159265358979323846;

#define ints(n) scanf("%d", &n)
#define intp(n) printf("%d\n", n)
#define longs(n) scanf("%lld", &n)
#define longp(n) printf("%lld\n", n)

#define f first
#define s second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define mp(x, y) make_pair(x, y)
#define GCD(a, b) __gcd((a), (b))
#define all(v) v.begin(), v.end()
#define bits(x) __builtin_popcount(x)
#define LCM(a, b) ((a) * (b)) / GCD((a), (b))
#define ms(dp, val) memset(dp, val, sizeof(dp))
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
template <typename T> T power(T x, T y, ll m) {
  T ans = 1;
  while (y > 0) {
    if (y & 1LL)
      ans = (ans * x) % m;
    y >>= 1LL;
    x = (x * x) % m;
  }
  return ans % m;
}

ll n, a[N], dp[N][2];

int main() {
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  dp[1][0] = 0;
  dp[1][1] = INF;
  dp[2][0] = abs(a[2] - a[1]);
  dp[2][1] = INF;
  for (ll i = 3; i <= n; i++) {
    dp[i][0] = abs(a[i] - a[i - 1]) + min(dp[i - 1][0], dp[i - 1][1]);
    dp[i][1] = abs(a[i] - a[i - 2]) + min(dp[i - 2][1], dp[i - 2][0]);
  }
  // cout<<endl;
  // for(ll i=1;i<=n;i++){cout<<dp[i][0]<<" "<<dp[i][1]<<endl;}
  // cout<<endl;
  cout << min(dp[n][0], dp[n][1]) << endl;
}