Problem p03160 (C++)

Buggy submission (status: Runtime Error):
/********************************************/ /**
                                                ****** Author: Shivam Kejriwal
                                                ******************
                                                ****** Handle: techno_phyle
                                                ******************
                                                ***********************************************/

#include <bits/stdc++.h>
using namespace std;

#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
typedef long long ll;

#define mod 1000000007
#define N 100010
#define M 200010
#define db(a) cout << a << endl
#define db2(a, b) cout << a << " " << b << endl
#define dbp(a) cout << a.first << " " << a.second << endl
#define adb(a)                                                                 \
  for (auto it : a)                                                            \
    cout << it << " ";                                                         \
  cout << endl
#define adbp(a)                                                                \
  for (auto it : a)                                                            \
  cout << it.first << " " << it.second << endl
#define pb push_back
#define mp make_pair
typedef pair<double, long> key;

struct comp {
  bool operator()(pair<int, int> p1, pair<int, int> p2) {
    if (p1.first < p2.first)
      return false;
    if (p2.first < p1.first)
      return true;
    return p1.second < p2.second;
  };
};
// greater<pair<int,int> , greater<int>>
int main() {
  fastIO;
#ifndef ONLINE_JUDGE
  freopen("../input.txt", "r", stdin);
  freopen("../output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int dp[n + 1];
  dp[1] = 0;
  dp[2] = abs(a[2] - a[1]);
  for (int i = 3; i <= n; i++) {
    dp[i] =
        min(abs(a[i] - a[i - 1]) + dp[i - 1], abs(a[i] - a[i - 2]) + dp[i - 2]);
  }
  cout << dp[n] << endl;
  return 0;
}


Fixed submission:
/********************************************/ /**
                                                ****** Author: Shivam Kejriwal
                                                ******************
                                                ****** Handle: techno_phyle
                                                ******************
                                                ***********************************************/

#include <bits/stdc++.h>
using namespace std;

#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
typedef long long ll;

#define mod 1000000007
#define N 100010
#define M 200010
#define db(a) cout << a << endl
#define db2(a, b) cout << a << " " << b << endl
#define dbp(a) cout << a.first << " " << a.second << endl
#define adb(a)                                                                 \
  for (auto it : a)                                                            \
    cout << it << " ";                                                         \
  cout << endl
#define adbp(a)                                                                \
  for (auto it : a)                                                            \
  cout << it.first << " " << it.second << endl
#define pb push_back
#define mp make_pair
typedef pair<double, long> key;

struct comp {
  bool operator()(pair<int, int> p1, pair<int, int> p2) {
    if (p1.first < p2.first)
      return false;
    if (p2.first < p1.first)
      return true;
    return p1.second < p2.second;
  };
};
// greater<pair<int,int> , greater<int>>
int main() {
  fastIO;
  // #ifndef ONLINE_JUDGE
  //     freopen("../input.txt", "r", stdin);
  //     freopen("../output.txt","w",stdout);
  // #endif
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int dp[n + 1];
  dp[1] = 0;
  dp[2] = abs(a[2] - a[1]);
  for (int i = 3; i <= n; i++) {
    dp[i] =
        min(abs(a[i] - a[i - 1]) + dp[i - 1], abs(a[i] - a[i - 2]) + dp[i - 2]);
  }
  cout << dp[n] << endl;
  return 0;
}