Problem p03160 (C++)

Buggy submission (status: Runtime Error):
//////////////////////////////////////
////       ABHINASH GIRI          ////
/////////////////////////////////////
#include <bits/stdc++.h>
using namespace std;
#define pr 4294967296 // 2^32
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr)
#define high 1000000001
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf -1e18
#define pinf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define e 1000000000
#define w(x)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define co(x) cout << (x) << "\n"
#define endl "\n"
typedef unsigned long int ul;
typedef unsigned long long int ull;
typedef unsigned long long int li;
typedef vector<int> vi;
typedef vector<long long int> vll;
typedef vector<long int> vl;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<long int, long int>> vpl;
typedef vector<pair<long long int, long long int>> vpll;
typedef long long ll;
bool cmp(pair<char, int> a, pair<char, int> b) {
  if (a.first == b.first)
    return a.second < b.second;
  return a.first > b.first;
}

ll power(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  else {
    ll res = power(a, b / 2);
    if (b % 2) {
      return (res * res * (a));
    } else {
      return (res * res);
    }
  }
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll cost(vl &v, int n, vll dp) {
  // cout<< " solving for n="<<n<<endl;
  if (n == 1) {
    return 0;
  }
  if (n == 2) {
    return abs(v[2] - v[1]);
  }
  ll a, b;
  if (!dp[n - 1]) {
    a = cost(v, n - 1, dp);
    dp[n - 1] = a;
  } else
    a = dp[n - 1];
  if (!dp[n - 2]) {
    b = cost(v, n - 2, dp);
    dp[n - 2] = b;
  } else
    b = dp[n - 2];

  return min(a + abs(v[n] - v[n - 1]), b + abs(v[n] - v[n - 2]));
}
int main() {
  std::chrono::time_point<std::chrono::high_resolution_clock> start_timer,
      end_timer;
  start_timer = std::chrono::high_resolution_clock::now();
  bool show_time = false; //  change it to true when needed;
                          //    #ifndef ONLINE_JUDGE
                          //    freopen("input.txt", "r", stdin);
                          //    freopen("output.txt", "w", stdout);
                          //    #endif

  /* Cook your Code  */
  fast;
  int n;
  cin >> n;
  vl v(n + 1, 0);
  v[0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
  }
  vll dp(n + 1, 0);

  cout << cost(v, n, dp) << endl;
  end_timer = std::chrono::high_resolution_clock::now();
  ll elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(
                        end_timer - start_timer)
                        .count();
  if (show_time) {
    cout << "\\nElapsed Time: " << elapsed_time << "ms\\n";
  }

  return 0;
}

Fixed submission:
//////////////////////////////////////
////       ABHINASH GIRI          ////
/////////////////////////////////////
#include <bits/stdc++.h>
using namespace std;
#define pr 4294967296 // 2^32
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr)
#define high 1000000001
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf -1e18
#define pinf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define e 1000000000
#define w(x)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define co(x) cout << (x) << "\n"
#define endl "\n"
typedef unsigned long int ul;
typedef unsigned long long int ull;
typedef unsigned long long int li;
typedef vector<int> vi;
typedef vector<long long int> vll;
typedef vector<long int> vl;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<long int, long int>> vpl;
typedef vector<pair<long long int, long long int>> vpll;
typedef long long ll;
bool cmp(pair<char, int> a, pair<char, int> b) {
  if (a.first == b.first)
    return a.second < b.second;
  return a.first > b.first;
}

ll power(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  else {
    ll res = power(a, b / 2);
    if (b % 2) {
      return (res * res * (a));
    } else {
      return (res * res);
    }
  }
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll cost(vl &v, int n, vll dp) {
  // cout<< " solving for n="<<n<<endl;
  if (n == 1) {
    return 0;
  }
  if (n == 2) {
    return abs(v[2] - v[1]);
  }
  ll a, b;
  if (!dp[n - 1]) {
    a = cost(v, n - 1, dp);
    dp[n - 1] = a;
  } else
    a = dp[n - 1];
  if (!dp[n - 2]) {
    b = cost(v, n - 2, dp);
    dp[n - 2] = b;
  } else
    b = dp[n - 2];

  return min(a + abs(v[n] - v[n - 1]), b + abs(v[n] - v[n - 2]));
}
int main() {
  std::chrono::time_point<std::chrono::high_resolution_clock> start_timer,
      end_timer;
  start_timer = std::chrono::high_resolution_clock::now();
  bool show_time = false; //  change it to true when needed;
                          //    #ifndef ONLINE_JUDGE
                          //    freopen("input.txt", "r", stdin);
                          //    freopen("output.txt", "w", stdout);
                          //    #endif

  /* Cook your Code  */
  fast;
  int n;
  cin >> n;
  vl v(n + 1, 0);
  v[0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
  }
  vll dp(n + 1, 0);
  dp[2] = abs(v[2] - v[1]);
  for (int j = 3; j <= n; ++j) {
    dp[j] =
        min(dp[j - 1] + abs(v[j] - v[j - 1]), dp[j - 2] + abs(v[j] - v[j - 2]));
  }
  cout << dp[n] << endl;
  end_timer = std::chrono::high_resolution_clock::now();
  ll elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(
                        end_timer - start_timer)
                        .count();
  if (show_time) {
    cout << "\\nElapsed Time: " << elapsed_time << "ms\\n";
  }

  return 0;
}