prithivMLmods/Coder-Stat huggingface dataset:
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);
#define mp make_pair
#define pb push_back
const int mod = 1e9 + 7;
bool isPrime(int n) {
  for (int i = 2; i < n; i++)
    if (n % i == 0)
      return false;
  return true;
}
struct cmp {
  bool operator()(const pair<int, int> &a, const pair<int, int> &b) {
    int len1 = a.second - a.first + 1;
    int len2 = b.second - b.first + 1;
    if (len1 != len2)
      return len1 > len2;
    return a.first < b.first;
  }
};
bool comp(pair<pair<int, int>, int> &a, pair<pair<int, int>, int> &b) {
  int diff1 = abs(a.first.first - a.first.second);
  int diff2 = abs(b.first.first - b.first.second);

  if (diff1 > diff2)
    return true;
  return false;
  // return (a.first.second > b.first.second);
}
void findAns() {
  int n;
  cin >> n;

  int height[n];
  for (auto &it : height)
    cin >> it;

  int dp[n];

  dp[0] = 0;
  dp[1] = abs(height[0] - height[1]);

  for (int i = 2; i < n; i++) {
    dp[i] = min(dp[i - 1] + abs(height[i - 1] - height[i]),
                dp[i - 2] + abs(height[i - 2] - height[i]));
  }
  cout << dp[n - 1];
  return;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio;
  // ll int t;
  // cin >> t;

  // while (t--)
  findAns();

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);
#define mp make_pair
#define pb push_back
const int mod = 1e9 + 7;
bool isPrime(int n) {
  for (int i = 2; i < n; i++)
    if (n % i == 0)
      return false;
  return true;
}
struct cmp {
  bool operator()(const pair<int, int> &a, const pair<int, int> &b) {
    int len1 = a.second - a.first + 1;
    int len2 = b.second - b.first + 1;
    if (len1 != len2)
      return len1 > len2;
    return a.first < b.first;
  }
};
bool comp(pair<pair<int, int>, int> &a, pair<pair<int, int>, int> &b) {
  int diff1 = abs(a.first.first - a.first.second);
  int diff2 = abs(b.first.first - b.first.second);

  if (diff1 > diff2)
    return true;
  return false;
  // return (a.first.second > b.first.second);
}
void findAns() {
  int n;
  cin >> n;

  int height[n];
  for (auto &it : height)
    cin >> it;

  int dp[n];

  dp[0] = 0;
  dp[1] = abs(height[0] - height[1]);

  for (int i = 2; i < n; i++) {
    dp[i] = min(dp[i - 1] + abs(height[i - 1] - height[i]),
                dp[i - 2] + abs(height[i - 2] - height[i]));
  }
  cout << dp[n - 1];
  return;
}
int main() {

  fastio;
  // ll int t;
  // cin >> t;

  // while (t--)
  findAns();

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <iostream>
#define mp make_pair

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, LL> pil;

const int MAXN = 1e5 + 7;

template <typename T> inline T read() {
  T res = 0, flag = 1;
  char in = getchar();
  while (!isdigit(in)) {
    if (in == '-')
      flag = -1;
    in = getchar();
  }
  while (isdigit(in)) {
    res = (res << 1) + (res << 3) + in - '0';
    in = getchar();
  }
  return res * flag;
}

template <typename T> inline void chkmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <typename T> inline void chkmin(T &a, T b) {
  if (a > b)
    a = b;
}

int dp[MAXN], h[MAXN], n;

inline void solve() {
  n = read<int>();
  for (int i = 1; i <= n; ++i)
    h[i] = read<int>();
  memset(dp, 0x3f, sizeof dp);
  dp[0] = dp[1] = 0;
  for (int i = 2; i <= n; ++i) {
    chkmin(dp[i], dp[i - 1] + abs(h[i] - h[i - 1]));
    if (i - 2)
      chkmin(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
  }
  printf("%d\n", dp[n]);
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("A.in", "r", stdin);
  freopen("A.out", "w", stdout);
#endif

  solve();

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <iostream>
#define mp make_pair

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, LL> pil;

const int MAXN = 1e5 + 7;

template <typename T> inline T read() {
  T res = 0, flag = 1;
  char in = getchar();
  while (!isdigit(in)) {
    if (in == '-')
      flag = -1;
    in = getchar();
  }
  while (isdigit(in)) {
    res = (res << 1) + (res << 3) + in - '0';
    in = getchar();
  }
  return res * flag;
}

template <typename T> inline void chkmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <typename T> inline void chkmin(T &a, T b) {
  if (a > b)
    a = b;
}

int dp[MAXN], h[MAXN], n;

inline void solve() {
  n = read<int>();
  for (int i = 1; i <= n; ++i)
    h[i] = read<int>();
  memset(dp, 0x3f, sizeof dp);
  dp[0] = dp[1] = 0;
  for (int i = 2; i <= n; ++i) {
    chkmin(dp[i], dp[i - 1] + abs(h[i] - h[i - 1]));
    if (i - 2)
      chkmin(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
  }
  printf("%d\n", dp[n]);
}

int main() {

  solve();

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

const int INF = 1e9 + 5;

int main() {
  int n;
  int h[10010];

  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> h[i];

  vector<int> dp(n, INF);
  dp[0] = 0;

  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j <= i + 2; j++) {
      if (j < n) {
        dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
      }
    }
  }
  cout << dp[n - 1] << "\n";

  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

const int INF = 1e9 + 5;

int main() {
  int n;
  int h[100010];

  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> h[i];

  vector<int> dp(n, INF);
  dp[0] = 0;

  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j <= i + 2; j++) {
      if (j < n) {
        dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
      }
    }
  }
  cout << dp[n - 1] << "\n";

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int dp[10005];
  int h[10005];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);
  for (int i = 2; i <= n; i++) {
    dp[i] =
        min(abs(h[i] - h[i - 1]) + dp[i - 1], abs(h[i] - h[i - 2]) + dp[i - 2]);
  }
  cout << dp[n - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int dp[100005];
  int h[100005];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);
  for (int i = 2; i <= n; i++) {
    dp[i] =
        min(abs(h[i] - h[i - 1]) + dp[i - 1], abs(h[i] - h[i - 2]) + dp[i - 2]);
  }
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);

  int n;
  cin >> n;
  int a[n];
  int dp[n];

  for (int i = 0; i < n; i++)
    cin >> a[i];

  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (int i = 2; i < n; i++)
    dp[i] =
        min(dp[i - 2] + abs(a[i - 2] - a[i]), dp[i - 1] + abs(a[i - 1] - a[i]));

  cout << dp[n - 1];
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  ios_base::sync_with_stdio(false);

  int n;
  cin >> n;
  int a[n];
  int dp[n];

  for (int i = 0; i < n; i++)
    cin >> a[i];

  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (int i = 2; i < n; i++)
    dp[i] =
        min(dp[i - 2] + abs(a[i - 2] - a[i]), dp[i - 1] + abs(a[i - 1] - a[i]));

  cout << dp[n - 1];
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define reps(i, x) for (ll i = 1; i <= (ll)(x); i++)
#define rrep(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define rreps(i, x) for (ll i = (ll)(x); i > 0; i--)
#define all(x) (x).begin(), (x).end()
#define m0(x) memset(x, 0, sizeof(x))
#define vll vector<ll>
#define vi vector<int>
#define vpll vector<pair<ll, ll>>
#define vpi vector<pair<int, int>>
#define mod 1000000007
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using namespace std;
int h[10000];
int dp[10001];
int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> h[i];
  int INF = 1 << 30;
  reps(i, n) dp[i] = INF;
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < n; i++) {
    chmin(dp[i], dp[i - 1] + abs(h[i - 1] - h[i]));
    chmin(dp[i], dp[i - 2] + abs(h[i - 2] - h[i]));
  }
  cout << dp[n - 1] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define reps(i, x) for (ll i = 1; i <= (ll)(x); i++)
#define rrep(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define rreps(i, x) for (ll i = (ll)(x); i > 0; i--)
#define all(x) (x).begin(), (x).end()
#define m0(x) memset(x, 0, sizeof(x))
#define vll vector<ll>
#define vi vector<int>
#define vpll vector<pair<ll, ll>>
#define vpi vector<pair<int, int>>
#define mod 1000000007
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using namespace std;
int h[100000];
int dp[100001];
int main() {
  int n;
  cin >> n;
  rep(i, n) cin >> h[i];
  int INF = 1 << 30;
  reps(i, n) dp[i] = INF;
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < n; i++) {
    chmin(dp[i], dp[i - 1] + abs(h[i - 1] - h[i]));
    chmin(dp[i], dp[i - 2] + abs(h[i - 2] - h[i]));
  }
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {

  int num_stones;
  vector<int> height(10000);
  vector<int> cost(10000);

  cin >> num_stones;
  for (int i = 0; i < num_stones; i++)
    cin >> height[i];

  cost[0] = 0;
  cost[1] = abs(height[1] - height[0]);

  for (int i = 2; i < num_stones; i++) {
    cost[i] = min(cost[i - 1] + abs(height[i - 1] - height[i]),
                  cost[i - 2] + abs(height[i - 2] - height[i]));
  }

  cout << cost[num_stones - 1];

  return 0;
}

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

int main() {

  int num_stones;
  vector<int> height(1000000);
  vector<int> cost(1000000);

  cin >> num_stones;
  for (int i = 0; i < num_stones; i++)
    cin >> height[i];

  cost[0] = 0;
  cost[1] = abs(height[1] - height[0]);

  for (int i = 2; i < num_stones; i++) {
    cost[i] = min(cost[i - 1] + abs(height[i - 1] - height[i]),
                  cost[i - 2] + abs(height[i - 2] - height[i]));
  }

  cout << cost[num_stones - 1];

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> ht(n, 0);
  for (int i = 0; i < n; ++i) {
    cin >> ht[i];
  }
  std::vector<int> cost(n);
  cost[0] = 0;
  cost[1] = abs(ht[0] - ht[1]);
  for (int i = 2; i < n; ++i) {
    cost[i] = min(abs(ht[i] - ht[i - 2]) + cost[i - 2],
                  abs(ht[i] - ht[i - 1]) + cost[i - 1]);
  }
  cout << cost[n - 1];
}
int main() {

#ifndef ONLINE_JUDGE
  freopen("input.text", "r", stdin);
  freopen("output.text", "w", stdout);
#endif
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> ht(n, 0);
  for (int i = 0; i < n; ++i) {
    cin >> ht[i];
  }
  std::vector<int> cost(n);
  cost[0] = 0;
  cost[1] = abs(ht[0] - ht[1]);
  for (int i = 2; i < n; ++i) {
    cost[i] = min(abs(ht[i] - ht[i - 2]) + cost[i - 2],
                  abs(ht[i] - ht[i - 1]) + cost[i - 1]);
  }
  cout << cost[n - 1];
}
int main() {

  solve();
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// JAI SHREE RAM
typedef long long int ll;
typedef unsigned long long int ull;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define FOR(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define sz(a) int((a).size())
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define loop for (int i = 0; i < n; i++)
#define max3(a, b, c) max(a, max(b, c))
#define max4(a, b, c, d) max3(a, max(b, c), d)
#define max5(a, b, c, d, e) max4(a, max(b, c), d, e)
#define min3(a, b, c) min(a, min(b, c))
#define min4(a, b, c, d) min3(a, min(b, c), d)
#define min5(a, b, c, d, e) min4(a, min(b, c), d, e)
#define sec second
#define fir first
// #define all(a) (a).begin(),(a).end()
#define vec vector<int>
#define M_PI 3.14159265358979323846 /* pi */
#define pb push_back
#define pii pair<int, int>
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
std::vector<ll> v;
ll n;
std::vector<ll> memo(100005, -1);
ll dp(ll i) {
  if (i == 0)
    return 0;

  if (i == 1)
    return abs(v[1] - v[0]);

  if (memo[i] == -1)
    return memo[i] = min(dp(i - 1) + abs(v[i] - v[i - 1]),
                         dp(i - 2) + abs(v[i] - v[i - 2]));
  else
    return memo[i];
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast cin >> n;
  FOR(i, n) {
    ll x;
    cin >> x;
    v.pb(x);
  }
  cout << dp(n - 1);

  return 0;
}


Fixed submission:
// JAI SHREE RAM
typedef long long int ll;
typedef unsigned long long int ull;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define FOR(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define sz(a) int((a).size())
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define loop for (int i = 0; i < n; i++)
#define max3(a, b, c) max(a, max(b, c))
#define max4(a, b, c, d) max3(a, max(b, c), d)
#define max5(a, b, c, d, e) max4(a, max(b, c), d, e)
#define min3(a, b, c) min(a, min(b, c))
#define min4(a, b, c, d) min3(a, min(b, c), d)
#define min5(a, b, c, d, e) min4(a, min(b, c), d, e)
#define sec second
#define fir first
// #define all(a) (a).begin(),(a).end()
#define vec vector<int>
#define M_PI 3.14159265358979323846 /* pi */
#define pb push_back
#define pii pair<int, int>
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
std::vector<ll> v;
ll n;
std::vector<ll> memo(100005, -1);
ll dp(ll i) {
  if (i == 0)
    return 0;

  if (i == 1)
    return abs(v[1] - v[0]);

  if (memo[i] == -1)
    return memo[i] = min(dp(i - 1) + abs(v[i] - v[i - 1]),
                         dp(i - 2) + abs(v[i] - v[i - 2]));
  else
    return memo[i];
}
int main() {

  fast cin >> n;
  FOR(i, n) {
    ll x;
    cin >> x;
    v.pb(x);
  }
  cout << dp(n - 1);

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 20010;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int h[maxn], dp[maxn], n;
int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    h[i] = read();
  dp[0] = 0;
  dp[1] = 0;
  dp[2] = abs(h[2] - h[1]);
  for (int i = 3; i <= n; i++)
    dp[i] =
        min(dp[i - 2] + abs(h[i] - h[i - 2]), dp[i - 1] + abs(h[i] - h[i - 1]));
  cout << dp[n] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int h[maxn], dp[maxn], n;
int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    h[i] = read();
  dp[0] = 0;
  dp[1] = 0;
  dp[2] = abs(h[2] - h[1]);
  for (int i = 3; i <= n; i++)
    dp[i] =
        min(dp[i - 2] + abs(h[i] - h[i - 2]), dp[i - 1] + abs(h[i] - h[i - 1]));
  cout << dp[n] << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#if __has_include("print.hpp")
#include "print.hpp"
#endif

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

typedef long long ll;
typedef pair<int, int> p;

vector<int> dp(10, 0);
vector<int> v;

int n;

void dfs() {
  for (int i = 2; i < n; i++) {
    dp[i] =
        min(dp[i - 1] + abs(v[i - 1] - v[i]), dp[i - 2] + abs(v[i - 2] - v[i]));
  }

  // print(v);
  // print(dp);

  cout << dp[n - 1] << endl;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    v.push_back(tmp);
  }

  dp[0] = 0;
  dp[1] = abs(v[0] - v[1]);

  dfs();
}


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

#if __has_include("print.hpp")
#include "print.hpp"
#endif

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

typedef long long ll;
typedef pair<int, int> p;

vector<int> dp(100500, 0);
vector<int> v;

int n;

void dfs() {
  for (int i = 2; i < n; i++) {
    dp[i] =
        min(dp[i - 1] + abs(v[i - 1] - v[i]), dp[i - 2] + abs(v[i - 2] - v[i]));
  }

  // print(v);
  // print(dp);

  cout << dp[n - 1] << endl;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    v.push_back(tmp);
  }

  dp[0] = 0;
  dp[1] = abs(v[0] - v[1]);

  dfs();
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <utility>
#define ll long long
#define fr first
#define sd second

using namespace std;

struct Node {
  int data;
  struct Node *left;
  struct Node *right;
  Node(int x) {
    data = x;
    left = NULL;
    right = NULL;
  }
};

#define Node struct Node

int dp[100005];
int calc(int a[], int i) {
  if (i == 0)
    return 0;
  if (i == 1)
    return abs(a[1] - a[0]);
  return min(abs(a[i] - a[i - 1]) + calc(a, i - 1),
             abs(a[i] - a[i - 2]) + calc(a, i - 2));
}

int main() {
  /*freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);*/
  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++)
      cin >> a[i];
    memset(dp, -1, sizeof(dp));
    cout << calc(a, n - 1) << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <utility>
#define ll long long
#define fr first
#define sd second

using namespace std;

struct Node {
  int data;
  struct Node *left;
  struct Node *right;
  Node(int x) {
    data = x;
    left = NULL;
    right = NULL;
  }
};

#define Node struct Node

int dp[100005];
int calc(int a[], int i) {
  if (i == 0)
    return 0;
  if (i == 1)
    return abs(a[1] - a[0]);
  if (dp[i] != -1)
    return dp[i];
  return dp[i] = min(abs(a[i] - a[i - 1]) + calc(a, i - 1),
                     abs(a[i] - a[i - 2]) + calc(a, i - 2));
}

int main() {
  /*freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);*/
  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++)
      cin >> a[i];
    memset(dp, -1, sizeof(dp));
    cout << calc(a, n - 1) << endl;
  }
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INF 1000000000000
using namespace std;
using ll = long long;
int main() {
  ll n;
  cin >> n;
  vector<ll> h(n);
  ll dp[10010];
  for (ll i = 0; i < n; i++) {
    cin >> h[i];
  }
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);
  for (ll i = 2; i < n; i++) {
    dp[i] =
        min(dp[i - 2] + abs(h[i - 2] - h[i]), dp[i - 1] + abs(h[i - 1] - h[i]));
  }
  cout << dp[n - 1] << endl;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INF 1000000000000
using namespace std;
using ll = long long;
int main() {
  ll n;
  cin >> n;
  vector<ll> h(n);
  ll dp[100010];
  for (ll i = 0; i < n; i++) {
    cin >> h[i];
  }
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);
  for (ll i = 2; i < n; i++) {
    dp[i] =
        min(dp[i - 2] + abs(h[i - 2] - h[i]), dp[i - 1] + abs(h[i - 1] - h[i]));
  }
  cout << dp[n - 1] << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;

  cin >> n;
  vector<int> v(n), dp(n);

  for (int i = 0; i < n; i++)
    cin >> v[i];

  dp[0] = 0;

  for (int i = 1; i <= n; i++)
    dp[i] = INT_MAX;

  for (int i = 0; i < n; i++) {
    if (i + 1 < n)
      dp[i + 1] = min(dp[i + 1], dp[i] + abs(v[i] - v[i + 1]));
    if (i + 2 < n)
      dp[i + 2] = min(dp[i + 2], dp[i] + abs(v[i] - v[i + 2]));
  }

  cout << dp[n - 1] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;

  cin >> n;
  vector<int> v(n), dp(n + 1);

  for (int i = 0; i < n; i++)
    cin >> v[i];

  dp[0] = 0;

  for (int i = 1; i <= n; i++)
    dp[i] = INT_MAX;

  for (int i = 0; i < n; i++) {
    if (i + 1 < n)
      dp[i + 1] = min(dp[i + 1], dp[i] + abs(v[i] - v[i + 1]));
    if (i + 2 < n)
      dp[i + 2] = min(dp[i + 2], dp[i] + abs(v[i] - v[i + 2]));
  }

  cout << dp[n - 1] << endl;

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define int long long
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)

using namespace std;
// const int MOD = 10e9+7;
const int INF = 1LL << 60;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

signed main() {
  int N;
  cin >> N;
  int h[N];
  int dp[10100];

  REP(i, N) cin >> h[i];
  fill(dp, dp + N, INF);
  dp[0] = 0;
  REP(i, N) {
    // dp[i]はi+1番目のところに行きつくまでの最短コスト
    if (i + 1 <= N)
      dp[i + 1] = min(dp[i + 1], dp[i] + abs(h[i + 1] - h[i]));
    if (i + 2 <= N)
      dp[i + 2] = min(dp[i + 2], dp[i] + abs(h[i + 2] - h[i]));
  }

  cout << dp[N - 1] << endl;
}


Fixed submission:
#include <iostream>
#define int long long
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)

using namespace std;
// const int MOD = 10e9+7;
const int INF = 1LL << 60;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

signed main() {
  int N;
  cin >> N;
  int h[N];
  int dp[100100];

  REP(i, N) cin >> h[i];
  fill(dp, dp + N, INF);
  dp[0] = 0;
  REP(i, N) {
    // dp[i]はi+1番目のところに行きつくまでの最短コスト
    if (i + 1 <= N)
      dp[i + 1] = min(dp[i + 1], dp[i] + abs(h[i + 1] - h[i]));
    if (i + 2 <= N)
      dp[i + 2] = min(dp[i + 2], dp[i] + abs(h[i + 2] - h[i]));
  }

  cout << dp[N - 1] << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
int h[10010];
int ans[10010];
int cost(int a, int b) {
  int ret = a - b;
  return ret >= 0 ? ret : -ret;
}
int min(int a, int b) { return a < b ? a : b; }
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = 1; i <= n; i++) {
      scanf("%d", &h[i]);
    }
    ans[2] = cost(h[1], h[2]);
    ans[3] = min(cost(h[1], h[3]), ans[2] + cost(h[2], h[3]));
    for (int i = 4; i <= n; i++) {
      ans[i] = min(ans[i - 2] + cost(h[i - 2], h[i]),
                   ans[i - 1] + cost(h[i - 1], h[i]));
    }
    printf("%d\n", ans[n]);
  }
  return 0;
}


Fixed submission:
#include <cstdio>
int h[100100];
int ans[100100];
int cost(int a, int b) {
  int ret = a - b;
  return ret >= 0 ? ret : -ret;
}
int min(int a, int b) { return a < b ? a : b; }
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = 1; i <= n; i++) {
      scanf("%d", &h[i]);
    }
    ans[2] = cost(h[1], h[2]);
    ans[3] = min(cost(h[1], h[3]), ans[2] + cost(h[2], h[3]));
    for (int i = 4; i <= n; i++) {
      ans[i] = min(ans[i - 2] + cost(h[i - 2], h[i]),
                   ans[i - 1] + cost(h[i - 1], h[i]));
    }
    printf("%d\n", ans[n]);
  }
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {

  int N;
  cin >> N;
  vector<int> h(N);
  for (int i = 0; i < N; i++)
    cin >> h[i];

  static int dp[1000000000];
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);

  for (int i = 2; i < N; i++) {
    dp[i] =
        min(dp[i - 2] + abs(h[i - 2] - h[i]), dp[i - 1] + abs(h[i - 1] - h[i]));
  }

  cout << dp[N - 1] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {

  int N;
  cin >> N;
  vector<int> h(N);
  for (int i = 0; i < N; i++)
    cin >> h[i];

  int dp[1000000];
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);

  for (int i = 2; i < N; i++) {
    dp[i] =
        min(dp[i - 2] + abs(h[i - 2] - h[i]), dp[i - 1] + abs(h[i - 1] - h[i]));
  }

  cout << dp[N - 1] << endl;

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define watch(x) cout << (#x) << " is " << (x) << endl
#define watchArray(v)                                                          \
  for (auto i : v)                                                             \
    cout << i << " ";                                                          \
  cout << endl;
#define all(v) (v).begin(), (v).end()

#define endl '\n'
#define MAX 200000
#define MOD 100000007
#define INF numeric_limits<int>::max()
#define INFLL numeric_limits<long long int>::max()

typedef long long int lli;
typedef long double ld;

int n;
vector<int> memo, v;

int main(void) {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  memo.assign(n, INF);
  v.resize(n);
  for (auto &i : v)
    cin >> i;

  memo[0] = 0;
  memo[1] = abs(v[0] - v[1]);
  for (int i = 2; i < v.size(); i++) {
    memo[i] = min(memo[i - 1] + abs(v[i] - v[i - 1]),
                  memo[i - 2] + abs(v[i] - v[i - 2]));
  }

  cout << memo[n - 1] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define watch(x) cout << (#x) << " is " << (x) << endl
#define watchArray(v)                                                          \
  for (auto i : v)                                                             \
    cout << i << " ";                                                          \
  cout << endl;
#define all(v) (v).begin(), (v).end()

#define endl '\n'
#define MAX 200000
#define MOD 100000007
#define INF numeric_limits<int>::max()
#define INFLL numeric_limits<long long int>::max()

typedef long long int lli;
typedef long double ld;

int n;
vector<int> memo, v;

int main(void) {
  // #ifndef ONLINE_JUDGE
  //   freopen("input.txt", "r", stdin);
  //   freopen("output.txt", "w", stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  memo.assign(n, INF);
  v.resize(n);
  for (auto &i : v)
    cin >> i;

  memo[0] = 0;
  memo[1] = abs(v[0] - v[1]);
  for (int i = 2; i < v.size(); i++) {
    memo[i] = min(memo[i - 1] + abs(v[i] - v[i - 1]),
                  memo[i - 2] + abs(v[i] - v[i - 2]));
  }

  cout << memo[n - 1] << endl;

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// 1.read question_2.read I/O_3.read test cases_4.read constraints(to decide TC
// of solution)_5.decide method_6.dry run_ 7.code_8.test_9.submit
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define mod 1000000007
#define F first
#define S second
#define _READ freopen("input.txt", "r", stdin);
#define pb push_back
#define MP make_pair
#define _FAST                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
int main() {
#ifndef ONLINE_JUDGE
  _READ
#endif
  _FAST
  lli n, t, i, j, k, ans = 0, flag = 0, a, b, m;
  cin >> n;
  vector<lli> ar(n);
  for (auto &i : ar)
    cin >> i;
  vector<lli> dp(n, 0);
  for (i = 1; i < n; i++)
    dp[i] = min(abs(ar[i - 1] - ar[i]) + dp[i - 1],
                i - 2 < 0 ? INT_MAX : abs(ar[i - 2] - ar[i]) + dp[i - 2]);
  cout << dp[n - 1];
  return 0;
}


Fixed submission:
// 1.read question_2.read I/O_3.read test cases_4.read constraints(to decide TC
// of solution)_5.decide method_6.dry run_ 7.code_8.test_9.submit
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define mod 1000000007
#define F first
#define S second
#define _READ freopen("input.txt", "r", stdin);
#define pb push_back
#define MP make_pair
#define _FAST                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
int main() {

  _FAST
  lli n, t, i, j, k, ans = 0, flag = 0, a, b, m;
  cin >> n;
  vector<lli> ar(n);
  for (auto &i : ar)
    cin >> i;
  vector<lli> dp(n, 0);
  for (i = 1; i < n; i++)
    dp[i] = min(abs(ar[i - 1] - ar[i]) + dp[i - 1],
                i - 2 < 0 ? INT_MAX : abs(ar[i - 2] - ar[i]) + dp[i - 2]);
  cout << dp[n - 1];
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// #include<bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int dp[n];
  dp[0] = 0;
  dp[1] = abs(arr[0] - arr[1]);
  for (int i = 2; i < n; i++) {
    dp[i] = min(dp[i - 1] + abs(arr[i] - arr[i - 1]),
                dp[i - 2] + abs(arr[i] - arr[i - 2]));
  }
  //   cout << dp[n-1];
  return dp[n - 1];
}

Fixed submission:
// #include<bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int dp[n];
  dp[0] = 0;
  dp[1] = abs(arr[0] - arr[1]);
  for (int i = 2; i < n; i++) {
    dp[i] = min(dp[i - 1] + abs(arr[i] - arr[i - 1]),
                dp[i - 2] + abs(arr[i] - arr[i - 2]));
  }
  cout << dp[n - 1];
  return 0;
  // return dp[n-1];
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define loopi(x, y) for (int i = x; i < (y); i++)
#define loopj(x, y) for (int j = x; j < (y); j++)

#define newline cout << "\n";
#define ll long long int
using namespace std;
#define mod 1000000007

int cost[1000000];

int jump(vector<int> &ht, int i, int n) {
  if (i >= n - 1) {
    return 0;
  }
  int a = INT_MAX, b = INT_MAX;

  if (i + 1 < n) {
    if (cost[i + 1] == -1) {
      cost[i + 1] = jump(ht, i + 1, n);
    }
    a = abs(ht[i + 1] - ht[i]) + cost[i + 1];
  }
  if (i + 2 < n) {
    if (cost[i + 2] == -1) {
      cost[i + 2] = jump(ht, i + 2, n);
    }
    b = abs(ht[i + 2] - ht[i]) + cost[i + 2];
  }
  return cost[i] = min(a, b);
}

int main() {

  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n;
  cin >> n;

  vector<int> ht(n);

  loopi(0, n) {
    cin >> ht[i];
    cost[i] = -1;
  }

  cout << jump(ht, 0, n);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define loopi(x, y) for (int i = x; i < (y); i++)
#define loopj(x, y) for (int j = x; j < (y); j++)

#define newline cout << "\n";
#define ll long long int
using namespace std;
#define mod 1000000007

int cost[1000000];

int jump(vector<int> &ht, int i, int n) {
  if (i >= n - 1) {
    return 0;
  }
  int a = INT_MAX, b = INT_MAX;

  if (i + 1 < n) {
    if (cost[i + 1] == -1) {
      cost[i + 1] = jump(ht, i + 1, n);
    }
    a = abs(ht[i + 1] - ht[i]) + cost[i + 1];
  }
  if (i + 2 < n) {
    if (cost[i + 2] == -1) {
      cost[i + 2] = jump(ht, i + 2, n);
    }
    b = abs(ht[i + 2] - ht[i]) + cost[i + 2];
  }
  return cost[i] = min(a, b);
}

int main() {

  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);

  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif

  int n;
  cin >> n;

  vector<int> ht(n);

  loopi(0, n) {
    cin >> ht[i];
    cost[i] = -1;
  }

  cout << jump(ht, 0, n);

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vec;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int, int> par;
typedef vector<par> vpar;

#define forn(i, n) for (ll i = 0; i < n; i++)
#define wez(n)                                                                 \
  ll(n);                                                                       \
  cin >> (n);
#define setmax(a, b) (a) = max((a), (b))
#define setmin(a, b) (a) = min((a), (b))
#define pb push_back
#define mp make_pair
#define outa(a, e) forn(i, e) cout << a[i] << (i == e - 1 ? '\n' : ' ')
#define oute(n) cout << n << "\n"

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  ll dp[n][2];
  ll h[n];
  forn(i, n) cin >> h[i];
  dp[0][1] = 0;
  dp[0][2] = 0;
  dp[1][2] = 1e9;
  dp[1][1] = (h[0] > h[1] ? h[0] - h[1] : h[1] - h[0]);
  for (ll i = 2; i < n; i++) {
    dp[i][1] = min(dp[i - 1][1], dp[i - 1][2]) +
               (h[i] >= h[i - 1] ? h[i] - h[i - 1] : h[i - 1] - h[i]);
    dp[i][2] = min(dp[i - 2][1], dp[i - 2][2]) +
               (h[i] >= h[i - 2] ? h[i] - h[i - 2] : h[i - 2] - h[i]);
  }
  cout << min(dp[n - 1][1], dp[n - 1][2]) << endl;
  return 0;
}


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

typedef long long ll;
typedef vector<int> vec;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int, int> par;
typedef vector<par> vpar;

#define forn(i, n) for (ll i = 0; i < n; i++)
#define wez(n)                                                                 \
  ll(n);                                                                       \
  cin >> (n);
#define setmax(a, b) (a) = max((a), (b))
#define setmin(a, b) (a) = min((a), (b))
#define pb push_back
#define mp make_pair
#define outa(a, e) forn(i, e) cout << a[i] << (i == e - 1 ? '\n' : ' ')
#define oute(n) cout << n << "\n"

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  ll n;
  cin >> n;
  ll dp[n][2];
  ll h[n];
  forn(i, n) cin >> h[i];
  dp[0][1] = 0;
  dp[0][2] = 0;
  dp[1][2] = 1e9;
  dp[1][1] = (h[0] > h[1] ? h[0] - h[1] : h[1] - h[0]);
  for (ll i = 2; i < n; i++) {
    dp[i][1] = min(dp[i - 1][1], dp[i - 1][2]) +
               (h[i] >= h[i - 1] ? h[i] - h[i - 1] : h[i - 1] - h[i]);
    dp[i][2] = min(dp[i - 2][1], dp[i - 2][2]) +
               (h[i] >= h[i - 2] ? h[i] - h[i - 2] : h[i - 2] - h[i]);
  }
  cout << min(dp[n - 1][1], dp[n - 1][2]) << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long n, arr[100005], dp[100005];
long long f(long long a) {
  if (a > n)
    return 1e18;
  if (a == n)
    return 0;
  if (dp[a] != -1)
    return dp[a];
  return min(abs(arr[a + 1] - arr[a]) + f(a + 1),
             abs(arr[a + 2] - arr[a] + f(a + 2)));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  memset(dp, -1, sizeof(dp));
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  cout << f(1) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, arr[100005], dp[100005];
long long f(long long a) {
  if (a > n)
    return 1e18;
  if (a == n)
    return 0;
  if (dp[a] != -1)
    return dp[a];
  return dp[a] = min(abs(arr[a + 1] - arr[a]) + f(a + 1),
                     abs(arr[a + 2] - arr[a]) + f(a + 2));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  memset(dp, -1, sizeof(dp));
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  cout << f(1) << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int h[10010];
int a[100100];
int dp[100100];

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


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int h[100100];
int a[100100];
int dp[100100];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  a[0] = 0;
  for (int i = 1; i < n; i++)
    a[i] = h[i] - h[i - 1];
  dp[1] = abs(a[1]);
  for (int i = 0; i + 2 < n; i++)
    dp[i + 2] =
        min(dp[i] + abs(a[i + 1] + a[i + 2]), dp[i + 1] + abs(a[i + 2]));
  cout << dp[n - 1];
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define max1 1000005
#define siz 100000
#define mod 998244353
#define inf 1e18 + 5
#define ll long long int
#define debug(x) cout << #x << " " << x << endl
#define jam(x) cout << "Case #" << x << ": "
typedef pair<ll, ll> pr;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<ll> dp(n, 0);
  for (ll i = 1; i < n; i++) {
    if (i == 1) {
      dp[i] = abs(a[i] - a[i - 1]);
    } else {
      dp[i] = min(dp[i - 2] + abs(a[i] - a[i - 2]),
                  dp[i - 1] + abs(a[i] - a[i - 1]));
    }
  }
  cout << dp[n - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define max1 1000005
#define siz 100000
#define mod 998244353
#define inf 1e18 + 5
#define ll long long int
#define debug(x) cout << #x << " " << x << endl
#define jam(x) cout << "Case #" << x << ": "
typedef pair<ll, ll> pr;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  /*#ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif*/
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<ll> dp(n, 0);
  for (ll i = 1; i < n; i++) {
    if (i == 1) {
      dp[i] = abs(a[i] - a[i - 1]);
    } else {
      dp[i] = min(dp[i - 2] + abs(a[i] - a[i - 2]),
                  dp[i - 1] + abs(a[i] - a[i - 1]));
    }
  }
  cout << dp[n - 1] << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

// 💖 Hi, thanks for using ICIE! 💖
// 🔧 To use a custom code template, press Ctrl+Shift+P and select "ICIE Template
// configure" from the list 🔧 📝 If you spot any bugs or miss any features,
// create an issue at https://github.com/pustaczek/icie/issues 📝

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  int h[10005], dp[10005];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < N; i++) {
    int c1 = dp[i - 1] + abs(h[i] - h[i - 1]);
    int c2 = dp[i - 2] + abs(h[i] - h[i - 2]);
    dp[i] = min(c1, c2);
  }
  cout << dp[N - 1];
}


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

// 💖 Hi, thanks for using ICIE! 💖
// 🔧 To use a custom code template, press Ctrl+Shift+P and select "ICIE Template
// configure" from the list 🔧 📝 If you spot any bugs or miss any features,
// create an issue at https://github.com/pustaczek/icie/issues 📝

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  int h[100005], dp[100005];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < N; i++) {
    int c1 = dp[i - 1] + abs(h[i] - h[i - 1]);
    int c2 = dp[i - 2] + abs(h[i] - h[i - 2]);
    dp[i] = min(c1, c2);
  }
  cout << dp[N - 1];
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int dp[101000];

int main() {

  int n;
  scanf("%d", &n);
  vector<int> a(1 << n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }

  memset(dp, 0, sizeof dp);
  dp[0] = 0;
  for (int i = 1; i < n; ++i) {
    int op1 = INT_MAX, op2 = INT_MAX;
    if (i >= 1) {
      op1 = abs(a[i] - a[i - 1]) + dp[i - 1];
    }
    if (i >= 2) {
      op2 = abs(a[i] - a[i - 2]) + dp[i - 2];
    }
    dp[i] = min(op1, op2);
  }

  printf("%d\n", dp[n - 1]);

  return 0;
}

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

int dp[101000];

int main() {

  int n;
  scanf("%d", &n);
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }

  memset(dp, 0, sizeof dp);
  dp[0] = 0;
  for (int i = 1; i < n; ++i) {
    int op1 = INT_MAX, op2 = INT_MAX;
    if (i >= 1) {
      op1 = abs(a[i] - a[i - 1]) + dp[i - 1];
    }
    if (i >= 2) {
      op2 = abs(a[i] - a[i - 2]) + dp[i - 2];
    }
    dp[i] = min(op1, op2);
  }

  printf("%d\n", dp[n - 1]);

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
// #include <map>
#include <cmath>
#include <cstring>
#include <iomanip>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned int uii;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
typedef vector<int> vii;
const int maxn = 1e5 + 5;
LL a[maxn], d[maxn];

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
  int n;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  d[1] = abs(a[1] - a[0]);
  for (int i = 2; i < n; ++i) {
    d[i] =
        min(d[i - 1] + abs(a[i - 1] - a[i]), d[i - 2] + abs(a[i - 2] - a[i]));
  }
  cout << d[n - 1] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
// #include <map>
#include <cmath>
#include <cstring>
#include <iomanip>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned int uii;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
typedef vector<int> vii;
const int maxn = 1e5 + 5;
LL a[maxn], d[maxn];

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
  int n;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  d[1] = abs(a[1] - a[0]);
  for (int i = 2; i < n; ++i) {
    d[i] =
        min(d[i - 1] + abs(a[i - 1] - a[i]), d[i - 2] + abs(a[i - 2] - a[i]));
  }
  cout << d[n - 1] << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define p(V)                                                                   \
  for (int iiv = 0; iiv < V.size(); iiv++)                                     \
    cout << V[iiv] << " ";
#define mp make_pair
#define pb push_back
#define graph vector<vector<int>>
#define pf first
#define ps second
#define pq priority_queue
const int N = 2e5;
const int l = 18;
const double pi = acos(-1);

int n, dp[N];
vector<int> v;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> n;
  v.resize(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  dp[n - 2] = abs(v[n - 1] - v[n - 2]);
  int def;
  for (int i = n - 3; i >= 0; i--)
    dp[i] =
        min(dp[i + 1] + abs(v[i] - v[i + 1]), dp[i + 2] + abs(v[i] - v[i + 2]));
  cout << dp[0];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define p(V)                                                                   \
  for (int iiv = 0; iiv < V.size(); iiv++)                                     \
    cout << V[iiv] << " ";
#define mp make_pair
#define pb push_back
#define graph vector<vector<int>>
#define pf first
#define ps second
#define pq priority_queue
const int N = 2e5;
const int l = 18;
const double pi = acos(-1);

int n, dp[N];
vector<int> v;
int main() {
  cin >> n;
  v.resize(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  dp[n - 2] = abs(v[n - 1] - v[n - 2]);
  int def;
  for (int i = n - 3; i >= 0; i--)
    dp[i] =
        min(dp[i + 1] + abs(v[i] - v[i + 1]), dp[i + 2] + abs(v[i] - v[i + 2]));
  cout << dp[0];
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 1e5 + 5;

signed main() {
#ifndef ONLINE_JUDGE
  freopen("solve.in", "r", stdin);
  freopen("solve.out", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  int h[N], dp[N];
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  dp[1] = 0;
  dp[2] = abs(h[2] - h[1]);
  for (int i = 3; i <= n; i++) {
    dp[i] =
        min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << dp[n] << "\n";
  return false;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 1e5 + 5;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  int h[N], dp[N];
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  dp[1] = 0;
  dp[2] = abs(h[2] - h[1]);
  for (int i = 3; i <= n; i++) {
    dp[i] =
        min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << dp[n] << "\n";
  return false;
}