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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define mod 1000000007
ll dp[100005] = {INT_MAX}, cur = 0;

int main() {
#ifndef ONLINE_JUDGE
  freopen("C:\\Users\\msmut\\Documents\\Programming\\Input.txt", "r", stdin);
  freopen("C:\\Users\\msmut\\Documents\\Programming\\Output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  ll h[n + 5];
  for (ll i = 0; i < n; i++)
    cin >> h[i];
  cur = 0;
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  if (n == 2)
    cout << dp[1];
  else {
    for (ll 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];
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define mod 1000000007
ll dp[100005] = {INT_MAX}, cur = 0;

int main() {
  ll n;
  cin >> n;
  ll h[n + 5];
  for (ll i = 0; i < n; i++)
    cin >> h[i];
  cur = 0;
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  if (n == 2)
    cout << dp[1];
  else {
    for (ll 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];
  }
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long int ll;

int main() {
  int N, dp[1000] = {};
  cin >> N;
  vector<int> h(N);
  for (int i = 0; i < N; i++)
    cin >> h[i];
  dp[1] = abs(h[1] - h[0]);
  for (int i = 0; i < N; i++) {
    if (i > 1)
      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];
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long int ll;

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

Buggy submission (status: Runtime Error):
/*
dp[i] = min(dp[i-1]+abs(h[i]-h[i-1]),dp[i-2]+abs(h[i]-h[i-2]))
*/

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#define N_MAX 10001
using namespace std;

int dp[N_MAX];
int h[N_MAX];
int n;

int res;

void solve() {
  dp[1] = 0;

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

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  solve();
  cout << res << endl;
  return 0;
}


Fixed submission:
/*
dp[i] = min(dp[i-1]+abs(h[i]-h[i-1]),dp[i-2]+abs(h[i]-h[i-2]))
*/

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#define N_MAX 100001
using namespace std;

int dp[N_MAX];
int h[N_MAX];
int n;

int res;

void solve() {
  dp[1] = 0;

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

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  solve();
  cout << res << endl;
  return 0;
}
Problem p03160 (C++)

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

#define ll long long
#define Ull unsigned long long
#define pb push_back
#define INF 2e16
#define PI acos(-1.0)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define min4(a, b, c, d) min(a, min(b, min(c, d)))
#define max4(a, b, c, d) max(a, max(b, max(c, d)))
#define SQR(a) ((a) * (a))
#define MEM(a, x) memset(a, x, sizeof(a))
#define ABS(x) ((x) < 0 ? -(x) : (x))

#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())
#define FastRead                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(nullptr);

const int N = 2;
const ll MOD = 1e9 + 7;

void Solve() {
  ll n;
  cin >> n;
  ll a[n], dp[n];
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  for (ll i = 0; i < n; i++)
    dp[i] = INF;

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

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  FastRead;
  int T = 1;
  // cin>>T;
  while (T--) {
    Solve();
  }

  return 0;
}


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

#define ll long long
#define Ull unsigned long long
#define pb push_back
#define INF 2e16
#define PI acos(-1.0)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define min4(a, b, c, d) min(a, min(b, min(c, d)))
#define max4(a, b, c, d) max(a, max(b, max(c, d)))
#define SQR(a) ((a) * (a))
#define MEM(a, x) memset(a, x, sizeof(a))
#define ABS(x) ((x) < 0 ? -(x) : (x))

#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())
#define FastRead                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(nullptr);

const int N = 2;
const ll MOD = 1e9 + 7;

void Solve() {
  ll n;
  cin >> n;
  ll a[n], dp[n];
  for (ll i = 0; i < n; i++)
    cin >> a[i];

  for (ll i = 0; i < n; i++)
    dp[i] = INF;

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

int main() {

  FastRead;
  int T = 1;
  // cin>>T;
  while (T--) {
    Solve();
  }

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
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 ll INF = 1LL << 60;

int n;
vector<ll> h(100010, 0);
vector<ll> dp(10010, INF);

int main() {
  cin >> n;
  rep(i, n) cin >> h[i];

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

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
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 ll INF = 1LL << 60;

int n;
vector<ll> h(100010, 0);
vector<ll> dp(100010, INF);

int main() {
  cin >> n;
  rep(i, n) cin >> h[i];

  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    chmin(dp[i], dp[i - 1] + abs(h[i] - h[i - 1]));
    if (i > 1)
      chmin(dp[i], 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>
#define INF 10000000
#define MAXN 11000
using namespace std;

long long h[MAXN], dp[MAXN];
int N;

int main() {
  fill(dp, dp + MAXN, INF);
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  dp[0] = 0;
  for (int i = 0; i < N - 1; i++) {
    dp[i + 1] = min(dp[i + 1], dp[i] + abs(h[i + 1] - h[i]));
    dp[i + 2] = min(dp[i + 2], dp[i] + abs(h[i + 2] - h[i]));
  }
  cout << dp[N - 1];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF 1000000000
#define MAXN 110000
using namespace std;

long long h[MAXN], dp[MAXN];
int N;

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

Buggy submission (status: Runtime Error):
/* -- Aditya0412-- */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define pop pop_back
#define mp make_pair
#define ve vector
#define vii vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpl vector<pll>
#define fi first
#define sz size
#define len length
#define se second
// const ll mod=998244353;
const ll mod = 1000000000 + 7;
const ll N = 1000000 + 6;
#define M_PI 3.14159265358979323846
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("INPUT.txt", "r", stdin);
  freopen("OUTPUT.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  int arr[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  int brr[n + 1];
  brr[0] = 0;
  brr[1] = 0;
  brr[2] = abs(arr[1] - arr[2]);
  for (int i = 3; i <= n; i++) {
    brr[i] = min(brr[i - 2] + abs(arr[i - 2] - arr[i]),
                 brr[i - 1] + abs(arr[i - 1] - arr[i]));
  }
  cout << brr[n];
}

Fixed submission:
/* -- Aditya0412-- */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define pop pop_back
#define mp make_pair
#define ve vector
#define vii vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpl vector<pll>
#define fi first
#define sz size
#define len length
#define se second
// const ll mod=998244353;
const ll mod = 1000000000 + 7;
const ll N = 1000000 + 6;
#define M_PI 3.14159265358979323846
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int arr[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  int brr[n + 1];
  brr[0] = 0;
  brr[1] = 0;
  brr[2] = abs(arr[1] - arr[2]);
  for (int i = 3; i <= n; i++) {
    brr[i] = min(brr[i - 2] + abs(arr[i - 2] - arr[i]),
                 brr[i - 1] + abs(arr[i - 1] - arr[i]));
  }
  cout << brr[n];
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <limits.h>
using namespace std;
int dp[100004];
int mincost(int n, int height[]) {
  if (n == 1 || n == 0)
    return 0;
  int cost1 = INT_MAX;
  int cost2 = INT_MAX;
  if (dp[n] != -1)
    return dp[n];
  if (n >= 2)
    cost1 = abs(height[n - 1] - height[n - 2]) + mincost(n - 1, height);
  if (n >= 3)
    cost2 = abs(height[n - 1] - height[n - 3]) + mincost(n - 2, height);
  return dp[n] = min(cost1, cost2);
}
int main() {
  int n;
  cin >> n;
  int height[1000];
  for (int i = 0; i < n; i++)
    cin >> height[i];
  for (int i = 0; i < 100004; i++)
    dp[i] = -1;
  cout << mincost(n, height);
}


Fixed submission:
#include <iostream>
#include <limits.h>
using namespace std;
int dp[100004];
int mincost(int n, int height[]) {
  if (n == 1 || n == 0)
    return 0;
  int cost1 = INT_MAX;
  int cost2 = INT_MAX;
  if (dp[n] != -1)
    return dp[n];
  if (n >= 2)
    cost1 = abs(height[n - 1] - height[n - 2]) + mincost(n - 1, height);
  if (n >= 3)
    cost2 = abs(height[n - 1] - height[n - 3]) + mincost(n - 2, height);
  return dp[n] = min(cost1, cost2);
}
int main() {
  int n;
  cin >> n;
  int height[100004];
  for (int i = 0; i < n; i++)
    cin >> height[i];
  for (int i = 0; i < 100004; i++)
    dp[i] = -1;
  cout << mincost(n, height);
}
Problem p03160 (C++)

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


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

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


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

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

#define itf ios_base::sync_with_stdio(false)
#define ct cin.tie(NULL)

int Z[100002];

int step(int a[], int n, int i) {
  if (i >= n)
    return 0;

  return min(abs(a[i - 1] - a[i]) + step(a, n, i + 1),
             abs(a[i - 1] - a[i + 1]) + step(a, n, i + 2));
}

int main() {

  memset(Z, -1, sizeof(Z));
  int n;
  cin >> n;
  int a[n];

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

  cout << step(a, n, 1) << " ";
  return 0;
}


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

#define itf ios_base::sync_with_stdio(false)
#define ct cin.tie(NULL)

int Z[100002];

int step(int a[], int n, int i) {
  if (i >= n)
    return 0;

  if (Z[i] != -1)
    return Z[i];

  return (Z[i] = min(abs(a[i - 1] - a[i]) + step(a, n, i + 1),
                     abs(a[i - 1] - a[i + 1]) + step(a, n, i + 2)));
}

int main() {

  memset(Z, -1, sizeof(Z));
  int n;
  cin >> n;
  int a[n];

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

  cout << step(a, n, 1) << " ";
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int N;
int memo[10001];
int arr[10001];
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &arr[i]);
    memo[i] = 1000000000;
  }
  memo[0] = 0;
  for (int i = 0; i < N; i++) {
    if (i + 1 < N)
      memo[i + 1] = min(memo[i + 1], memo[i] + abs(arr[i] - arr[i + 1]));
    if (i + 2 < N)
      memo[i + 2] = min(memo[i + 2], memo[i] + abs(arr[i] - arr[i + 2]));
  }
  printf("%d\n", memo[N - 1]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int N;
int memo[100001];
int arr[100001];
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &arr[i]);
    memo[i] = 1000000000;
  }
  memo[0] = 0;
  for (int i = 0; i < N; i++) {
    if (i + 1 < N)
      memo[i + 1] = min(memo[i + 1], memo[i] + abs(arr[i] - arr[i + 1]));
    if (i + 2 < N)
      memo[i + 2] = min(memo[i + 2], memo[i] + abs(arr[i] - arr[i + 2]));
  }
  printf("%d\n", memo[N - 1]);
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using Graph = vector<vector<int>>;

int N;
vector<int> h(100001);

int main(void) {
  cin >> N;

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

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

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

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using Graph = vector<vector<int>>;

int N;
vector<int> h(100001);

int main(void) {
  cin >> N;

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

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

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

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

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

using namespace std;

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


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

using namespace std;

int main() {
  int N;
  int h[1 << 17];
  int m[1 << 17];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  m[0] = 0;
  m[1] = abs(h[0] - h[1]);
  for (int i = 2; i < N; i++) {
    m[i] =
        min(m[i - 2] + abs(h[i] - h[i - 2]), m[i - 1] + abs(h[i] - h[i - 1]));
  }
  cout << m[N - 1] << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll int
const ll inf = 1e9;
const int N = 1e4 + 7;

ll dp[N][3], v[N];
ll n;
ll solve(ll cur, int p) {
  if (cur == n)
    return 0;
  if (cur > n)
    return inf;
  // cout<<cur<<" "<<p<<endl;
  if (dp[cur][p] != -1)
    return dp[cur][p];
  dp[cur][p] = solve(cur + 1, 1) + abs(v[cur + 1] - v[cur]);
  dp[cur][p] = min(solve(cur + 2, 2) + abs(v[cur + 2] - v[cur]), dp[cur][p]);
  return dp[cur][p];
}
int main() {
  cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> v[i];
  memset(dp, -1, sizeof dp);
  int ans = solve(1, 1);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll int
const ll inf = 1e9;
const int N = 1e5 + 7;

ll dp[N][3], v[N];
ll n;
ll solve(ll cur, int p) {
  if (cur == n)
    return 0;
  if (cur > n)
    return inf;
  // cout<<cur<<" "<<p<<endl;
  if (dp[cur][p] != -1)
    return dp[cur][p];
  dp[cur][p] = solve(cur + 1, 1) + abs(v[cur + 1] - v[cur]);
  dp[cur][p] = min(solve(cur + 2, 2) + abs(v[cur + 2] - v[cur]), dp[cur][p]);
  return dp[cur][p];
}
int main() {
  cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> v[i];
  memset(dp, -1, sizeof dp);
  int ans = solve(1, 1);
  cout << ans << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repi(i, 0, n)
#define SZ(x) ((int)(x).size())
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
// using P = pair<int, int>;
struct Vec {
  double x, y;
};
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;
}
using namespace std;

int dp[10001];
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  // cout << fixed << setprecision(15);

  int N;
  cin >> N;
  vector<int> h(N);
  rep(i, N) cin >> h[i];
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  repi(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] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repi(i, 0, n)
#define SZ(x) ((int)(x).size())
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
// using P = pair<int, int>;
struct Vec {
  double x, y;
};
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;
}
using namespace std;

int dp[100001];
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  // cout << fixed << setprecision(15);

  int N;
  cin >> N;
  vector<int> h(N);
  rep(i, N) cin >> h[i];
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  repi(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] << endl;
  return 0;
}
Problem p03160 (C++)

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

#define str(a) to_string(a)
#define int long long
#define pb push_back
#define mp make_pair
#define SORT(c) sort(c.begin(), c.end())
#define max_heap priority_queue<int>
#define min_heap priority_queue<int, vector<int>, greater<int>>
#define mod 1000000007
#define setbits(x) __builtin_popcount(x)
#define ps(x, y) fixed << setprecision(y) << x
#define start(t)                                                               \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define inf LLONG_MAX
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define endl "\n"

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


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

#define str(a) to_string(a)
#define int long long
#define pb push_back
#define mp make_pair
#define SORT(c) sort(c.begin(), c.end())
#define max_heap priority_queue<int>
#define min_heap priority_queue<int, vector<int>, greater<int>>
#define mod 1000000007
#define setbits(x) __builtin_popcount(x)
#define ps(x, y) fixed << setprecision(y) << x
#define start(t)                                                               \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define inf LLONG_MAX
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define endl "\n"

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
#define ac(x, i) get<i>(x)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define MOD 1000000007

const int infi = 1e9;
const ll infl = 1e18;

const int N = 1e5 + 100;
ll dp[N][2];

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 2; i <= n; i++) {
    dp[i][0] = infl;
    dp[i][1] = infl;
  }
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dp[1][0] = dp[1][1] = 0LL;
  for (int i = 1; i < n; i++) {
    dp[i + 1][0] = min(dp[i][0], dp[i][1]) + abs(a[i + 1] - a[i]);
    dp[i + 2][1] = min(dp[i][0], dp[i][1]) + abs(a[i + 2] - a[i]);
  }
  cout << min(dp[n][0], dp[n][1]) << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
#define ac(x, i) get<i>(x)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define MOD 1000000007

const int infi = 1e9;
const ll infl = 1e18;

const int N = 1e5 + 100;
ll dp[N][2];

int main() {
#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 2; i <= n; i++) {
    dp[i][0] = infl;
    dp[i][1] = infl;
  }
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dp[1][0] = dp[1][1] = 0LL;
  for (int i = 1; i < n; i++) {
    dp[i + 1][0] = min(dp[i][0], dp[i][1]) + abs(a[i + 1] - a[i]);
    dp[i + 2][1] = min(dp[i][0], dp[i][1]) + abs(a[i + 2] - a[i]);
  }
  cout << min(dp[n][0], dp[n][1]) << "\n";
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
using namespace std;

int main() {
  int i, n;
  cin >> n;
  int h[10000], dp[10001];
  for (i = 0; i < n; i++) {
    cin >> h[i];
  }
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  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] << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <math.h>
using namespace std;

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e9 + 5;
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vector<int> h(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, i + 2}) {
      if (j < n) {
        dp[j] = min(dp[j], dp[i] + abs(h[j] - h[i]));
      }
    }
  }
  cout << dp[n - 1];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e9 + 5;
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  /*#ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif*/
  int n;
  cin >> n;
  vector<int> h(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, i + 2}) {
      if (j < n) {
        dp[j] = min(dp[j], dp[i] + abs(h[j] - h[i]));
      }
    }
  }
  cout << dp[n - 1];
}
Problem p03160 (C++)

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

using namespace std;

const int N = 1e5;
int n, stones[N];
int memo[N];

int jumps(int ind) {
  if (ind == (n - 1))
    return 0;
  if (ind >= n)
    return INT_MAX;

  if (memo[ind] != -1)
    return memo[ind];

  int oneJump = abs(stones[ind] - stones[ind + 1]) + jumps(ind + 1);
  int twoJumps = INT_MAX;

  if (ind + 2 < n) {
    twoJumps = abs(stones[ind] - stones[ind + 2]) + jumps(ind + 2);
  }

  return memo[n] = min(oneJump, twoJumps);
}

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

  memset(memo, -1, sizeof(memo));
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> stones[i];
  }

  // We will start from first stone
  cout << jumps(0) << endl;

  return 0;
}

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

using namespace std;

const int N = 1e5;
int n, stones[N];
int memo[N];

int jumps(int ind) {
  if (ind == (n - 1))
    return 0;
  if (ind >= n)
    return INT_MAX;

  if (memo[ind] != -1)
    return memo[ind];

  int oneJump = abs(stones[ind] - stones[ind + 1]) + jumps(ind + 1);
  int twoJumps = INT_MAX;

  if (ind + 2 < n) {
    twoJumps = abs(stones[ind] - stones[ind + 2]) + jumps(ind + 2);
  }

  return memo[ind] = min(oneJump, twoJumps);
}

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

  memset(memo, -1, sizeof(memo));
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> stones[i];
  }

  // We will start from first stone
  cout << jumps(0) << endl;

  return 0;
}
Problem p03160 (C++)

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

#define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mod 1000000007
#define qmod 998244353
#define M 1000001
#define endl "\n"
#define all(a) (a).begin(), (a).end()
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define repr(i, a, b) for (int i = a; i >= b; --i)
#define F first
#define S second
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;

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

int main() {

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

  fast;
  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    rep(i, 0, n) cin >> a[i];
    int dp[n];
    memset(dp, -1, sizeof(dp));
    cout << solve(a, dp, n, 0);
  }
  return 0;
}

// First, solve the problem. Then, write the code.


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

#define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mod 1000000007
#define qmod 998244353
#define M 1000001
#define endl "\n"
#define all(a) (a).begin(), (a).end()
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define repr(i, a, b) for (int i = a; i >= b; --i)
#define F first
#define S second
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;

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

int main() {

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

  fast;
  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    rep(i, 0, n) cin >> a[i];
    int dp[n];
    memset(dp, -1, sizeof(dp));
    cout << solve(a, dp, n, 0);
  }
  return 0;
}

// First, solve the problem. Then, write the code.
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
int minCost(ll *h, int n) {
  ll dp[n];
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  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]));
  return dp[n - 1];
}
int main() {
  c_p_c();
  int n;
  cin >> n;
  ll h[n];
  for (int i = 0; i < n; i++)
    cin >> h[i];
  cout << minCost(h, n) << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int minCost(ll *h, int n) {
  ll dp[n];
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  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]));
  return dp[n - 1];
}
int main() {
  c_p_c();
  int n;
  cin >> n;
  ll h[n];
  for (int i = 0; i < n; i++)
    cin >> h[i];
  cout << minCost(h, n) << "\n";
}
Problem p03160 (C++)

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

int n;
int num[101];
int dp[101];

int main() {
  memset(dp, 63, sizeof dp);
  cin >> n;
  for (int qq = 1; qq <= n; qq++)
    cin >> num[qq];
  dp[1] = 0;
  dp[2] = abs(num[1] - num[2]);
  for (int qq = 3; qq <= n; qq++) {
    dp[qq] = min(abs(num[qq - 2] - num[qq]) + dp[qq - 2],
                 abs(num[qq - 1] - num[qq]) + dp[qq - 1]);
  }
  cout << dp[n];
  return 0;
}


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

int n;
int num[100001];
int dp[100001];

int main() {
  memset(dp, 63, sizeof dp);
  cin >> n;
  for (int qq = 1; qq <= n; qq++)
    cin >> num[qq];
  dp[1] = 0;
  dp[2] = abs(num[1] - num[2]);
  for (int qq = 3; qq <= n; qq++) {
    dp[qq] = min(abs(num[qq - 2] - num[qq]) + dp[qq - 2],
                 abs(num[qq - 1] - num[qq]) + dp[qq - 1]);
  }
  cout << dp[n];
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// g++ -std=gnu++14 a.cpp
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
ll MOD = 1e9 + 7;
int INF = 1 << 30;
ll INFL = 1LL << 60;

// int N;
vector<ll> h(10010);
vector<ll> dp(10010, INFL);

ll rec(ll i) {
  if (dp[i] < INFL)
    return dp[i];

  if (i == 0)
    return 0;

  ll res = INF;
  res = min(res, rec(i - 1) + abs(h[i] - h[i - 1]));
  if (i > 1)
    res = min(res, rec(i - 2) + abs(h[i] - h[i - 2]));
  return dp[i] = res;
}

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

  cout << rec(N - 1) << endl;
}


Fixed submission:
// g++ -std=gnu++14 a.cpp
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
ll MOD = 1e9 + 7;
int INF = 1 << 30;
ll INFL = 1LL << 60;

// int N;
vector<ll> h(100010);
vector<ll> dp(100010, INFL);

ll rec(ll i) {
  if (dp[i] < INFL)
    return dp[i];

  if (i == 0)
    return 0;

  ll res = INF;
  res = min(res, rec(i - 1) + abs(h[i] - h[i - 1]));
  if (i > 1)
    res = min(res, rec(i - 2) + abs(h[i] - h[i - 2]));
  return dp[i] = res;
}

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

  cout << rec(N - 1) << endl;
}
Problem p03160 (C++)

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

using namespace std;

int main() {

  // freopen("t.txt","r",stdin);
  int n;
  cin >> n;
  int a[MAXN];
  long long dp[MAXN];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  dp[1] = 0;
  dp[2] = abs(a[2] - a[1]);
  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];
}

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

using namespace std;

int main() {

  // freopen("t.txt","r",stdin);
  int n;
  cin >> n;
  int a[MAXN];
  long long dp[MAXN];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  dp[1] = 0;
  dp[2] = abs(a[2] - a[1]);
  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];
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

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

  return 0;
}
Problem p03160 (C++)

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

int dp[10001];
int h[10001];
int func(int a) {
  if (dp[a] != -1) {
    return dp[a];
  }
  if (func(a - 1) + abs(h[a] - h[a - 1]) < func(a - 2) + abs(h[a] - h[a - 2])) {
    return dp[a] = func(a - 1) + abs(h[a] - h[a - 1]);
  } else {
    return dp[a] = func(a - 2) + abs(h[a] - h[a - 2]);
  }
}
int main() {
  int n;
  std::cin >> n;
  for (int i = 0; i <= n; i++) {
    dp[i] = -1;
  };
  for (int i = 1; i <= n; i++) {
    std::cin >> h[i];
  }
  dp[1] = 0;
  dp[2] = abs(h[2] - h[1]);
  std::cout << func(n) << std::endl;
}


Fixed submission:
#include <iostream>

int dp[100001];
int h[100001];
int func(int a) {
  if (dp[a] != -1) {
    return dp[a];
  }
  if (func(a - 1) + abs(h[a] - h[a - 1]) < func(a - 2) + abs(h[a] - h[a - 2])) {
    return dp[a] = func(a - 1) + abs(h[a] - h[a - 1]);
  } else {
    return dp[a] = func(a - 2) + abs(h[a] - h[a - 2]);
  }
}
int main() {
  int n;
  std::cin >> n;
  for (int i = 0; i <= n; i++) {
    dp[i] = -1;
  };
  for (int i = 1; i <= n; i++) {
    std::cin >> h[i];
  }
  dp[1] = 0;
  dp[2] = abs(h[2] - h[1]);
  std::cout << func(n) << std::endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define vi vector<int>
#define vii vector<pii>
#define pii pair<int, int>
#define mii map<int, int>
#define pb push_back
#define rev(v) reverse(v.begin(), v.end())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define endl '\n'
#define F first
#define S second
#define PI 3.14159265359
#define MAXN 100007

const int mod = 1e9 + 7;

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

int solve(int idx) {
  if (idx >= n)
    return INT_MAX;
  if (idx == n - 1)
    return 0;
  if (dp[idx] != -1)
    return dp[idx];

  dp[idx] = min(abs(arr[idx] - arr[idx + 1]) + solve(idx + 1),
                abs(arr[idx] - arr[idx + 2]) + solve(idx + 2));

  return dp[idx];
}

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

  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>
using namespace std;
#define int long long int
#define vi vector<int>
#define vii vector<pii>
#define pii pair<int, int>
#define mii map<int, int>
#define pb push_back
#define rev(v) reverse(v.begin(), v.end())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define endl '\n'
#define F first
#define S second
#define PI 3.14159265359
#define MAXN 100007

const int mod = 1e9 + 7;

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

int solve(int idx) {
  if (idx >= n)
    return INT_MAX;
  if (idx == n - 1)
    return 0;
  if (dp[idx] != -1)
    return dp[idx];

  dp[idx] = min(abs(arr[idx] - arr[idx + 1]) + solve(idx + 1),
                abs(arr[idx] - arr[idx + 2]) + solve(idx + 2));

  return dp[idx];
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  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;

const int N = 10010;
int ls[N], c[N];

int calc(int n) {
  c[2] = abs(ls[1] - ls[2]);
  c[3] = min(abs(ls[1] - ls[3]), c[2] + abs(ls[2] - ls[3]));
  for (int i = 4; i <= n; i++)
    c[i] = min(c[i - 1] + abs(ls[i] - ls[i - 1]),
               c[i - 2] + abs(ls[i - 2] - ls[i]));
  // for (int i=2;i<=n;i++)cout<<c[i]<<" ";
  return c[n];
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n, t;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> ls[i];
  cout << calc(n) << "\n";

  return 0;
}

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

const int N = 100010;
int ls[N], c[N];

int calc(int n) {
  c[2] = abs(ls[1] - ls[2]);
  c[3] = min(abs(ls[1] - ls[3]), c[2] + abs(ls[2] - ls[3]));
  for (int i = 4; i <= n; i++)
    c[i] = min(c[i - 1] + abs(ls[i] - ls[i - 1]),
               c[i - 2] + abs(ls[i - 2] - ls[i]));
  // for (int i=2;i<=n;i++)cout<<c[i]<<" ";
  return c[n];
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n, t;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> ls[i];
  cout << calc(n) << "\n";

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
long long int minimum(long long int a[], long long int n, long long int b[]) {
  if (n == 2) {
    b[2] = abs(a[1] - a[0]);
    return abs(a[1] - a[0]);
  } else if (n == 3) {
    long long int min = abs(a[2] - a[0]);
    if (abs(a[1] - a[0]) + abs(a[2] - a[1]) < min) {
      min = abs(a[1] - a[0]) + abs(a[2] - a[1]);
    }
    b[3] = min;
    return min;

  } else {
    if (b[n] == -1) {

      long long int min = minimum(a, n - 1, b) + abs(a[n - 1] - a[n - 2]);
      long long min2 = minimum(a, n - 2, b) + abs(a[n - 1] - a[n - 3]);
      if (min2 < min) {
        min = min2;
      }
      return min;
    } else {
      return b[n];
    }
  }
}
int main() {
  long long int n;
  cin >> n;
  long long int a[n], b[n + 1];
  for (long long int i = 0; i < n; i++) {
    b[i] = -1;
    cin >> a[i];
  }
  b[n] = -1;
  long long int result = minimum(a, n, b);
  cout << result;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
long long int minimum(long long int a[], long long int n, long long int b[]) {
  if (n == 2) {
    b[2] = abs(a[1] - a[0]);
    return abs(a[1] - a[0]);
  } else if (n == 3) {
    long long int min = abs(a[2] - a[0]);
    if (abs(a[1] - a[0]) + abs(a[2] - a[1]) < min) {
      min = abs(a[1] - a[0]) + abs(a[2] - a[1]);
    }
    b[3] = min;
    return min;

  } else {
    if (b[n] == -1) {

      long long int min = minimum(a, n - 1, b) + abs(a[n - 1] - a[n - 2]);
      long long min2 = minimum(a, n - 2, b) + abs(a[n - 1] - a[n - 3]);
      if (min2 < min) {
        min = min2;
      }
      b[n] = min;
      return min;
    } else {
      return b[n];
    }
  }
}
int main() {
  long long int n;
  cin >> n;
  long long int a[n], b[n + 1];
  for (long long int i = 0; i < n; i++) {
    b[i] = -1;
    cin >> a[i];
  }
  b[n] = -1;
  long long int result = minimum(a, n, b);
  cout << result;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main(void) {
  int n;
  cin >> n;
  int h[n];
  rep(i, n) cin >> h[i];
  int dp[10000];
  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] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main(void) {
  int n;
  cin >> n;
  int h[n];
  rep(i, n) cin >> h[i];
  int dp[1000000];
  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] << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAX_N = 10000;
int h[MAX_N], dp[MAX_N + 1];
int n;
void solve() {
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  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]));
  }
  printf("%d", dp[n - 1]);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &h[i]);
  }
  solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAX_N = 100000;
int h[MAX_N], dp[MAX_N + 1];
int n;
void solve() {
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  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]));
  }
  printf("%d", dp[n - 1]);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &h[i]);
  }
  solve();
  return 0;
}