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

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

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl '\n'
#define ll long long
#define inf 1000000007
int main() {
  fastio;
#ifdef APNA_IO
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  ll n;
  cin >> n;
  ll arr[n];
  ll dp[n];
  for (ll i = 0; i < n; i++) {
    cin >> arr[i];
  }
  dp[0] = 0;
  dp[1] = abs(arr[1] - arr[0]);
  for (ll i = 2; i < n; i++) {
    dp[i] = min(abs(arr[i] - arr[i - 1]) + dp[i - 1],
                abs(arr[i] - arr[i - 2]) + dp[i - 2]);
  }
  cout << dp[n - 1] << endl;

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
vector<int> stones;
vector<int> costs;
int hops(int stone) {
  if (stone < 1) {
    return 0;
  }
  if (stone == 1) {
    return abs(costs[stone] - costs[stone - 1]);
  }
  if (stones[stone] != -1) {
    return stones[stone];
  }
  int cost1 = hops(stone - 1) + abs(costs[stone] - costs[stone - 1]);
  int cost2 = hops(stone - 2) + abs(costs[stone] - costs[stone - 2]);
  int preferable = min(cost1, cost2);
  stones[stone] = preferable;
  return preferable;
}
int main() {
  int n;
  cin >> n;
  stones.assign(1000000000, -1);
  for (int i = 0; i < n; i++) {
    int cost;
    cin >> cost;
    costs.push_back(cost);
  }
  cout << hops(n - 1) << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
vector<int> stones;
vector<int> costs;
int hops(int stone) {
  if (stone < 1) {
    return 0;
  }
  if (stone == 1) {
    return abs(costs[stone] - costs[stone - 1]);
  }
  if (stones[stone] != -1) {
    return stones[stone];
  }
  int cost1 = hops(stone - 1) + abs(costs[stone] - costs[stone - 1]);
  int cost2 = hops(stone - 2) + abs(costs[stone] - costs[stone - 2]);
  int preferable = min(cost1, cost2);
  stones[stone] = preferable;
  return preferable;
}
int main() {
  int n;
  cin >> n;
  stones.assign(n, -1);
  for (int i = 0; i < n; i++) {
    int cost;
    cin >> cost;
    costs.push_back(cost);
  }
  cout << hops(n - 1) << endl;
}
Problem p03160 (C++)

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

#define LL long long

#define FOR(INDEX, START, END) for (LL INDEX = (START); INDEX < (END); ++INDEX)
#define REP(INDEX, COUNT) for (LL INDEX = 0; INDEX < (COUNT); ++INDEX)

vector<LL> InputVec(LL size) {
  vector<LL> vec((size_t)size);
  REP(i, size) { cin >> vec[(size_t)i]; }
  return vec;
}

vector<LL> tbl;

LL CalcCost(vector<LL> const &hs, int i) {
  if (i == hs.size() - 1) {
    return 0;
  }

  if (tbl[i] >= 0) {
    // return tbl[i];
  }

  LL cost = 0;
  {
    LL c = abs(hs[i] - hs[i + 1]);
    c += CalcCost(hs, i + 1);
    cost = c;
  }

  if (i + 2 < hs.size()) {
    LL c = abs(hs[i] - hs[i + 2]);
    c += CalcCost(hs, i + 2);
    cost = min(cost, c);
  }

  tbl[i] = cost;

  return cost;
}

int main() {
  LL N;
  cin >> N;
  vector<LL> hs = InputVec(N);
  tbl.resize(N, -1);

  LL cost = CalcCost(hs, 0);
  cout << cost << endl;

  return 0;
}


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

#define LL long long

#define FOR(INDEX, START, END) for (LL INDEX = (START); INDEX < (END); ++INDEX)
#define REP(INDEX, COUNT) for (LL INDEX = 0; INDEX < (COUNT); ++INDEX)

vector<LL> InputVec(LL size) {
  vector<LL> vec((size_t)size);
  REP(i, size) { cin >> vec[(size_t)i]; }
  return vec;
}

vector<LL> tbl;

LL CalcCost(vector<LL> const &hs, int i) {
  if (i == hs.size() - 1) {
    return 0;
  }

  if (tbl[i] >= 0) {
    return tbl[i];
  }

  LL cost = 0;
  {
    LL c = abs(hs[i] - hs[i + 1]);
    c += CalcCost(hs, i + 1);
    cost = c;
  }

  if (i + 2 < hs.size()) {
    LL c = abs(hs[i] - hs[i + 2]);
    c += CalcCost(hs, i + 2);
    cost = min(cost, c);
  }

  tbl[i] = cost;

  return cost;
}

int main() {
  LL N;
  cin >> N;
  vector<LL> hs = InputVec(N);
  tbl.resize(N, -1);

  LL cost = CalcCost(hs, 0);
  cout << cost << endl;

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int n;
int dp[10010];
int height[10010];

int main() {
  cin >> n;
  dp[0] = 9999999;
  for (int i = 1; i <= n; i++) {
    dp[i] = 9999999;
    int a;
    cin >> a;
    height[i] = a;
  }

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

  for (int i = 3; 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] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int n;
int dp[100100];
int height[100100];

int main() {
  cin >> n;
  dp[0] = 9999999;
  for (int i = 1; i <= n; i++) {
    dp[i] = 9999999;
    int a;
    cin >> a;
    height[i] = a;
  }

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

  for (int i = 3; 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] << endl;

  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> vec;
  for (int i = 0; i < n; i++)
    cin >> vec[i];
  int dp[n + 1];
  dp[0] = 0;
  dp[1] = 0;

  for (int j = 2; j <= n; j++) {
    if (j > 2)
      dp[j] = min(dp[j - 1] + abs(vec[j - 1] - vec[j - 2]),
                  dp[j - 2] + abs(vec[j - 1] - vec[j - 3]));
    else
      dp[j] = dp[j - 1] + abs(vec[j - 1] - vec[j - 2]);
  }
  cout << dp[n] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> vec(n);
  for (int i = 0; i < n; i++)
    cin >> vec[i];
  int dp[n + 1];
  dp[0] = 0;
  dp[1] = 0;

  for (int j = 2; j <= n; j++) {
    if (j > 2)
      dp[j] = min(dp[j - 1] + abs(vec[j - 1] - vec[j - 2]),
                  dp[j - 2] + abs(vec[j - 1] - vec[j - 3]));
    else
      dp[j] = dp[j - 1] + abs(vec[j - 1] - vec[j - 2]);
  }
  cout << dp[n] << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long int
using namespace std;
int ans1 = 0, ans2 = 0;
const int N = 1e4 + 5;
int arr[N];
int recur(int n) {
  int dp[n];
  dp[0] = 0;

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

    else {
      dp[i] = min(dp[i - 1] + abs(arr[i] - arr[i - 1]),
                  dp[i - 2] + abs(arr[i] - arr[i - 2]));
    }
  }
  return dp[n - 1];
}
signed main() {

  int n;
  cin >> n;

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

  cout << recur(n) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long int
using namespace std;
int ans1 = 0, ans2 = 0;
const int N = 1e5 + 5;
int arr[N];
int recur(int n) {
  int dp[n];
  dp[0] = 0;

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

    else {
      dp[i] = min(dp[i - 1] + abs(arr[i] - arr[i - 1]),
                  dp[i - 2] + abs(arr[i] - arr[i - 2]));
    }
  }
  return dp[n - 1];
}
signed main() {

  int n;
  cin >> n;

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

  cout << recur(n) << endl;

  return 0;
}
Problem p03160 (C++)

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

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;
  int a[n], dp[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    dp[i] = INT_MAX;
  }
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    if (i + 1 < n)
      dp[i + 1] = min(dp[i + 1], abs(a[i + 1] - a[i]) + dp[i]);
    if (i + 2 < n)
      dp[i + 2] = min(dp[i + 2], abs(a[i + 2] - a[i]) + dp[i]);
  }
  cout << dp[n - 1];
}

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

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;
  int a[n], dp[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    dp[i] = INT_MAX;
  }
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    if (i + 1 < n)
      dp[i + 1] = min(dp[i + 1], abs(a[i + 1] - a[i]) + dp[i]);
    if (i + 2 < n)
      dp[i + 2] = min(dp[i + 2], abs(a[i + 2] - a[i]) + dp[i]);
  }
  cout << dp[n - 1];
}
Problem p03160 (C++)

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

#define lint long long
#define P pair<int, int>
#define LLP pair<long long, long long>
#define REP(i, x, n) for (int i = x; i < n; ++i)
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define SORT(x) sort((x).begin(), (x).end())

const int IINF = 1e9 + 10;
const long long LLINF = (long long)1e18 + 10;
const long long MOD = (long long)1e9 + 7;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1},
          dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

int n;
vector<int> h;
int memo[100010];

int solve(int i) {
  if (i == 0) {
    return 0;
  }
  if (i == 1) {
    return abs(h[1] - h[0]);
  }
  if (memo[i] >= 0) {
    return memo[i];
  }
  return min(solve(i - 1) + abs(h[i] - h[i - 1]),
             solve(i - 2) + abs(h[i] - h[i - 2]));
}

int main() {
  cin >> n;
  h.resize(n);
  rep(i, n) { cin >> h[i]; }
  fill(memo, memo + n, -1);
  int ans = solve(n - 1);
  cout << ans << endl;
  return 0;
}

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

#define lint long long
#define P pair<int, int>
#define LLP pair<long long, long long>
#define REP(i, x, n) for (int i = x; i < n; ++i)
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define SORT(x) sort((x).begin(), (x).end())

const int IINF = 1e9 + 10;
const long long LLINF = (long long)1e18 + 10;
const long long MOD = (long long)1e9 + 7;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1},
          dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

int n;
vector<int> h;
int memo[100010];

int solve(int i) {
  if (i == 0) {
    return 0;
  }
  if (i == 1) {
    return abs(h[1] - h[0]);
  }
  if (memo[i] >= 0) {
    return memo[i];
  }
  return memo[i] = min(solve(i - 1) + abs(h[i] - h[i - 1]),
                       solve(i - 2) + abs(h[i] - h[i - 2]));
}

int main() {
  cin >> n;
  h.resize(n);
  rep(i, n) { cin >> h[i]; }
  fill(memo, memo + n, -1);
  int ans = solve(n - 1);
  cout << ans << endl;
  return 0;
}
Problem p03160 (C++)

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

using namespace std;
typedef long long ll;

ll dp[1000];
ll arr[1000];
int n = 0;
ll dpfun(int i) {
  if (dp[i] != -1)
    return dp[i];
  if (i == n - 1)
    return dp[i] = 0;
  if (i == n - 2)
    return dp[i] = abs(arr[i] - arr[i + 1]);
  else
    return dp[i] = min(abs(arr[i] - arr[i + 1]) + dpfun(i + 1),
                       abs(arr[i] - arr[i + 2]) + dpfun(i + 2));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  memset(dp, -1, sizeof dp);
  int i;
  for (i = 0; i < n; i++)
    cin >> arr[i];

  cout << dpfun(0) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <math.h>
#include <utility>

using namespace std;
typedef long long ll;

ll dp[100005];
ll arr[100005];
int n = 0;
ll dpfun(int i) {
  if (dp[i] != -1)
    return dp[i];
  if (i == n - 1)
    return dp[i] = 0;
  if (i == n - 2)
    return dp[i] = abs(arr[i] - arr[i + 1]);
  else
    return dp[i] = min(abs(arr[i] - arr[i + 1]) + dpfun(i + 1),
                       abs(arr[i] - arr[i + 2]) + dpfun(i + 2));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  memset(dp, -1, sizeof dp);
  int i;
  for (i = 0; i < n; i++)
    cin >> arr[i];

  cout << dpfun(0) << endl;
  return 0;
}
Problem p03160 (C++)

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

typedef long long ll;
const ll INF = 1LL << 60;

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

int main(void) {
  int N;
  cin >> N;
  ll h[N];
  ll dp[N];

  for (int i = 1; i <= N; i++) {
    cin >> h[i];
  }
  for (int i = 1; i <= N; i++) {
    dp[i] = INF;
  }

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

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


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

typedef long long ll;
const ll INF = 1LL << 60;

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

int main(void) {
  int N;
  cin >> N;
  ll h[100010];
  ll dp[100010];

  for (int i = 1; i <= N; i++) {
    cin >> h[i];
  }
  for (int i = 1; i <= N; i++) {
    dp[i] = INF;
  }

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
const int mod = 1e9 + 7;
const int INF = 1 << 20;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int dp[10010];

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

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
const int mod = 1e9 + 7;
const int INF = 1 << 20;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int dp[100100];

int main() {
  int n;
  cin >> n;
  int h[n];
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 0; i < n - 2; i++) {
    dp[i + 2] =
        min(dp[i] + abs(h[i + 2] - h[i]), dp[i + 1] + abs(h[i + 2] - h[i + 1]));
  }
  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> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  // i番目に到達するために必要なコストの最小値をdp[i]とする。
  int dp[10010];
  // dpテーブルの初期化
  dp[0] = 0;
  dp[1] = 0;
  dp[2] = abs(h[1] - h[0]);
  for (int i = 2; i < n; i++) {
    dp[i + 1] =
        min(dp[i] + abs(h[i] - h[i - 1]), dp[i - 1] + abs(h[i] - h[i - 2]));
  }
  cout << dp[n] << endl;
}


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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  // i番目に到達するために必要なコストの最小値をdp[i]とする。
  int dp[n];
  // dpテーブルの初期化
  dp[0] = 0;
  dp[1] = 0;
  dp[2] = abs(h[1] - h[0]);
  for (int i = 2; i < n; i++) {
    dp[i + 1] =
        min(dp[i] + abs(h[i] - h[i - 1]), dp[i - 1] + abs(h[i] - h[i - 2]));
  }
  cout << dp[n] << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

int n, x, dp[10001];
vector<int> a;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n && cin >> x; i++)
    a.push_back(x);
  dp[1] = abs(a[1] - a[0]);
  for (int i = 2; 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] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

int n, x, dp[100001];
vector<int> a;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n && cin >> x; i++)
    a.push_back(x);
  dp[1] = abs(a[1] - a[0]);
  for (int i = 2; 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] << endl;
}
Problem p03160 (C++)

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

#define int long long
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;
}
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define COUT(x) cout << x << endl;
#define PB(x) push_back(x)
#define MP make_pair
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

signed main() {
  int n;
  cin >> n;
  vector<int> h(n + 1);
  rep(i, n) cin >> h[i + 1];
  vector<int> dp(10010, INF);
  dp[0] = 0;
  dp[1] = 0;
  dp[2] = abs(h[2] - h[1]);
  for (int i = 1; i < n - 1; i++) {
    chmin(dp[i + 2], dp[i + 1] + abs(h[i + 2] - h[i + 1]));
    chmin(dp[i + 2], dp[i] + abs(h[i + 2] - h[i]));
  }
  // for(int i=1; i<=n; i++) cout<<dp[i]<<' ';
  cout << dp[n] << endl;
}

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

#define int long long
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;
}
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define COUT(x) cout << x << endl;
#define PB(x) push_back(x)
#define MP make_pair
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

signed main() {
  int n;
  cin >> n;
  vector<int> h(n + 1);
  rep(i, n) cin >> h[i + 1];
  vector<int> dp(100100, INF);
  dp[0] = 0;
  dp[1] = 0;
  dp[2] = abs(h[2] - h[1]);
  for (int i = 1; i < n - 1; i++) {
    chmin(dp[i + 2], dp[i + 1] + abs(h[i + 2] - h[i + 1]));
    chmin(dp[i + 2], dp[i] + abs(h[i + 2] - h[i]));
  }
  // for(int i=1; i<=n; i++) cout<<dp[i]<<' ';
  cout << dp[n] << endl;
}
Problem p03160 (C++)

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

using namespace std;

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;
}

const long long INF = 1LL << 60;
const int MAX = 10000;
long long h[MAX], dp[MAX];

void solve() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> h[i];
    dp[i] = INF;
  }
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 0; i < N - 1; i++) {
    // dp[i] = min(dp[i-1] + abs(h[i]-h[i-1]), dp[i-2] + abs(h[i]-h[i-2]));
    if (i != N - 2)
      chmin(dp[i + 2], dp[i] + abs(h[i + 2] - h[i]));
    chmin(dp[i + 1], dp[i] + abs(h[i + 1] - h[i]));
    // chmin(dp[i], dp[i-1] + abs(h[i]-h[i-1]));
    // chmin(dp[i], dp[i-2] + abs(h[i]-h[i-2]));
  }
  cout << dp[N - 1] << endl;
  return;
}

int main() {
  solve();
  return 0;
}

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

using namespace std;

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;
}

const long long INF = 1LL << 60;
const int MAX = 100000;
long long h[MAX], dp[MAX];

void solve() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> h[i];
    dp[i] = INF;
  }
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 0; i < N - 1; i++) {
    // dp[i] = min(dp[i-1] + abs(h[i]-h[i-1]), dp[i-2] + abs(h[i]-h[i-2]));
    if (i != N - 2)
      chmin(dp[i + 2], dp[i] + abs(h[i + 2] - h[i]));
    chmin(dp[i + 1], dp[i] + abs(h[i + 1] - h[i]));
    // chmin(dp[i], dp[i-1] + abs(h[i]-h[i-1]));
    // chmin(dp[i], dp[i-2] + abs(h[i]-h[i-2]));
  }
  cout << dp[N - 1] << endl;
  return;
}

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0), cin.tie(0);
int main() {
  IOS;
  int n;
  cin >> n;
  int dp[n];
  vector<int> H;
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    H.push_back(temp);
  }
  bool change = true;
  for (int i = 1; i < n; i++) {
    dp[i] = abs(H[i] - H[i - 1]) + dp[i - 1];
  }
  while (change) {
    change = false;
    for (int i = 2; i < n; i++) {
      int t = abs(H[i] - H[i - 2]) + dp[i - 2];
      if (t < dp[i]) {
        dp[i] = t;
        change = true;
      }
    }
    if (!change)
      break;
    for (int i = 1; i < n; i++) {
      int t = abs(H[i] - H[i - 1]) + dp[i - 1];
      if (t < dp[i]) {
        dp[i] = t;
        change = true;
      }
    }
  }
  cout << dp[n - 1];
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0), cin.tie(0);
int main() {
  IOS;
  int n;
  cin >> n;
  int dp[n];
  vector<int> H;
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    H.push_back(temp);
  }
  bool change = true;
  dp[1] = abs(H[1] - H[0]) + dp[0];
  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]);
  } /*
   int flag=0;
   while(change)
   {
       change= false;
       for(int i=2;i<n;i++)
       {
           int t=abs(H[i]-H[i-2])+dp[i-2];
           if(t<dp[i])
           {
               dp[i]= t;
               change= true;
           }
       }
       if(!change)
           break;
       for(int i=1;i<n;i++)
       {
           int t=abs(H[i]-H[i-1])+dp[i-1];
           if(t<dp[i])
           {
               dp[i]= t;
               change= true;
           }
       }
   }*/
  cout << dp[n - 1];
}
Problem p03160 (C++)

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

#define endl "\n"
#define all(V) V.begin(), V.end()
#define see(x) cerr << #x << " = " << x << "\n";
#define Unique(V) sort(all(V)), V.erase(unique(all(V)), V.end())
typedef long long int LL;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const double PI = acos(-1.0);
const long long INF = 1e18;
const int N = 2e3 + 5;

int n, a[N];
int dp[N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    dp[i] = inf;
  dp[n] = 0;
  for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= 2; j++) {
      if (i + j <= n) {
        dp[i] = min(dp[i], dp[i + j] + abs(a[i + j] - a[i]));
      }
    }
  }
  cout << dp[1] << endl;
  return 0;
}


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

#define endl "\n"
#define all(V) V.begin(), V.end()
#define see(x) cerr << #x << " = " << x << "\n";
#define Unique(V) sort(all(V)), V.erase(unique(all(V)), V.end())
typedef long long int LL;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const double PI = acos(-1.0);
const long long INF = 1e18;
const int N = 1e5 + 5;

int n, a[N];
int dp[N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    dp[i] = inf;
  dp[n] = 0;
  for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= 2; j++) {
      if (i + j <= n) {
        dp[i] = min(dp[i], dp[i + j] + abs(a[i + j] - a[i]));
      }
    }
  }
  cout << dp[1] << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
int h[N];
int dp[N];

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
int h[N];
int dp[N];

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

  return 0;
}
Problem p03160 (C++)

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

int n, h[5000], dp[5000];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  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(dp[i - 2] + abs(h[i] - h[i - 2]), dp[i - 1] + abs(h[i] - h[i - 1]));
  }
  cout << dp[n - 1];
  return 0;
}

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

int n, h[10000000], dp[10000000];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  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(dp[i - 2] + abs(h[i] - h[i - 2]), dp[i - 1] + abs(h[i] - h[i - 1]));
  }
  cout << dp[n - 1];
  return 0;
}
Problem p03160 (C++)

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

template <typename T> T load() {
  T r;
  cin >> r;
  return r;
}
template <typename T> vector<T> loadMany(int n) {
  vector<T> rs(n);
  generate(rs.begin(), rs.end(), &load<T>);
  return rs;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  while (true)
    new int[10000];
  auto n = load<int>();
  auto height = loadMany<int>(n);
  auto dyn = vector<int>(n);
  dyn[0] = 0;
  for (auto i = 1; i < n; ++i) {
    dyn[i] = dyn[i - 1] + abs(height[i - 1] - height[i]);
    if (i >= 2)
      dyn[i] = min(dyn[i], dyn[i - 2] + abs(height[i] - height[i - 2]));
  }

  cout << dyn[n - 1] << '\n';
}


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

template <typename T> T load() {
  T r;
  cin >> r;
  return r;
}
template <typename T> vector<T> loadMany(int n) {
  vector<T> rs(n);
  generate(rs.begin(), rs.end(), &load<T>);
  return rs;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  auto n = load<int>();
  auto height = loadMany<int>(n);
  auto dyn = vector<int>(n);
  dyn[0] = 0;
  for (auto i = 1; i < n; ++i) {
    dyn[i] = dyn[i - 1] + abs(height[i - 1] - height[i]);
    if (i >= 2)
      dyn[i] = min(dyn[i], dyn[i - 2] + abs(height[i] - height[i - 2]));
  }

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

Buggy submission (status: Runtime Error):
// 98tarunkumar

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

#define ll long long
#define pb push_back
#define lop(i, n) for (int i = 0; i < n; i++)
#define rlop(i, n) for (int i = n; i > 0; i--)
#define lli long long int
#define MOD 1000000007
#define f(i, a, b) for (i = a; i < b; i++)
#define ainp(a, n)                                                             \
  for (int i = 0; i < n; i++)                                                  \
    cin >> a[i];
#define aout(a, n)                                                             \
  for (int i = 0; i < n; i++)                                                  \
    cout << a[i] << " ";
#define ss                                                                     \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);
#define vi vector<int>
#define ln cout << "\n"

int main() {
  ss;
#ifndef ONLINE_JUDGE
  freopen("cf_inp.txt", "r", stdin);
  freopen("cf_oup.txt", "w", stdout);
#endif

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

  ainp(a, n) if (n == 1) {
    cout << 0;
    ln;
  }
  else if (n == 2) {
    cout << a[1] - a[0];
    ln;
  }
  else {
    int dp[n];
    dp[0] = 0;
    dp[1] = abs(a[1] - a[0]);
    for (int i = 2; i < n; i++) {
      dp[i] = min(abs(a[i] - a[i - 2]) + dp[i - 2],
                  dp[i - 1] + abs(a[i] - a[i - 1]));
    }
    // aout(dp,n)ln;
    cout << dp[n - 1];
  }
}


Fixed submission:
// 98tarunkumar

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

#define ll long long
#define pb push_back
#define lop(i, n) for (int i = 0; i < n; i++)
#define rlop(i, n) for (int i = n; i > 0; i--)
#define lli long long int
#define MOD 1000000007
#define f(i, a, b) for (i = a; i < b; i++)
#define ainp(a, n)                                                             \
  for (int i = 0; i < n; i++)                                                  \
    cin >> a[i];
#define aout(a, n)                                                             \
  for (int i = 0; i < n; i++)                                                  \
    cout << a[i] << " ";
#define ss                                                                     \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);
#define vi vector<int>
#define ln cout << "\n"

int main() {
  ss;
  /*#ifndef ONLINE_JUDGE
  freopen("cf_inp.txt","r",stdin);
  freopen("cf_oup.txt","w",stdout);
  #endif*/

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

  ainp(a, n) if (n == 1) {
    cout << 0;
    ln;
  }
  else if (n == 2) {
    cout << a[1] - a[0];
    ln;
  }
  else {
    int dp[n];
    dp[0] = 0;
    dp[1] = abs(a[1] - a[0]);
    for (int i = 2; i < n; i++) {
      dp[i] = min(abs(a[i] - a[i - 2]) + dp[i - 2],
                  dp[i - 1] + abs(a[i] - a[i - 1]));
    }
    // aout(dp,n)ln;
    cout << dp[n - 1];
  }
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
// i_am_arin

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

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<long long> vll;
typedef vector<string> vs;

#define f(i, a, n) for (ll i = a; i < (ll)n; i++)
#define f2(i, a, b) for (ll i = a; i <= (ll)b; i++)
#define PB push_back
#define FF first
#define SS second
const ll MOD = 1e9 + 7;
const ll INF = LLONG_MAX;

ll dp[100005];
ll a[100005];
ll n;
void fill() { f(i, 0, 100005) dp[i] = -1; }

ll go(ll n) {
  if (n == 2)
    return abs(a[n] - a[n - 1]);
  if (n == 1)
    return 0;
  if (n < 0)
    return INF;
  // if(dp[n]!=-1) return dp[n];

  return dp[n] = min((go(n - 1) + abs(a[n] - a[n - 1])),
                     go(n - 2) + abs(a[n] - a[n - 2]));
}

void solve() {
  cin >> n;
  fill();
  f(i, 1, n + 1) cin >> a[i];
  cout << go(n) << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
// i_am_arin

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

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<long long> vll;
typedef vector<string> vs;

#define f(i, a, n) for (ll i = a; i < (ll)n; i++)
#define f2(i, a, b) for (ll i = a; i <= (ll)b; i++)
#define PB push_back
#define FF first
#define SS second
const ll MOD = 1e9 + 7;
const ll INF = LLONG_MAX;

ll dp[100005];
ll a[100005];
ll n;
void fill() { f(i, 0, 100005) dp[i] = -1; }

ll go(ll n) {
  if (n == 2)
    return abs(a[n] - a[n - 1]);
  if (n == 1)
    return 0;
  if (n < 0)
    return INF;
  if (dp[n] != -1)
    return dp[n];

  return dp[n] = min((go(n - 1) + abs(a[n] - a[n - 1])),
                     go(n - 2) + abs(a[n] - a[n - 2]));
}

void solve() {
  cin >> n;
  fill();
  f(i, 1, n + 1) cin >> a[i];
  cout << go(n) << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p03160 (C++)

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

using namespace std;
const int MAX_T = 10100;
const long long INF = 1LL << 60;

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int main() {
  int N;
  cin >> N;
  long long h[MAX_T];
  long long dp[MAX_T];
  for (int i = 0; i < N; i++)
    scanf("%lld", &h[i]);

  for (int i = 0; i < N; i++) {
    dp[i] = INF;
  }

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

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

  return 0;
}

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

using namespace std;
const int MAX_T = 100100;
const long long INF = 1LL << 60;

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int main() {
  int N;
  cin >> N;
  long long h[MAX_T];
  long long dp[MAX_T];
  for (int i = 0; i < N; i++)
    scanf("%lld", &h[i]);

  for (int i = 0; i < N; i++) {
    dp[i] = INF;
  }

  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    chmin(dp[i + 1], dp[i] + abs(h[i] - h[i + 1]));
    chmin(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>

using namespace std;
#define tam 10001
int dp[tam], A[tam], n;
int solver(int index) {
  if (dp[index] != -1)
    return dp[index];
  if (index <= 1)
    return dp[index] = abs(A[0] - A[index]);
  return dp[index] = min(solver(index - 1) + abs(A[index] - A[index - 1]),
                         solver(index - 2) + abs(A[index] - A[index - 2]));
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  memset(dp, -1, sizeof dp);
  dp[0] = 0;
  cout << solver(n - 1) << "\n";
  return 0;
}


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

using namespace std;
#define tam 100001
int dp[tam], A[tam], n, pr = 0;
int solver(int index) {
  if (dp[index] != -1)
    return dp[index];
  if (index <= 1)
    return dp[index] = abs(A[0] - A[index]);
  return dp[index] = min(solver(index - 1) + abs(A[index] - A[index - 1]),
                         solver(index - 2) + abs(A[index] - A[index - 2]));
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  memset(dp, -1, sizeof dp);
  dp[0] = 0;
  cout << solver(n - 1) << "\n";
  return 0;
}
Problem p03160 (C++)

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

int main() {
  int N, i;
  cin >> N;
  int *h = new int(N);
  int *d = new int(N);

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

  d[0] = 0;
  d[1] = abs(h[1] - h[0]);
  for (i = 2; i < N; i++)
    d[i] =
        min(d[i - 1] + abs(h[i] - h[i - 1]), d[i - 2] + abs(h[i] - h[i - 2]));
  cout << d[N - 1] << endl;
  return 0;
}

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

int main() {
  int N, i;
  cin >> N;
  int h[100000];
  int d[100000];

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

  d[0] = 0;
  d[1] = abs(h[1] - h[0]);
  for (i = 2; i < N; i++)
    d[i] =
        min(d[i - 1] + abs(h[i] - h[i - 1]), d[i - 2] + abs(h[i] - h[i - 2]));
  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 ll;
#define IOS                                                                    \
  ;                                                                            \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define MX 80
#define FF                                                                     \
  freopen("input.in", "r", stdin);                                             \
  freopen("output.txt", "w", stdout);
int n;
int a[MX];
int dp[MX];
int f(int i) {
  if (i == n)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  if (i < n - 1)
    return dp[i] = min(f(i + 1) + abs(a[i] - a[i + 1]),
                       f(i + 2) + abs(a[i] - a[i + 2]));
  else
    return dp[i] = f(i + 1) + abs(a[i] - a[i + 1]);
}
int main() {
  // FF;
  IOS;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  memset(dp, -1, sizeof dp);
  cout << f(1);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS                                                                    \
  ;                                                                            \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define MX 100001
#define FF                                                                     \
  freopen("input.in", "r", stdin);                                             \
  freopen("output.txt", "w", stdout);
int n;
int a[MX];
int dp[MX];
int f(int i) {
  if (i == n)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  if (i < n - 1)
    return dp[i] = min(f(i + 1) + abs(a[i] - a[i + 1]),
                       f(i + 2) + abs(a[i] - a[i + 2]));
  else
    return dp[i] = f(i + 1) + abs(a[i] - a[i + 1]);
}
int main() {
  // FF;
  IOS;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  memset(dp, -1, sizeof dp);
  cout << f(1);
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fast                                                                   \
  ios::sync_with_stdio(NULL);                                                  \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define make_pair mp
#define pb push_back
#define ALL(s) s.begin(), s.end()
using namespace std;
typedef long long ll;
const int maxn = 1 << 21;
const int mod = 1e9 + 7;
const int INF = 1e9 + 5;
int h[100005];
int main() {
  int n;
  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; i++) {
    for (int j = i + 1; j <= i + 2; i++) {
      if (j < n)
        dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fast                                                                   \
  ios::sync_with_stdio(NULL);                                                  \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define make_pair mp
#define pb push_back
#define ALL(s) s.begin(), s.end()
using namespace std;
typedef long long ll;
const int maxn = 1 << 21;
const int mod = 1e9 + 7;
const int INF = 1e9 + 5;
int h[100005];
int main() {
  int n;
  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; i++) {
    if (i + 1 < n)
      dp[i + 1] = min(dp[i + 1], dp[i] + abs(h[i] - h[i + 1]));
    if (i + 2 < n)
      dp[i + 2] = min(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>
using namespace std;
#define LL long long
#define N 100005
#define MOD 1000000007
#define dd double
#define fin for (int i = 1; i <= n; i++)
#define fiab for (int i = a; i < b; i++)
#define fi1b for (int i = 1; i <= b; i++)
#define pb push_back
#define mp make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define F first
#define S second
int m;
int h[100004];
int n;
LL dp[100004];
long frog1(int i) {

  if (i == 1) {
    return 0;
  }
  if (i == 2) {
    return (abs(h[i] - h[i - 1]));
  }
  LL &ans = dp[i];
  if (ans != -1) {
    return ans;
  }
  ans = min(frog1(i - 1) + abs(h[i] - h[i - 1]),
            frog1(i - 2) + abs(h[i] - h[i - 2]));
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ios_base &scientific(ios_base & str);

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  // this can be opted out if you want to print the output to the sublime
  // console
  freopen("output.txt", "w", stdout);
#endif

  memset(dp, -1, sizeof(dp));
  cin >> n;
  m = n;

  fin { cin >> h[i]; }
  long p = frog1(n);
  cout << p << endl;
  return 0;
}

Fixed submission:
/******************************************
******************************************/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define N 100005
#define MOD 1000000007
#define dd double
#define fin for (int i = 1; i <= n; i++)
#define fiab for (int i = a; i < b; i++)
#define fi1b for (int i = 1; i <= b; i++)
#define pb push_back
#define mp make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define F first
#define S second
int m;
int h[100004];
int n;
LL dp[100004];
long frog1(int i) {

  if (i == 1) {
    return 0;
  }
  if (i == 2) {
    return (abs(h[i] - h[i - 1]));
  }
  LL &ans = dp[i];
  if (ans != -1) {
    return ans;
  }
  ans = min(frog1(i - 1) + abs(h[i] - h[i - 1]),
            frog1(i - 2) + abs(h[i] - h[i - 2]));
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ios_base &scientific(ios_base & str);

  memset(dp, -1, sizeof(dp));
  cin >> n;
  m = n;

  fin { cin >> h[i]; }
  long p = frog1(n);
  cout << p << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define endl '\n'
#define init(n)                                                                \
  int n;                                                                       \
  cin >> n;                                                                    \
  int a[n];                                                                    \
  rep(i, n) cin >> a[i];
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long int> vll;
typedef vector<pii> vii;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int n;
  cin >> n;
  int dp[n];
  int 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];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define endl '\n'
#define init(n)                                                                \
  int n;                                                                       \
  cin >> n;                                                                    \
  int a[n];                                                                    \
  rep(i, n) cin >> a[i];
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long int> vll;
typedef vector<pii> vii;
int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int n;
  cin >> n;
  int dp[n];
  int 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];
  return 0;
}