Problem p03160 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp> // Common file

using namespace __gnu_pbds;   //policy-based data structures (like set with
adttional functionality) some operations
//(*name.find_by_order(x) it givesyou value of x index in sorted set and
name.find_by_key(no) it gives you the number at index no before sorting)

 typedef
tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
pbds_s;




/* OHH..... BHAI MAARO... MUJHE MAARO.... JO MEIN ISS.. MAADHERCHOD CONTEST MEIN
AAYA...*/

using namespace std;

#define ull unsigned long long
#define ld long double
#define PI 3.1415926535897932384626433832795
#define substring                                                              \
  s.substr(form_index, to_which_index) // last index is not included
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long int
#define deque                                                                  \
  deque<int> d; // dynamic array with push_front() ans pop_front functionality
#define pq priority_queue<int> p; // max_heap stored data in decresing order
#define rpq                                                                    \
  priority_queue<int, vector<int>, greater<int>>                               \
      p; // min_heap in increasing order
#define decimalupto(no, y) fixed << setprecision(y) << no
#define b_to_d(no)                                                             \
  bitset<32> ans("no");                                                        \
  ans.to_ullong(); // when you have to make an array of binary array
#define d_to_b(no, upto)                                                       \
  bitset<upto> ans(no);                                                        \
  ans.to_string(); // then you use bitset<int> name.And you can use operations
                   // (a&b),(a|b),(a^b)
#define inf 1e9 + 5
#define mod 1e9 + 7 // 1e9+7
#define count1(no)                                                             \
  __builtin_popcountll(no) // tells us the number of setbits means the total no.
                           // of 1 in a binary no.
#define count0_front(no)                                                       \
  __builtin_clz(no) // tells us the number of 0s in a binary no. from begiining
#define count0_back(no)                                                        \
  __builtin_ctz(no) // tells us the number of 0s from the count0_back
#define parity(no)                                                             \
  __builtin_parity(                                                            \
      no) // tells the number is even or odd by counting the number of 1s
#define Erase(i)                                                               \
  myvector.erase(myvector.begin() + i); // erase a specific element from a
                                        // vector
#define grp_Erase(i)                                                           \
  myvector.erase(myvector.begin(),                                             \
                 myvector.begin() +                                            \
                     i); // erase the first i elemnts from a vector
#define copy array copy(arr1.begin(), arr1.begin() + n, arr2.begin());
#define to_uppercase(s) transform(s.begin(), s.end(), s.begin(), ::toupper);
#define to_lowercase(s) transform(s.begin(), s.end(), s.begin(), ::tolower);
#define gcd(a, b) __gcd(a, b)
#define array_to_string to_string(i) or string.push_back(arr[i] + '0')
#define string_to_int s[i] - 'a'
ll power(ll x, ll y, ll md = mod) {
  ll res = 1;
  x %= md;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % md;
    x *= x;
    if (x >= md)
      x %= md;
    y >>= 1;
  }
  return res;
}

// #define left_operand(x<<y)    is equivalent to (x*2^y)
// #define right_operand(x>>y)   is equivalent to (x/2^y)

// int arr[100][100];

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  vector<int> dp(n, inf);
  int sum = 0;
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j : {i + 1, i + 2}) {
      if (j < n) {
        dp[j] = min(dp[j], dp[i] + abs(arr[j] - arr[i]));
      }
    }
  }
  cout << dp[n - 1];
}


Fixed submission:

#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp> // Common file

using namespace __gnu_pbds;   //policy-based data structures (like set with
adttional functionality) some operations
//(*name.find_by_order(x) it givesyou value of x index in sorted set and
name.find_by_key(no) it gives you the number at index no before sorting)

 typedef
tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
pbds_s;




/* OHH..... BHAI MAARO... MUJHE MAARO.... JO MEIN ISS.. MAADHERCHOD CONTEST MEIN
AAYA...*/

using namespace std;

#define ull unsigned long long
#define ld long double
#define PI 3.1415926535897932384626433832795
#define substring                                                              \
  s.substr(form_index, to_which_index) // last index is not included
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long int
#define deque                                                                  \
  deque<int> d; // dynamic array with push_front() ans pop_front functionality
#define pq priority_queue<int> p; // max_heap stored data in decresing order
#define rpq                                                                    \
  priority_queue<int, vector<int>, greater<int>>                               \
      p; // min_heap in increasing order
#define decimalupto(no, y) fixed << setprecision(y) << no
#define b_to_d(no)                                                             \
  bitset<32> ans("no");                                                        \
  ans.to_ullong(); // when you have to make an array of binary array
#define d_to_b(no, upto)                                                       \
  bitset<upto> ans(no);                                                        \
  ans.to_string(); // then you use bitset<int> name.And you can use operations
                   // (a&b),(a|b),(a^b)
#define inf 1e9 + 5
#define mod 1e9 + 7 // 1e9+7
#define count1(no)                                                             \
  __builtin_popcountll(no) // tells us the number of setbits means the total no.
                           // of 1 in a binary no.
#define count0_front(no)                                                       \
  __builtin_clz(no) // tells us the number of 0s in a binary no. from begiining
#define count0_back(no)                                                        \
  __builtin_ctz(no) // tells us the number of 0s from the count0_back
#define parity(no)                                                             \
  __builtin_parity(                                                            \
      no) // tells the number is even or odd by counting the number of 1s
#define Erase(i)                                                               \
  myvector.erase(myvector.begin() + i); // erase a specific element from a
                                        // vector
#define grp_Erase(i)                                                           \
  myvector.erase(myvector.begin(),                                             \
                 myvector.begin() +                                            \
                     i); // erase the first i elemnts from a vector
#define copy array copy(arr1.begin(), arr1.begin() + n, arr2.begin());
#define to_uppercase(s) transform(s.begin(), s.end(), s.begin(), ::toupper);
#define to_lowercase(s) transform(s.begin(), s.end(), s.begin(), ::tolower);
#define gcd(a, b) __gcd(a, b)
#define array_to_string to_string(i) or string.push_back(arr[i] + '0')
#define string_to_int s[i] - 'a'
ll power(ll x, ll y, ll md = mod) {
  ll res = 1;
  x %= md;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % md;
    x *= x;
    if (x >= md)
      x %= md;
    y >>= 1;
  }
  return res;
}

// #define left_operand(x<<y)    is equivalent to (x*2^y)
// #define right_operand(x>>y)   is equivalent to (x/2^y)

// int arr[100][100];

int main() {

  fastio int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  vector<int> dp(n, inf);
  int sum = 0;
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j : {i + 1, i + 2}) {
      if (j < n) {
        dp[j] = min(dp[j], dp[i] + abs(arr[j] - arr[i]));
      }
    }
  }
  cout << dp[n - 1];
}