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

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

using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int n;
  cin >> n;
  vector<int> h(n);
  vector<long long> dp(n, 1e18);
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j <= i + 2 and j < n; ++j) {
      dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[j]));
    }
  }
  cout << dp[n - 1];

  return 0;
}

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

using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int n;
  cin >> n;
  vector<int> h(n);
  vector<long long> dp(n, 1e18);
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j <= i + 2 and j < n; ++j) {
      dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
    }
  }
  cout << dp[n - 1];

  return 0;
}
Problem p03160 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> h(N), dp(1010);
  for (int i = 0; i < N; i++)
    cin >> h[i];
  for (int i = 0; i < N; i++)
    dp[i] = INT_MAX;
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    if (dp[i + 1] > dp[i] + abs(h[i] - h[i + 1]))
      dp[i + 1] = dp[i] + abs(h[i] - h[i + 1]);
    if (dp[i + 2] > dp[i] + abs(h[i] - h[i + 2]))
      dp[i + 2] = dp[i] + abs(h[i] - h[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> h(N), dp(100010);
  for (int i = 0; i < N; i++)
    cin >> h[i];
  for (int i = 0; i < N; i++)
    dp[i] = INT_MAX;
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    if (dp[i + 1] > dp[i] + abs(h[i] - h[i + 1]))
      dp[i + 1] = dp[i] + abs(h[i] - h[i + 1]);
    if (dp[i + 2] > dp[i] + abs(h[i] - h[i + 2]))
      dp[i + 2] = dp[i] + abs(h[i] - h[i + 2]);
  }
  cout << dp[N - 1] << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#define lli long long int
using namespace std;

lli cache[100005];
lli getmin(vector<int> h, int i, int n) {
  if (i == n - 1) {
    return 0;
  }

  if (cache[i] != -1) {
    return cache[i];
  }

  lli r1 = INT_MAX, r2 = INT_MAX;
  if ((i + 1) <= (n - 1)) {
    r1 = abs(h[i] - h[i + 1]) + getmin(h, i + 1, n);
  }

  if ((i + 2) <= (n - 1)) {
    r2 = abs(h[i] - h[i + 2]) + getmin(h, i + 2, n);
  }

  cache[i] = min(r1, r2);

  return cache[i];
}

void solve() {
  int n;
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    cache[i] = -1;
  }
  cache[n] = -1;

  lli res = getmin(h, 0, n);
  cout << res << endl;
}

int main() { solve(); }


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#define lli long long int
using namespace std;

lli cache[100005];
lli getmin(vector<int> &h, int i, int n) {
  if (i == n - 1) {
    return 0;
  }

  if (cache[i] != -1) {
    return cache[i];
  }

  lli r1 = INT_MAX, r2 = INT_MAX;
  if ((i + 1) <= (n - 1)) {
    r1 = abs(h[i] - h[i + 1]) + getmin(h, i + 1, n);
  }

  if ((i + 2) <= (n - 1)) {
    r2 = abs(h[i] - h[i + 2]) + getmin(h, i + 2, n);
  }

  cache[i] = min(r1, r2);

  return cache[i];
}

void solve() {
  int n;
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    cache[i] = -1;
  }
  cache[n] = -1;

  lli res = getmin(h, 0, n);
  cout << res << endl;
}

int main() { solve(); }
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define z 500000000000000000
#define sh 500000

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, i;
  cin >> n;
  int ar[n + 1];
  for (i = 1; i <= n; i++)
    cin >> ar[i];
  int dp[n + 1];
  dp[1] = 0;
  dp[2] = abs(ar[2] - ar[1]);
  for (i = 3; i <= n; i++) {
    dp[i] = min(dp[i - 1] + abs(ar[i] - ar[i - 1]),
                dp[i - 2] + abs(ar[i] - ar[i - 2]));
  }
  cout << dp[n];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define z 500000000000000000
#define sh 500000

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, i;
  cin >> n;
  int ar[n + 1];
  for (i = 1; i <= n; i++)
    cin >> ar[i];
  int dp[n + 1];
  dp[1] = 0;
  dp[2] = abs(ar[2] - ar[1]);
  for (i = 3; i <= n; i++) {
    dp[i] = min(dp[i - 1] + abs(ar[i] - ar[i - 1]),
                dp[i - 2] + abs(ar[i] - ar[i - 2]));
  }
  cout << dp[n];
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define eb emplace_back
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define whole(v) begin(v), end(v)
#define print(c, n)                                                            \
  for (int i = 0; i < n; i++)                                                  \
    cout << c[i] << " ";
#define MOD 1000000007
#define INF 0x3f3f3f3f
int a[100005], dp[100005];
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  dp[2] = min(dp[1] + abs(a[2] - a[1]), abs(a[2] - a[0]));
  for (int i = 3; i < n; i++) {
    dp[i] =
        min(dp[i - 1] + abs(a[i] - a[i - 1]), dp[i - 2] + abs(a[i] - a[i - 2]));
  }
  cout << dp[n - 1] << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define eb emplace_back
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define whole(v) begin(v), end(v)
#define print(c, n)                                                            \
  for (int i = 0; i < n; i++)                                                  \
    cout << c[i] << " ";
#define MOD 1000000007
#define INF 0x3f3f3f3f
int a[100005], dp[100005];
int main() {

  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  dp[2] = min(dp[1] + abs(a[2] - a[1]), abs(a[2] - a[0]));
  for (int i = 3; i < n; i++) {
    dp[i] =
        min(dp[i - 1] + abs(a[i] - a[i - 1]), dp[i - 2] + abs(a[i] - a[i - 2]));
  }
  cout << dp[n - 1] << "\n";
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

int dp[10000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  vs32 h(N);
  rep(i, N) cin >> h[i];

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

  for (int i = 2; 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 - 1] << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

int dp[100000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  vs32 h(N);
  rep(i, N) cin >> h[i];

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

  for (int i = 2; 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 - 1] << "\n";
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <tr1/unordered_map>
using namespace std;
#define ll long long
#define fr first
#define se second
#define fi(i, s, e, inc) for (auto i = s; i < e; i += inc)
#define fie(i, s, e, inc) for (auto i = s; i <= e; i += inc)
#define fd(i, s, e, dec) for (auto i = s; i > e; i -= dec)
#define fde(i, s, e, dec) for (auto i = s; i >= e; i -= dec)
#define umap tr1::unordered_map
#define nl cout << "\n";
#define vt vector
#define pb push_back
#define pr(x) cout << x;
#define ps(x) cout << x << " ";
#define sp cout << " ";
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define rev reverse
#define tc                                                                     \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define pq priority_queue
ll a[100005];
ll n;
ll dp[100005];
ll solve(ll idx) {
  if (idx == n - 1)
    return 0;

  if (idx >= n)
    return INT_MAX;

  if (dp[idx] != -1)
    return dp[idx];

  // jump one step
  ll jump_one = abs(a[idx] - a[idx + 1]) + solve(idx + 1);

  // jump two step
  ll jump_two = INT_MAX;
  if (idx + 2 < n)
    jump_two = abs(a[idx] - a[idx + 2]) + solve(idx + 2);

  return dp[idx] = min(jump_one, jump_two);
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  fi(i, 0, n, 1) cin >> a[i];
  memset(dp, -1, sizeof(dp));
  pr(solve(0)) nl return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <tr1/unordered_map>
using namespace std;
#define ll long long
#define fr first
#define se second
#define fi(i, s, e, inc) for (auto i = s; i < e; i += inc)
#define fie(i, s, e, inc) for (auto i = s; i <= e; i += inc)
#define fd(i, s, e, dec) for (auto i = s; i > e; i -= dec)
#define fde(i, s, e, dec) for (auto i = s; i >= e; i -= dec)
#define umap tr1::unordered_map
#define nl cout << "\n";
#define vt vector
#define pb push_back
#define pr(x) cout << x;
#define ps(x) cout << x << " ";
#define sp cout << " ";
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define rev reverse
#define tc                                                                     \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define pq priority_queue
ll a[100005];
ll n;
ll dp[100005];
ll solve(ll idx) {
  if (idx == n - 1)
    return 0;

  if (idx >= n)
    return INT_MAX;

  if (dp[idx] != -1)
    return dp[idx];

  // jump one step
  ll jump_one = abs(a[idx] - a[idx + 1]) + solve(idx + 1);

  // jump two step
  ll jump_two = INT_MAX;
  if (idx + 2 < n)
    jump_two = abs(a[idx] - a[idx + 2]) + solve(idx + 2);

  return dp[idx] = min(jump_one, jump_two);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  fi(i, 0, n, 1) cin >> a[i];
  memset(dp, -1, sizeof(dp));
  pr(solve(0)) nl return 0;
}
Problem p03160 (C++)

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

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long ll;

int n;
ll h[100005];
ll memo[100005];

ll dp(int i) {
  if (i == n - 1)
    return 0;
  if (i >= n)
    return 10000000000;
  if (memo[i] != -1) {
    return memo[i];
  }
  return memo[i] = min(abs(h[i] - h[i + 1]) + dp(i + 1),
                       abs(h[i] - h[i + 2]) + dp(i + 2));
}

int main() {

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif

  memset(memo, -1, sizeof(memo));

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

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

  cout << dp(0) << "\n";

  return 0;
}

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

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long ll;

int n;
ll h[100005];
ll memo[100005];

ll dp(int i) {
  if (i == n - 1)
    return 0;
  if (i >= n)
    return 10000000000;
  if (memo[i] != -1) {
    return memo[i];
  }
  return memo[i] = min(abs(h[i] - h[i + 1]) + dp(i + 1),
                       abs(h[i] - h[i + 2]) + dp(i + 2));
}

int main() {

  memset(memo, -1, sizeof(memo));

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

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

  cout << dp(0) << "\n";

  return 0;
}
Problem p03160 (C++)

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

int main() {
  int n;
  cin >> n;
  int h[11000] = {}, dp[11000] = {};
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < n; i++) {
    dp[i] = (abs(h[i] - h[i - 1]) + dp[i - 1] < abs(h[i] - h[i - 2]) + dp[i - 2]
                 ? 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 <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int h[110000] = {}, dp[110000] = {};
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < n; i++) {
    dp[i] = (abs(h[i] - h[i - 1]) + dp[i - 1] < abs(h[i] - h[i - 2]) + dp[i - 2]
                 ? 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;
#define F first
#define S second
#define show(x) cout << #x << " " << x << " ";
#define f(i, j, k) for (int i = j; i <= k; i++)
#define fr(i, j, k) for (int i = j; i >= k; i--)
#define ll long long
#define all(A) A.begin(), A.end()
#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
const int MSIZE = 1e2 + 5;
const int MOD = 1e9 + 7;

int arr[MSIZE];
int dp[MSIZE];

int main() {
  FIO int n;
  cin >> n;
  f(i, 0, n - 1) cin >> arr[i];
  dp[0] = 0;
  dp[1] = abs(arr[0] - arr[1]);
  f(i, 2, n - 1) {
    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] << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define F first
#define S second
#define show(x) cout << #x << " " << x << " ";
#define f(i, j, k) for (int i = j; i <= k; i++)
#define fr(i, j, k) for (int i = j; i >= k; i--)
#define ll long long
#define all(A) A.begin(), A.end()
#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
const int MSIZE = 2e5 + 5;
const int MOD = 1e9 + 7;

int arr[MSIZE];
int dp[MSIZE];

int main() {
  FIO int n;
  cin >> n;
  f(i, 0, n - 1) cin >> arr[i];
  dp[0] = 0;
  dp[1] = abs(arr[0] - arr[1]);
  f(i, 2, n - 1) {
    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] << endl;
  return 0;
}
Problem p03160 (C++)

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

using namespace std;
using ll = long long;

using Field = vector<vector<int>>;
using Graph = vector<vector<int>>;
using VI = vector<int>;
using VC = vector<char>;
using PI = pair<int, int>;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()

const long long INF = 1LL << 60;
const int mod = 1000000007;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int N;
vector<int> H(100010);
vector<int> H1(100010);
vector<int> H2(100010);
int MAX_N = 100000;

vector<ll> dp(10010, INF);

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> N;
  REP(i, N) { cin >> H.at(i); }
  for (int i = 0; i < N - 1; i++) {
    H1.at(i) = abs(H.at(i) - H.at(i + 1));
  }
  for (int i = 0; i < N - 2; i++) {
    H2.at(i) = abs(H.at(i) - H.at(i + 2));
  }

  dp.at(0) = 0;

  for (int i = 0; i < N; i++) {
    chmin(dp.at(i + 1), dp.at(i) + H1.at(i));
    chmin(dp.at(i + 2), dp.at(i) + H2.at(i));
  }

  cout << dp.at(N - 1) << endl;

  return 0;
}

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

using namespace std;
using ll = long long;

using Field = vector<vector<int>>;
using Graph = vector<vector<int>>;
using VI = vector<int>;
using VC = vector<char>;
using PI = pair<int, int>;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()

const long long INF = 1LL << 60;
const int mod = 1000000007;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int N;
vector<int> H(100010);
vector<int> H1(100010);
vector<int> H2(100010);
int MAX_N = 100000;

vector<ll> dp(100010, INF);

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> N;
  REP(i, N) { cin >> H.at(i); }
  for (int i = 0; i < N - 1; i++) {
    H1.at(i) = abs(H.at(i) - H.at(i + 1));
  }
  for (int i = 0; i < N - 2; i++) {
    H2.at(i) = abs(H.at(i) - H.at(i + 2));
  }

  dp.at(0) = 0;

  for (int i = 0; i < N; i++) {
    chmin(dp.at(i + 1), dp.at(i) + H1.at(i));
    chmin(dp.at(i + 2), dp.at(i) + H2.at(i));
  }

  cout << dp.at(N - 1) << endl;

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
/*author* Priyanshu Shrivastav (from IIT Palakkad) *
 * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
 * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
 * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
 * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
When I wrote this, only God and I understood what I was doing
 ** * * * * * * * Now, only God knows * * * * * * */
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr)
#define PREC                                                                   \
  cout.precision(10);                                                          \
  cout << fixed
using namespace std;

const int N = (int)1e5 + 4;
int n;
int memo[N], h[N];
int dp(int i) {
  if (i >= n)
    return 0;
  else if (memo[i] != -1)
    return memo[i];
  else {
    int j1 = dp(i + 1) + abs(h[i] - h[i + 1]),
        j2 = (i + 2 <= n ? dp(i + 2) + abs(h[i] - h[i + 2]) : INT_MAX);
    return min(j1, j2);
  }
}

int main() {
  IOS;
  PREC;

  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> h[i], memo[i] = -1;
  cout << dp(1) << endl;

  return EXIT_SUCCESS;
}


Fixed submission:
/*author* Priyanshu Shrivastav (from IIT Palakkad) *
 * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
 * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
 * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
 * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
When I wrote this, only God and I understood what I was doing
 ** * * * * * * * Now, only God knows * * * * * * */
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr)
#define PREC                                                                   \
  cout.precision(10);                                                          \
  cout << fixed
using namespace std;

const int N = (int)1e5 + 4;
int n;
int memo[N], h[N];
int dp(int i) {
  if (i >= n)
    return 0;
  else if (memo[i] != -1)
    return memo[i];
  else {
    int j1 = dp(i + 1) + abs(h[i] - h[i + 1]),
        j2 = (i + 2 <= n ? dp(i + 2) + abs(h[i] - h[i + 2]) : INT_MAX);
    return memo[i] = min(j1, j2);
  }
}

int main() {
  IOS;
  PREC;

  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> h[i], memo[i] = -1;
  cout << dp(1) << endl;

  return EXIT_SUCCESS;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define w(t)                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)

int main() {

  // w(t)
  //{

  ll n, x, y, W, i, j, z = 0, k = 0, c = 0, s = 0;
  cin >> n;
  ll h[n];
  ll dp[1005];
  // map<ll,ll>mp;
  for (i = 0; i < n; i++) {
    cin >> h[i];
    // cout<<h[i];
  }
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);
  // cout<<dp[1];
  for (i = 2; 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 - 1];
}

//
//  cout<<a[i]<<b[j];

/*if(flag==1)
    cout<<"NO"<<endl;
    else
    cout<<"YES"<<endl;*/


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define w(t)                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)

int main() {

  // w(t)
  //{

  ll n, x, y, W, i, j, z = 0, k = 0, c = 0, s = 0;
  cin >> n;
  ll h[n];
  ll dp[100005];
  // map<ll,ll>mp;
  for (i = 0; i < n; i++) {
    cin >> h[i];
    // cout<<h[i];
  }
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);
  // cout<<dp[1];
  for (i = 2; 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 - 1];
}

//
//  cout<<a[i]<<b[j];

/*if(flag==1)
    cout<<"NO"<<endl;
    else
    cout<<"YES"<<endl;*/
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define mk make_pair
#define ascSort(v) sort(v.begin(), v.end())
#define descSort(v) sort(v.begin(), v.end(), greater<int>())
#define ff first
#define ss second
#define pi pair<int, int>
#define vi vector<int>
#define umapi unordered_map<int, int>

const int m = 998244353;
using namespace std;

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

int32_t main() {

  FIO();

  int n;
  cin >> n;
  int array[n], dp[n];
  for (int i = 0; i < n; i++) {
    cin >> array[i];
  }
  for (int i = 0; i < n; i++)
    dp[i] = 1e9;
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    dp[i + 1] = min(dp[i + 1], dp[i] + abs(array[i] - array[i + 1]));
    dp[i + 2] = min(dp[i + 2], dp[i] + abs(array[i] - array[i + 2]));
  }
  cout << dp[n - 1] << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define mk make_pair
#define ascSort(v) sort(v.begin(), v.end())
#define descSort(v) sort(v.begin(), v.end(), greater<int>())
#define ff first
#define ss second
#define pi pair<int, int>
#define vi vector<int>
#define umapi unordered_map<int, int>

const int m = 998244353;
using namespace std;

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

int32_t main() {

  // FIO();

  int n;
  cin >> n;
  int array[n], dp[n];
  for (int i = 0; i < n; i++) {
    cin >> array[i];
  }
  for (int i = 0; i < n; i++)
    dp[i] = 1e9;
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    dp[i + 1] = min(dp[i + 1], dp[i] + abs(array[i] - array[i + 1]));
    dp[i + 2] = min(dp[i + 2], dp[i] + abs(array[i] - array[i + 2]));
  }
  cout << dp[n - 1] << "\n";

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-ffloat-store")
#pragma GCC optimize("-fno-defer-pop")
#define all(a) a.begin(), a.end()
#define ll long long int
#define ld long double
ll power(ll a, ll b, ll m) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a % m;
  ll t = power(a, b / 2, m) % m;
  t = (t * t) % m;
  if (b & 1)
    t = ((t % m) * (a % m)) % m;
  return t;
}
ll modInverse(ll a, ll m) { return power(a, m - 2, m); }
#define ps push_back
#define fs first
#define sc second
#define N 3000005
#define endl "\n"
#define mod 1000000007
ll dp[100001];
ll maxval(ll ar[], ll n, ll i) {
  if (i == 0) {
    return 0;
  }
  if (i == 1) {
    return abs(ar[0] - ar[1]);
  }

  ll a, b;
  a = maxval(ar, n, i - 1) + abs(ar[i] - ar[i - 1]);
  b = maxval(ar, n, i - 2) + abs(ar[i] - ar[i - 2]);
  return dp[i] = min(a, b);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  memset(dp, -1, sizeof(dp));
  ll i, j, k, l, n;
  cin >> n;

  ll ar[n];
  for (i = 0; i < n; i++) {
    cin >> ar[i];
  }
  k = maxval(ar, n, n - 1);
  cout << k;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-ffloat-store")
#pragma GCC optimize("-fno-defer-pop")
#define all(a) a.begin(), a.end()
#define ll long long int
#define ld long double
ll power(ll a, ll b, ll m) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a % m;
  ll t = power(a, b / 2, m) % m;
  t = (t * t) % m;
  if (b & 1)
    t = ((t % m) * (a % m)) % m;
  return t;
}
ll modInverse(ll a, ll m) { return power(a, m - 2, m); }
#define ps push_back
#define fs first
#define sc second
#define N 3000005
#define endl "\n"
#define mod 1000000007
ll dp[100001];
ll maxval(ll ar[], ll n, ll i) {
  if (i == 0) {
    return 0;
  }
  if (i == 1) {
    return abs(ar[0] - ar[1]);
  }
  if (dp[i] != -1) {
    return dp[i];
  }

  ll a, b;
  a = maxval(ar, n, i - 1) + abs(ar[i] - ar[i - 1]);
  b = maxval(ar, n, i - 2) + abs(ar[i] - ar[i - 2]);
  return dp[i] = min(a, b);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  memset(dp, -1, sizeof(dp));
  ll i, j, k, l, n;
  cin >> n;

  ll ar[n];
  for (i = 0; i < n; i++) {
    cin >> ar[i];
  }
  k = maxval(ar, n, n - 1);
  cout << k;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ROCK                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define f first
#define sec second
using namespace std;
using namespace __gnu_pbds;
const long long int linf = (long long)9223372036854775807;
const int dx[8] = {0, 0, 1, -1, 1, -1, 1, -1},
          dy[9] = {1, -1, 0, 0, 1, -1, -1, 1};
typedef long long int ll;

const int N = 1e5 + 3, sz = 1e4 + 5;
ll arr[sz], dp[N], n;

ll solve(int pos) {
  if (pos == n - 1)
    return 0;
  if (dp[pos] != -1)
    return dp[pos];
  ll onestep = 1e9;
  ll twostep = 1e9;
  if (pos + 1 < n)
    onestep = solve(pos + 1) + abs(arr[pos] - arr[pos + 1]);
  if (pos + 2 < n)
    twostep = solve(pos + 2) + abs(arr[pos] - arr[pos + 2]);
  ll ans = min(onestep, twostep);
  return dp[pos] = ans;
}

int main() {
  ROCK;
  memset(dp, -1, sizeof dp);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  cout << solve(0) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ROCK                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define f first
#define sec second
using namespace std;
using namespace __gnu_pbds;
const long long int linf = (long long)9223372036854775807;
const int dx[8] = {0, 0, 1, -1, 1, -1, 1, -1},
          dy[9] = {1, -1, 0, 0, 1, -1, -1, 1};
typedef long long int ll;

const int N = 1e5 + 3;
ll arr[N], dp[N], n;

ll solve(int pos) {
  if (pos == n - 1)
    return 0;
  if (dp[pos] != -1)
    return dp[pos];
  ll onestep = 1e9;
  ll twostep = 1e9;
  if (pos + 1 < n)
    onestep = solve(pos + 1) + abs(arr[pos] - arr[pos + 1]);
  if (pos + 2 < n)
    twostep = solve(pos + 2) + abs(arr[pos] - arr[pos + 2]);
  ll ans = min(onestep, twostep);
  return dp[pos] = ans;
}

int main() {
  ROCK;
  memset(dp, -1, sizeof dp);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  cout << solve(0) << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int *a;
int dp[40001];
int n;
int f(int x) {
  if (x == n + 1)
    return 0;
  else {
    dp[x] =
        min(dp[x - 2] + abs(a[x - 2] - a[x]), dp[x - 1] + abs(a[x - 1] - a[x]));
    return f(x + 1);
  }
}
int main() {
  cin >> n;
  a = new int[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  dp[2] = abs(a[1] - a[2]);
  f(3);
  cout << dp[n];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int *a;
int dp[500001];
int n;
int f(int x) {
  if (x == n + 1)
    return 0;
  else {
    dp[x] =
        min(dp[x - 2] + abs(a[x - 2] - a[x]), dp[x - 1] + abs(a[x - 1] - a[x]));
    return f(x + 1);
  }
}
int main() {
  cin >> n;
  a = new int[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  dp[2] = abs(a[1] - a[2]);
  f(3);
  cout << dp[n];
  return 0;
}
Problem p03160 (C++)

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


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

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

using namespace std;

int dp[10000];

int main() {
  int n;
  cin >> n;

  vector<int> h(n);

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

  dp[0] = 0;

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

  cout << dp[n - 1] << "\n";
}


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

using namespace std;

int dp[1000000];

int main() {
  int n;
  cin >> n;

  vector<int> h(n);

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

  dp[0] = 0;

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int dp[n];
  dp[n - 1] = 0;
  dp[n - 2] = abs(a[n - 1] - a[n - 2]);
  for (int i = n - 3; i >= 0; i--) {
    dp[i] =
        min(abs(a[i] - a[i + 1]) + dp[i + 1], abs(a[i] - a[i + 2]) + dp[i + 2]);
  }
  cout << dp[0];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
int main() {
  /*
  #ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
  #endif
  */
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int dp[n];
  dp[n - 1] = 0;
  dp[n - 2] = abs(a[n - 1] - a[n - 2]);
  for (int i = n - 3; i >= 0; i--) {
    dp[i] =
        min(abs(a[i] - a[i + 1]) + dp[i + 1], abs(a[i] - a[i + 2]) + dp[i + 2]);
  }
  cout << dp[0];
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define FOR(i, a, b) for (ll i = (a); i < (ll)b; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1000000000000000
using namespace std;
typedef long long ll;
typedef double db;
typedef string str;
typedef pair<ll, ll> p;
const int MOD = 1000000007;

void print(const std::vector<int> &v) {
  std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
  std::cout << std::endl;
}

int main() {
  int N;
  vector<int> h(N);
  REP(i, N) cin >> h[i];
  vector<int> cost(N, 0);
  for (int i = 1; i < N; i++) {
    int tcost;
    if (i > 1) {
      int cost_1 = cost[i - 1] + abs(h[i] - h[i - 1]);
      int cost_2 = cost[i - 2] + abs(h[i] - h[i - 2]);
      tcost = min(cost_1, cost_2);
    } else {
      tcost = cost[i - 1] + abs(h[i] - h[i - 1]);
    }
    cost[i] = tcost;
  }
  cout << cost[N - 1] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define FOR(i, a, b) for (ll i = (a); i < (ll)b; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1000000000000000
using namespace std;
typedef long long ll;
typedef double db;
typedef string str;
typedef pair<ll, ll> p;
const int MOD = 1000000007;

void print(const std::vector<int> &v) {
  std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
  std::cout << std::endl;
}

int main() {
  int N;
  cin >> N;
  vector<int> h(N);
  REP(i, N) cin >> h[i];
  vector<int> cost(N, 0);
  for (int i = 1; i < N; i++) {
    int tcost;
    if (i > 1) {
      int cost_1 = cost[i - 1] + abs(h[i] - h[i - 1]);
      int cost_2 = cost[i - 2] + abs(h[i] - h[i - 2]);
      tcost = min(cost_1, cost_2);
    } else {
      tcost = cost[i - 1] + abs(h[i] - h[i - 1]);
    }
    cost[i] = tcost;
  }
  cout << cost[N - 1] << endl;

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define vb __int128
#define ll long long
#define ld long double
#define full(a) a.begin(), a.end()
#define stoink stack<ll>
#define vec vector<ll>
#define vg vector<vector<ll>>
#define vgw vector < vector<pair<ll, ll>>
#define pa pair<ll, ll>
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define pre 0.00000000000000000001
#define inf 1000000000000
#define mod 998244353
vec costdp;
vec height;
ll n;
ll func(ll ind) {
  if (ind > n - 1)
    return inf;
  if (costdp[ind] != -1)
    return costdp[ind];
  if (ind == n - 1)
    return 0;
  return min(func(ind + 1) + abs(height[ind + 1] - height[ind]),
             func(ind + 2) + abs(height[ind + 2] - height[ind]));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  height.resize(n);
  ll i;
  costdp.resize(n, -1);
  for (i = 0; i < n; i++)
    cin >> height[i];
  cout << func(0);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define vb __int128
#define ll long long
#define ld long double
#define full(a) a.begin(), a.end()
#define stoink stack<ll>
#define vec vector<ll>
#define vg vector<vector<ll>>
#define vgw vector < vector<pair<ll, ll>>
#define pa pair<ll, ll>
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define pre 0.00000000000000000001
#define inf 1000000000000
#define mod 998244353
vec costdp;
vec height;
ll n;
ll func(ll ind) {
  if (ind > n - 1)
    return inf;
  if (costdp[ind] != -1)
    return costdp[ind];
  if (ind == n - 1)
    return 0;
  ll res = min(func(ind + 1) + abs(height[ind + 1] - height[ind]),
               func(ind + 2) + abs(height[ind + 2] - height[ind]));
  return costdp[ind] = res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  height.resize(n);
  ll i;
  costdp.resize(n, -1);
  for (i = 0; i < n; i++)
    cin >> height[i];
  cout << func(0);
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;

typedef long long ll;
typedef long long ull;
typedef pair<ll, ll> ii;

#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define endl "\n"
#define fx(n) fixed << setprecision(n)
#define mk make_pair

void File() {
#ifndef ONLINE_JUDGE
  freopen("output.txt", "w", stdout);
  freopen("input.txt", "r", stdin);
#endif
#ifdef ONLINE_JUDGE
  /*freopen("output.txt", "w", stdout);
  freopen("pyramid.in", "r", stdin);*/
#endif
}
void fast() {
  ios::sync_with_stdio(NULL);
  cout.tie(NULL);
  cin.tie(NULL);
  File();
}
const double pi = 2 * acos(0.0);
const ll oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 1e5 + 15;
int dx[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[8] = {0, 0, 1, -1, 1, -1, -1, 1};

int n;
ll arr[nn];
ll dp[nn];
int main() {
  fast();
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  memset(dp, oo, sizeof dp);
  dp[0] = 0;
  for (int i = 1; i < n; i++) {
    if (i == 1)
      dp[i] = abs(arr[i - 1] - arr[i]) + dp[0];
    else
      dp[i] = min({dp[i - 1] + abs(arr[i - 1] - arr[i]),
                   dp[i - 2] + abs(arr[i - 2] - arr[i])});
  }
  cout << dp[n - 1] << endl;
  return 0;
}

Fixed submission:
#define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;

typedef long long ll;
typedef long long ull;
typedef pair<ll, ll> ii;

#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define endl "\n"
#define fx(n) fixed << setprecision(n)
#define mk make_pair

void File() {
#ifndef ONLINE_JUDGE
  freopen("output.txt", "w", stdout);
  freopen("input.txt", "r", stdin);
#endif
#ifdef ONLINE_JUDGE
  /*freopen("output.txt", "w", stdout);
  freopen("pyramid.in", "r", stdin);*/
#endif
}
void fast() {
  ios::sync_with_stdio(NULL);
  cout.tie(NULL);
  cin.tie(NULL);
  // File();
}
const double pi = 2 * acos(0.0);
const ll oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 1e5 + 15;
int dx[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[8] = {0, 0, 1, -1, 1, -1, -1, 1};

int n;
ll arr[nn];
ll dp[nn];
int main() {
  fast();
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  memset(dp, oo, sizeof dp);
  dp[0] = 0;
  for (int i = 1; i < n; i++) {
    if (i == 1)
      dp[i] = abs(arr[i - 1] - arr[i]) + dp[0];
    else
      dp[i] = min({dp[i - 1] + abs(arr[i - 1] - arr[i]),
                   dp[i - 2] + abs(arr[i - 2] - arr[i])});
  }
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES // M_PI
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
using ll = long long;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define rREP(i, a, n) for (int(i) = (n)-1; (i) >= (a); --(i))
#define all(x) (x).begin(), (x).end()
#define out(y, x, h, w) (y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w)
constexpr int INF = 2147483647;
constexpr ll mod = 1000000007;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void init() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

int main() {
  init();

  int N;
  cin >> N;
  vector<int> h(N);
  rep(i, N) cin >> h[i];
  int dp[10001];
  fill(dp, dp + 10001, INF);
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);
  REP(i, 2, N) {
    dp[i] =
        min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << dp[N - 1] << "\n";

  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES // M_PI
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
using ll = long long;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define rREP(i, a, n) for (int(i) = (n)-1; (i) >= (a); --(i))
#define all(x) (x).begin(), (x).end()
#define out(y, x, h, w) (y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w)
constexpr int INF = 2147483647;
constexpr ll mod = 1000000007;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void init() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

int main() {
  init();

  int N;
  cin >> N;
  vector<int> h(N);
  rep(i, N) cin >> h[i];
  vector<int> dp(N, INF);
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);
  REP(i, 2, N) {
    dp[i] =
        min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << dp[N - 1] << "\n";

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <string>
#include <vector>
using namespace std::chrono;
using namespace std;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define ll long long

bool isPowerOfTwo(ll x) {
  /* First x in the below expression is for the case when x is 0 */
  return x && (!(x & (x - 1)));
}

bool SameSign(ll x, ll y) { return (x >= 0) ^ (y < 0); }

ll gcd(ll u, ll v) {
  ll shift;
  if (u == 0)
    return v;
  if (v == 0)
    return u;
  shift = __builtin_ctz(u | v);
  u >>= __builtin_ctz(u);
  do {
    v >>= __builtin_ctz(v);
    if (u > v) {
      ll t = v;
      v = u;
      u = t;
    }
    v = v - u;
  } while (v != 0);
  return u << shift;
}

ll lcm(ll u, ll v) { return u * v / gcd(u, v); }

int isPower(int A) {
  int i = 2;
  int n;

  if (A == 1) {
    return true;
  } else {
    while (1) {
      n = A;
      int count = 0;
      while (n % i == 0) {
        n = n / i;
        count++;
      }
      if (n == 1 && count > 1) {
        return true;
      } else if (n == 1 && count <= 1)
        return false;
      i++;
    }
    return false;
  }
}
#define PI 3.14159265358979323846

// 30 10 60 10 60 50
// dp[6]=dp[5]+10;
// dp[5]=dp[3]+0;
// dp[3]=dp[1]+30;
// dp[1]=0;

int dp[100] = {0};

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

Fixed submission:
#include <bits/stdc++.h>
#include <string>
#include <vector>
using namespace std::chrono;
using namespace std;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define ll long long

bool isPowerOfTwo(ll x) {
  /* First x in the below expression is for the case when x is 0 */
  return x && (!(x & (x - 1)));
}

bool SameSign(ll x, ll y) { return (x >= 0) ^ (y < 0); }

ll gcd(ll u, ll v) {
  ll shift;
  if (u == 0)
    return v;
  if (v == 0)
    return u;
  shift = __builtin_ctz(u | v);
  u >>= __builtin_ctz(u);
  do {
    v >>= __builtin_ctz(v);
    if (u > v) {
      ll t = v;
      v = u;
      u = t;
    }
    v = v - u;
  } while (v != 0);
  return u << shift;
}

ll lcm(ll u, ll v) { return u * v / gcd(u, v); }

int isPower(int A) {
  int i = 2;
  int n;

  if (A == 1) {
    return true;
  } else {
    while (1) {
      n = A;
      int count = 0;
      while (n % i == 0) {
        n = n / i;
        count++;
      }
      if (n == 1 && count > 1) {
        return true;
      } else if (n == 1 && count <= 1)
        return false;
      i++;
    }
    return false;
  }
}
#define PI 3.14159265358979323846

// 30 10 60 10 60 50
// dp[6]=dp[5]+10;
// dp[5]=dp[3]+0;
// dp[3]=dp[1]+30;
// dp[1]=0;

int dp[100050] = {0};

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

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

int minCost(int *arr, int n, int curr, int *dp) {
  if (curr == n)
    return 0;
  if (curr == n - 1) {
    return abs(arr[curr] - arr[n]);
  }

  if (dp[curr] != -1)
    return dp[curr];

  int oneJump = abs(arr[curr] - arr[curr + 1]) + minCost(arr, n, curr + 1, dp);
  int twoJump = abs(arr[curr] - arr[curr + 2]) + minCost(arr, n, curr + 2, dp);
  dp[curr] = min(oneJump, twoJump);
  return dp[curr];
}

int main() {
  int n;
  cin >> n;
  int *arr = new int[n + 1];
  for (int i = 1; i <= n; i++) {
    int h;
    cin >> h;
    arr[i] = h;
  }

  int *dp = new int[n + 1];
  memset(dp, -1, sizeof(dp[0]) * (n + 1));

  return minCost(arr, n, 1, dp);
}

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

int minCost(int *arr, int n, int curr, int *dp) {
  if (curr == n)
    return 0;
  if (curr == n - 1) {
    return abs(arr[curr] - arr[n]);
  }

  if (dp[curr] != -1)
    return dp[curr];

  int oneJump = abs(arr[curr] - arr[curr + 1]) + minCost(arr, n, curr + 1, dp);
  int twoJump = abs(arr[curr] - arr[curr + 2]) + minCost(arr, n, curr + 2, dp);
  dp[curr] = min(oneJump, twoJump);
  return dp[curr];
}

int main() {
  int n;
  cin >> n;
  int *arr = new int[n + 1];
  for (int i = 1; i <= n; i++) {
    int h;
    cin >> h;
    arr[i] = h;
  }

  int *dp = new int[n + 1];
  memset(dp, -1, sizeof(dp[0]) * (n + 1));

  cout << minCost(arr, n, 1, dp) << endl;
  return 0;
}