Problem p03160 (C++)

Buggy submission (status: Runtime Error):
/*
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
 |                                                                 |
 |   author : thanos1399     Language : C ++                         |
 |   college : MSIT                                                |
 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
*/
#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define TEST                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define line cout << endl;
#define loop(i, a, b) for (long long int i = a; i < b; i++)
#define loopIn(i, a, b) for (long long int i = a; i <= b; i++)
#define MAX_SIZE 1000000
#define ff first
#define ss second
#define pb push_back
#define ll long long int
#define mp make_pair
#define vt vector<tup>
#define vtt vector<vt>
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
void Sieve(int *prime, int n) {
  for (int i = 0; i < n; i++) {
    prime[i] = 0;
  }
  for (int i = 2; i < n; i++) {
    prime[i] = 1;
  }
  for (int i = 4; i < n; i += 2) {
    prime[i] = 0;
  }
  for (int i = 3; i < n; i++) {
    if (prime[i] == 1) {
      for (int j = i * i; j < n; j += i) {
        prime[j] = 0;
      }
    }
  }
  return;
}
void input() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("/home/yash/Desktop/CPP/input.txt", "r", stdin);
  freopen("/home/yash/Desktop/CPP/output.txt", "w", stdout);
#endif
}
ll inp;
int parent[101];
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}
/*int find(int a)
{
    while(parent[a] > 0)
        a = parent[a];

    return a;
} */

void Union(int a, int b) {
  parent[a] += parent[b]; // adding size of set b to set a
  parent[b] = a;          // making a , parent of new set
}
int find(int node) {
  if (parent[node] < 0)
    return node;

  parent[node] = find(parent[node]);
  return parent[node];
}

int main() {
  input();
  int n;
  cin >> n;
  vi V;
  loop(i, 0, n) {
    cin >> inp;
    V.pb(inp);
  }
  int dp[n];
  dp[0] = 0;
  dp[1] = abs(V[0] - V[1]);
  loop(i, 2, n) {
    int ans1 = dp[i - 1] + abs(V[i] - V[i - 1]);
    int ans2 = dp[i - 2] + abs(V[i] - V[i - 2]);
    dp[i] = min(ans1, ans2);
  }
  cout << dp[n - 1] << endl;

  return 0;
}

Fixed submission:
/*
  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
 |                                                                 |
 |   author : thanos1399     Language : C ++                         |
 |   college : MSIT                                                |
 | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ |
*/
#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define TEST                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define line cout << endl;
#define loop(i, a, b) for (long long int i = a; i < b; i++)
#define loopIn(i, a, b) for (long long int i = a; i <= b; i++)
#define MAX_SIZE 1000000
#define ff first
#define ss second
#define pb push_back
#define ll long long int
#define mp make_pair
#define vt vector<tup>
#define vtt vector<vt>
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
void Sieve(int *prime, int n) {
  for (int i = 0; i < n; i++) {
    prime[i] = 0;
  }
  for (int i = 2; i < n; i++) {
    prime[i] = 1;
  }
  for (int i = 4; i < n; i += 2) {
    prime[i] = 0;
  }
  for (int i = 3; i < n; i++) {
    if (prime[i] == 1) {
      for (int j = i * i; j < n; j += i) {
        prime[j] = 0;
      }
    }
  }
  return;
}
void input() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("/home/yash/Desktop/CPP/input.txt", "r", stdin);
  freopen("/home/yash/Desktop/CPP/output.txt", "w", stdout);
#endif
}
ll inp;
int parent[101];
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}
/*int find(int a)
{
    while(parent[a] > 0)
        a = parent[a];

    return a;
} */

void Union(int a, int b) {
  parent[a] += parent[b]; // adding size of set b to set a
  parent[b] = a;          // making a , parent of new set
}
int find(int node) {
  if (parent[node] < 0)
    return node;

  parent[node] = find(parent[node]);
  return parent[node];
}

int main() {
  // input();
  int n;
  cin >> n;
  vi V;
  loop(i, 0, n) {
    cin >> inp;
    V.pb(inp);
  }
  int dp[n];
  dp[0] = 0;
  dp[1] = abs(V[0] - V[1]);
  loop(i, 2, n) {
    int ans1 = dp[i - 1] + abs(V[i] - V[i - 1]);
    int ans2 = dp[i - 2] + abs(V[i] - V[i - 2]);
    dp[i] = min(ans1, ans2);
  }
  cout << dp[n - 1] << endl;

  return 0;
}