Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
ll mod = 1e9 + 7;

ll a[200005];
ll b[200005];

ll idx[100005][2];
// ll cnt[200005];
ll pre[200005];
// ll end[200005];
map<ll, ll> m;

bool comp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.fi - a.se) > (b.fi - b.se);
}

bool revcomp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.fi) > (b.fi);
}

ll ceil1(ll a, ll b) {
  if (a % b != 0)
    return a / b + 1;
  else
    return a / b;
}
ll power(ll a, ll b, ll n) {
  ll ans = 1;
  while (b > 0) {
    if (b % 2 == 1)
      ans = (ans * a) % n;
    a = (a * a) % n;
    b /= 2;
  }
  return ans;
}

ll count1 = 0;

ll gcd(ll a, ll b) {
  // if(a<b)swap(a,b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll primepos(ll fact, ll p) {
  ll res = 0;
  while (fact > 0) {
    res += fact / p;
    fact /= p;
  }

  return res;
}

// void dfs(ll n, ll p){
//   if(vis[n]!=0){
//     return;
//   }
//   else{
//     vis[n]=1;
//     b[n]=p;
//     for(int i=0;i<v[n].size();i++){
//       dfs(v[n][i],p);
//     }
//   }
// }

int main() {

  ios::sync_with_stdio(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int dp[n + 1];
  memset(dp, 0, sizeof dp);
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (int i = 2; i < n; i++) {
    dp[i] =
        min(dp[i - 1] + abs(a[i] - a[i - 1]), dp[i - 2] + abs(a[i] - a[i - 2]));
  }
  cout << dp[n - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define mp make_pair
ll mod = 1e9 + 7;

ll a[200005];
ll b[200005];

ll idx[100005][2];
// ll cnt[200005];
ll pre[200005];
// ll end[200005];
map<ll, ll> m;

bool comp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.fi - a.se) > (b.fi - b.se);
}

bool revcomp(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.fi) > (b.fi);
}

ll ceil1(ll a, ll b) {
  if (a % b != 0)
    return a / b + 1;
  else
    return a / b;
}
ll power(ll a, ll b, ll n) {
  ll ans = 1;
  while (b > 0) {
    if (b % 2 == 1)
      ans = (ans * a) % n;
    a = (a * a) % n;
    b /= 2;
  }
  return ans;
}

ll count1 = 0;

ll gcd(ll a, ll b) {
  // if(a<b)swap(a,b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll primepos(ll fact, ll p) {
  ll res = 0;
  while (fact > 0) {
    res += fact / p;
    fact /= p;
  }

  return res;
}

// void dfs(ll n, ll p){
//   if(vis[n]!=0){
//     return;
//   }
//   else{
//     vis[n]=1;
//     b[n]=p;
//     for(int i=0;i<v[n].size();i++){
//       dfs(v[n][i],p);
//     }
//   }
// }

int main() {

  ios::sync_with_stdio(0);

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int dp[n + 1];
  memset(dp, 0, sizeof dp);
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (int i = 2; i < n; i++) {
    dp[i] =
        min(dp[i - 1] + abs(a[i] - a[i - 1]), dp[i - 2] + abs(a[i] - a[i - 2]));
  }
  cout << dp[n - 1] << endl;
  return 0;
}