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 PI 3.14159265359
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define pb(a) push_back(a)

//////////// Solution /////////////////////////////////

const int MAXN = 1e5 + 1;

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

  cout << dp[n] << '\n';
}

/////////// End Solution ///////////////////////////////////////

//////// Initialization ////////////////////////////////////

int main() {

#ifndef ONLINE_JUDGE
  FILE *FIN = freopen("/home/danil/prog/input.txt", "r", stdin);
  clock_t time_start = clock();
#endif

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

  solve();

#ifndef ONLINE_JUDGE
  fclose(FIN);
  cerr << "\x1b[031m\n-----------------\nTime="
       << (ll)((double)(clock() - time_start) / CLOCKS_PER_SEC * 1000)
       << "ms\n\x1b[0m";
#endif

  return 0;
}

//////// End Initialization ///////////////////////////////////////

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

#define ONLINE_JUDGE
#define ll long long
#define PI 3.14159265359
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define pb(a) push_back(a)

//////////// Solution /////////////////////////////////

const int MAXN = 1e5 + 1;

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

  cout << dp[n] << '\n';
}

/////////// End Solution ///////////////////////////////////////

//////// Initialization ////////////////////////////////////

int main() {

#ifndef ONLINE_JUDGE
  FILE *FIN = freopen("/home/danil/prog/input.txt", "r", stdin);
  clock_t time_start = clock();
#endif

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

  solve();

#ifndef ONLINE_JUDGE
  fclose(FIN);
  cerr << "\x1b[031m\n-----------------\nTime="
       << (ll)((double)(clock() - time_start) / CLOCKS_PER_SEC * 1000)
       << "ms\n\x1b[0m";
#endif

  return 0;
}

//////// End Initialization ///////////////////////////////////////
Problem p03160 (C++)

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

int dp[100010];

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

  for (int i = 0; i < 2 * n; i++) {
    dp[i] = 1 << 30;
    //        cerr << dp[i] << endl;
  }
  dp[0] = 0;
  for (int i = 0; i < n - 1; i++) {
    //        cerr << dp[i+1] << " " << dp[i]+abs(h[i]-h[i+1]) << endl;
    dp[i + 1] = min(dp[i + 1], dp[i] + abs(h[i] - h[i + 1]));
    dp[i + 2] = min(dp[i + 2], dp[i] + abs(h[i] - h[i + 2]));
  }

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


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int dp[100010];

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

  for (int i = 0; i < 100010; i++) {
    dp[i] = 1 << 30;
    //        cerr << dp[i] << endl;
  }
  dp[0] = 0;
  for (int i = 0; i < n - 1; i++) {
    //        cerr << dp[i+1] << " " << dp[i]+abs(h[i]-h[i+1]) << endl;
    dp[i + 1] = min(dp[i + 1], dp[i] + abs(h[i] - h[i + 1]));
    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 pb push_back
#define vii vector<int, int>
#define pii pair<int, int>
#define nn "\n"

int main() {

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

  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;

  std::vector<ll> v(n + 1);

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

  vector<int> dp(n + 1);
  dp[1] = 0;

  for (int i = 2; i <= n; i++) {
    int op1 = abs(v[i] - v[i - 1]) + dp[i - 1];
    int op2 = (i == 2) ? INT_MAX : abs(v[i] - v[i - 2]) + dp[i - 2];
    dp[i] = min(op1, op2);
  }

  cout << dp[n];

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define vii vector<int, int>
#define pii pair<int, int>
#define nn "\n"

int main() {

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

  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;

  std::vector<ll> v(n + 1);

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

  vector<int> dp(n + 1);
  dp[1] = 0;

  for (int i = 2; i <= n; i++) {
    int op1 = abs(v[i] - v[i - 1]) + dp[i - 1];
    int op2 = (i == 2) ? INT_MAX : abs(v[i] - v[i - 2]) + dp[i - 2];
    dp[i] = min(op1, op2);
  }

  cout << dp[n];

  return 0;
}
Problem p03160 (C++)

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

int h[10100];
int f[10100];

int main() {
  int N;
  scanf("%d%d", &N, h + 1);

  for (int i = 2; i <= N; i++) {
    scanf("%d", h + i);
    f[i] = f[i - 1] + abs(h[i] - h[i - 1]);
    if (i - 2)
      f[i] = min(f[i], f[i - 2] + abs(h[i] - h[i - 2]));
  }

  return printf("%d\n", f[N]) & 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;

int h[100100];
int f[100100];

int main() {
  int N;
  scanf("%d%d", &N, h + 1);

  for (int i = 2; i <= N; i++) {
    scanf("%d", h + i);
    f[i] = f[i - 1] + abs(h[i] - h[i - 1]);
    if (i - 2)
      f[i] = min(f[i], f[i - 2] + abs(h[i] - h[i - 2]));
  }

  return printf("%d\n", f[N]) & 0;
}
Problem p03160 (C++)

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

using namespace std;

int h[10005], dp[10005];

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


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

using namespace std;

int h[100005], dp[100005];

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

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
using namespace std;

const int INF = 1001001001;
const int MOD = 1000000007;

int main(void) {
  int n;
  cin >> n;
  vector<int> h(n); // コスト配列
  rep(i, n) cin >> h[i];

  queue<int> q;
  q.push(0);
  vector<int> c(n, INF);
  c[0] = 0; // DP配列

  while (!q.empty()) {
    int cur_v = q.front();
    q.pop();
    auto change = [&](int i) {
      c[cur_v + i] = min(c[cur_v + i], c[cur_v] + abs(h[cur_v + i] - h[cur_v]));
    };

    if (cur_v + 1 < n) {
      change(1);
      q.push(cur_v + 1);
    }
    if (cur_v + 2 < n) {
      change(2);
      q.push(cur_v + 2);
    }
  }
  cout << c[n - 1] << endl;

  return 0;
}

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

#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
using namespace std;

const int INF = 1001001001;
const int MOD = 1000000007;

int main(void) {
  int n;
  cin >> n;
  vector<int> h(n); // コスト配列
  rep(i, n) cin >> h[i];

  queue<int> q;
  q.push(0);
  vector<int> c(n, INF);
  c[0] = 0; // DP配列

  rep(i, n) {
    int cur_v = i;
    auto change = [&](int i) {
      c[cur_v + i] = min(c[cur_v + i], c[cur_v] + abs(h[cur_v + i] - h[cur_v]));
    };

    if (cur_v + 1 < n) {
      change(1);
      q.push(cur_v + 1);
    }
    if (cur_v + 2 < n) {
      change(2);
      q.push(cur_v + 2);
    }
  }
  cout << c[n - 1] << endl;

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <iostream>
#define SIZE 10001
#define INF INT_MAX >> 1

using namespace std;

int dp[SIZE];

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

  fill(dp, dp + n, INF);
  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]));
  }

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

Fixed submission:
#include <algorithm>
#include <climits>
#include <iostream>
#define SIZE 100001
#define INF INT_MAX >> 1

using namespace std;

int dp[SIZE];

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

  fill(dp, dp + n, INF);
  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]));
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define inf (ll)1e9
#define d(x) cerr << #x << "=" << x << endl;

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

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define inf (ll)1e9
#define d(x) cerr << #x << "=" << x << endl;

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pb push_back
#define mp make_pair
#define pii pair<ll, ll>
#define vi vector<ll>
#define vs vector<string>
#define vii vector<pii>
#define mi map<ll, ll>
#define mii map<pii, ll>
#define mc map<char, ll>
#define itr ::iterator
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fe(i, a, b) for (ll i = a; i <= b; i++)
#define fn(i, a, b) for (ll i = a; i > b; i--)
#define fne(i, a, b) for (ll i = a; i >= b; i--)
#define fm(it, m) for (it = m.begin(); it != m.end(); ++it)
#define all(v) v.begin(), v.end()
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define M 1000000007
#define pi 3.14159265358979323846

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

  int T = 1;
  cin >> T;
  f(t, 0, T) {
    ll n = 0;
    //,i=0,j=0,k=0,flag=0;
    cin >> n;

    vi a(n), dp(n);

    f(i, 0, n) cin >> a[i];

    dp[0] = 0, dp[1] = abs(a[1] - a[0]);

    f(i, 2, n) {
      dp[i] = min((dp[i - 2] + abs(a[i] - a[i - 2])),
                  (dp[i - 1] + abs(a[i] - a[i - 1])));
    }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pb push_back
#define mp make_pair
#define pii pair<ll, ll>
#define vi vector<ll>
#define vs vector<string>
#define vii vector<pii>
#define mi map<ll, ll>
#define mii map<pii, ll>
#define mc map<char, ll>
#define itr ::iterator
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fe(i, a, b) for (ll i = a; i <= b; i++)
#define fn(i, a, b) for (ll i = a; i > b; i--)
#define fne(i, a, b) for (ll i = a; i >= b; i--)
#define fm(it, m) for (it = m.begin(); it != m.end(); ++it)
#define all(v) v.begin(), v.end()
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define M 1000000007
#define pi 3.14159265358979323846

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

  int T = 1;
  // cin>>T;
  f(t, 0, T) {
    ll n = 0;
    //,i=0,j=0,k=0,flag=0;
    cin >> n;

    vi a(n), dp(n);

    f(i, 0, n) cin >> a[i];

    dp[0] = 0, dp[1] = abs(a[1] - a[0]);

    f(i, 2, n) {
      dp[i] = min((dp[i - 2] + abs(a[i] - a[i - 2])),
                  (dp[i - 1] + abs(a[i] - a[i - 1])));
    }

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

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

using namespace std;

#define pb push_back
#define mp make_pair
#define MOD ((int)(1e9) + 7)
#define fast                                                                   \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false)
#define filename "lynext"
#define freop                                                                  \
  freopen(filename ".cik", "w", stdout);                                       \
  freopen(filename ".gir", "r", stdin)
const long long int N = ((long long int)1e5) + 5;
const long long int M = ((long long int)1e3) + 5;
const long long int llinf = (long long int)1e18;
const int inf = INT_MAX;
typedef long long int lli;

lli dp[N];
lli n;
lli h[N];

lli f(lli i) {
  if (dp[i] != -1)
    return dp[i];
  if (i == n)
    return 0;
  if (i == n - 1)
    return abs(h[n] - h[i]);
  lli t1 = f(i + 1) + abs(h[i] - h[i + 1]);
  lli t2 = f(i + 2) + abs(h[i] - h[i + 2]);
  return min(t1, t2);
}

int main() {
  fast;
  // freop;
  cin >> n;
  memset(dp, -1, sizeof dp);
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  cout << f(1) << "\n";
}


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

using namespace std;

#define pb push_back
#define mp make_pair
#define MOD ((int)(1e9) + 7)
#define fast                                                                   \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false)
#define filename "lynext"
#define freop                                                                  \
  freopen(filename ".cik", "w", stdout);                                       \
  freopen(filename ".gir", "r", stdin)
const long long int N = ((long long int)1e5) + 5;
const long long int M = ((long long int)1e3) + 5;
const long long int llinf = (long long int)1e18;
const int inf = INT_MAX;
typedef long long int lli;

lli dp[N];
lli n;
lli h[N];

lli f(lli i) {
  if (dp[i] != -1)
    return dp[i];
  if (i == n)
    return 0;
  if (i == n - 1)
    return abs(h[n] - h[i]);
  lli t1 = f(i + 1) + abs(h[i] - h[i + 1]);
  lli t2 = f(i + 2) + abs(h[i] - h[i + 2]);
  return dp[i] = min(t1, t2);
}

int main() {
  fast;
  // freop;
  cin >> n;
  memset(dp, -1, sizeof dp);
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  cout << f(1) << "\n";
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#define ll long long int
#define lld long double
#define F first
#define S second
#define f(i, a, b) for (int i = a; i <= b; i++)
#define g(i, a, b) for (int i = a; i >= b; i--)
#define mp make_pair
#define pb push_back
#define mh make_heap
#define ph push_heap
#define pq priority_queue
#define bits(x) __builtin_popcountll(x)
#define op(x) cout << "Case #" << x << ": "
using namespace std;
ll mod = 1000000007;
ll INF = 1000000000000000000;
ll n;
ll a[100001];

void solve(int t) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll b[n];
  b[0] = 0;
  b[1] = abs(a[1] - a[0]);
  for (int i = 2; i < n; i++) {
    b[i] =
        min(abs(a[i - 1] - a[i]) + b[i - 1], abs(a[i - 2] - a[i]) + b[i - 2]);
  }
  cout << b[n - 1] << endl;
}

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

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

  int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    solve(i);
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#define ll long long int
#define lld long double
#define F first
#define S second
#define f(i, a, b) for (int i = a; i <= b; i++)
#define g(i, a, b) for (int i = a; i >= b; i--)
#define mp make_pair
#define pb push_back
#define mh make_heap
#define ph push_heap
#define pq priority_queue
#define bits(x) __builtin_popcountll(x)
#define op(x) cout << "Case #" << x << ": "
using namespace std;
ll mod = 1000000007;
ll INF = 1000000000000000000;
ll n;
ll a[100001];

void solve(int t) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll b[n];
  b[0] = 0;
  b[1] = abs(a[1] - a[0]);
  for (int i = 2; i < n; i++) {
    b[i] =
        min(abs(a[i - 1] - a[i]) + b[i - 1], abs(a[i - 2] - a[i]) + b[i - 2]);
  }
  cout << b[n - 1] << endl;
}

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

  for (int i = 1; i <= t; i++) {
    solve(i);
  }
}
Problem p03160 (C++)

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

using namespace std;
int a[10010];
int b[10010];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  b[0] = 0;
  b[1] = abs(a[0] - a[1]);
  for (int i = 2; i < n; i++) {
    int l = abs(a[i] - a[i - 1]) + b[i - 1];
    int r = abs(a[i] - a[i - 2]) + b[i - 2];
    b[i] = min(l, r);
  }
  cout << b[n - 1];

  return 0;
}


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

using namespace std;
int a[100010];
int b[100010];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  b[0] = 0;
  b[1] = abs(a[0] - a[1]);
  for (int i = 2; i < n; i++) {
    int l = abs(a[i] - a[i - 1]) + b[i - 1];
    int r = abs(a[i] - a[i - 2]) + b[i - 2];
    b[i] = min(l, r);
  }
  cout << b[n - 1];

  return 0;
}
Problem p03160 (Python)

Buggy submission (status: Runtime Error):
import functools
import threading
import sys


class SearchProblem(object):
    def __init__(self, N, H):
        self.N = N
        self.H = H

    def isEnd(self, i):
        return i == self.N

    def succ(self, i):
        res = []
        if i + 1 <= self.N:
            res.append((i + 1, abs(self.H[i + 1] - self.H[i])))
        if i + 2 <= self.N:
            res.append((i + 2, abs(self.H[i + 2] - self.H[i])))
        return res


def solve(problem: SearchProblem):
    @functools.lru_cache(maxsize=None)
    def future_cost(s):
        if problem.isEnd(s):
            return 0
        else:
            return min(
                cost + future_cost(next_state) for next_state, cost in problem.succ(s)
            )

    return future_cost(1)


def main():
    N = int(input())
    H = [None] + [int(x) for x in input().split(" ")]
    problem = SearchProblem(N, H)
    print(solve(problem))


if __name__ == "__main__":
    sys.setrecursionlimit(1024 * 1024)
    threading.stack_size(128 * 1024 * 1024)
    threading.Thread(target=main).start()


Fixed submission:
import functools
import threading
import sys


class SearchProblem(object):
    def __init__(self, N, H):
        self.N = N
        self.H = H

    def isEnd(self, i):
        return i == self.N

    def succ(self, i):
        res = []
        if i + 1 <= self.N:
            res.append((i + 1, abs(self.H[i + 1] - self.H[i])))
        if i + 2 <= self.N:
            res.append((i + 2, abs(self.H[i + 2] - self.H[i])))
        return res


def solve(problem: SearchProblem):
    @functools.lru_cache(maxsize=None)
    def future_cost(s):
        if problem.isEnd(s):
            return 0
        else:
            return min(
                cost + future_cost(next_state) for next_state, cost in problem.succ(s)
            )

    return future_cost(1)


def main():
    N = int(input())
    H = [None] + [int(x) for x in input().split(" ")]
    problem = SearchProblem(N, H)
    print(solve(problem))


if __name__ == "__main__":
    sys.setrecursionlimit(1024 * 1024 * 2)
    threading.stack_size(128 * 1024 * 1024 * 2)
    threading.Thread(target=main).start()
Problem p03160 (Python)

Buggy submission (status: Runtime Error):
import sys

sys.setrecursionlimit(10**4)

n = int(input())
h = [int(x) for x in input().split()]
dp = [-1 for _ in range(n)]


def rec_dp(i: int) -> int:
    global h, dp
    if i == 0:
        return 0
    if dp[i] != -1:
        return dp[i]
    res = rec_dp(i - 1) + abs(h[i] - h[i - 1])
    if i - 2 >= 0:
        res = min(rec_dp(i - 2) + abs(h[i] - h[i - 2]), res)
    dp[i] = res
    return res


print(rec_dp(n - 1))


Fixed submission:
import sys

sys.setrecursionlimit(10**9)

n = int(input())
h = [int(x) for x in input().split()]
dp = [-1 for _ in range(n)]


def rec_dp(i: int) -> int:
    global h, dp
    if i == 0:
        return 0
    if dp[i] != -1:
        return dp[i]
    res = rec_dp(i - 1) + abs(h[i] - h[i - 1])
    if i - 2 >= 0:
        res = min(rec_dp(i - 2) + abs(h[i] - h[i - 2]), res)
    dp[i] = res
    return res


print(rec_dp(n - 1))
Problem p03160 (Python)

Buggy submission (status: Runtime Error):
"""
author : halo2halo
date : 4, Feb, 2020
"""

import sys

# import itertools
# import math

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, *H = map(int, readline().split())
dp = [0] * N
dp[1] = abs(H[1] - H[0])

for i in range(2, N):
    dp[i] = min(dp[i - k] + abs(H[i] - H[i - k]) for k in [1, 2])

ans = dp[-1]
print(ans)


Fixed submission:
"""
author : halo2halo
date : 4, Feb, 2020
"""

import sys

# import itertools
# import math

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, *H = map(int, read().split())
dp = [0] * N
dp[1] = abs(H[1] - H[0])

for i in range(2, N):
    dp[i] = min(dp[i - k] + abs(H[i] - H[i - k]) for k in [1, 2])

ans = dp[-1]
print(ans)
Problem p03160 (Python)

Buggy submission (status: Runtime Error):
# -*- coding: utf-8 -*-
# Input#
n = int(input())
# n, k = map(int, input().split())
h = list(map(int, input().split()))

h = [-1] + h

ans = 0

memo = [-1] * (n + 1)
memo[1] = 0
memo[2] = abs(h[2] - h[1])


def dp(i):
    if memo[i] != -1:
        return memo[i]
    else:
        hop1 = dp(i - 1) + abs(h[i] - h[i - 1])
        hop2 = dp(i - 2) + abs(h[i] - h[i - 2])
        memo[i] = min(hop1, hop2)
        return memo[i]


for i in range(n):
    tmp = dp(i)

ans = dp(n)

print("{}".format(ans))


Fixed submission:
# -*- coding: utf-8 -*-
# Input#
n = int(input())
# n, k = map(int, input().split())
h = list(map(int, input().split()))

h = [-1] + h

ans = 0

memo = [-1] * (n + 1)
memo[1] = 0
memo[2] = abs(h[2] - h[1])


def dp(i):
    if memo[i] != -1:
        return memo[i]
    else:
        hop1 = dp(i - 1) + abs(h[i] - h[i - 1])
        hop2 = dp(i - 2) + abs(h[i] - h[i - 2])
        memo[i] = min(hop1, hop2)
        return memo[i]


for i in range(n):
    if i == 0 or i == 1 or i == 2:
        continue
    hop1 = memo[i - 1] + abs(h[i] - h[i - 1])
    hop2 = memo[i - 2] + abs(h[i] - h[i - 2])
    memo[i] = min(hop1, hop2)

ans = dp(n)

print("{}".format(ans))
Problem p03160 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
h = [int(i) for i in input().split()]

memo = [float("inf")] * N
memo[0], memo[1] = 0, abs(h[1] - h[0])


def frog(ith):
    if memo[ith] != float("inf"):
        return memo[ith]
    memo[ith] = min(
        frog(ith - 1) + abs(h[ith] - h[ith - 1]),
        frog(ith - 2) + abs(h[ith] - h[ith - 2]),
    )
    return memo[ith]


print(frog(N - 1))


Fixed submission:
import sys

sys.setrecursionlimit(500000)
N = int(input())
h = [int(i) for i in input().split()]

memo = [float("inf")] * N
memo[0], memo[1] = 0, abs(h[1] - h[0])


def frog(ith):
    if memo[ith] != float("inf"):
        return memo[ith]
    memo[ith] = min(
        frog(ith - 1) + abs(h[ith] - h[ith - 1]),
        frog(ith - 2) + abs(h[ith] - h[ith - 2]),
    )
    return memo[ith]


print(frog(N - 1))
Problem p03160 (Python)

Buggy submission (status: Runtime Error):
def frog():
    n = int(input())
    h_list = list(map(int, input().split()))

    dp = [0] * 100

    for n_element in range(n):
        if n_element >= 2:
            dp[n_element] = min(
                dp[n_element - 1] + abs(h_list[n_element] - h_list[n_element - 1]),
                dp[n_element - 2] + abs(h_list[n_element] - h_list[n_element - 2]),
            )
        elif n_element == 1:
            dp[n_element] = abs(h_list[n_element] - h_list[n_element - 1])
        else:
            dp[n_element] = 0

    print(dp[n - 1])


if __name__ == "__main__":
    frog()


Fixed submission:
def frog():
    n = int(input())
    h_list = list(map(int, input().split()))

    dp = [0] * 10**5

    for n_element in range(n):
        if n_element >= 2:
            dp[n_element] = min(
                dp[n_element - 1] + abs(h_list[n_element] - h_list[n_element - 1]),
                dp[n_element - 2] + abs(h_list[n_element] - h_list[n_element - 2]),
            )
        elif n_element == 1:
            dp[n_element] = abs(h_list[n_element] - h_list[n_element - 1])
        else:
            dp[n_element] = 0

    print(dp[n - 1])


if __name__ == "__main__":
    frog()
Problem p03160 (Python)

Buggy submission (status: Runtime Error):
INF = float("inf")


def LIST():
    return list(map(int, input().split()))


def rec(i: int):
    if dp[i] < INF:
        return dp[i]

    if i == 0:
        return 0

    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]))

    dp[i] = res

    return dp[i]


N = int(input())
h = LIST()

h.extend([0] * 2)

dp = [INF] * (N + 2)

print(rec(N - 1))


Fixed submission:
import sys

sys.setrecursionlimit(10**9)

INF = float("inf")


def LIST():
    return list(map(int, input().split()))


def rec(i: int):
    if dp[i] < INF:
        return dp[i]

    if i == 0:
        return 0

    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]))

    dp[i] = res

    return dp[i]


N = int(input())
h = LIST()

h.extend([0] * 2)

dp = [INF] * (N + 2)

print(rec(N - 1))
Problem p03160 (Python)

Buggy submission (status: Runtime Error):
N, K = map(int, input().split())
K = 2
(*h,) = map(int, input().split())
a = [abs(h[0] - h[k]) for k in range(K)]
for n in range(N - K):
    a = a[1:] + [min([a[k] + abs(h[n + k] - h[n + K]) for k in range(K)])]
print(a[K - 1])


Fixed submission:
N, K = int(input()), 2
(*h,) = map(int, input().split())
a = [abs(h[0] - h[k]) for k in range(K)]
for n in range(N - K):
    a = a[1:] + [min([a[k] + abs(h[n + k] - h[n + K]) for k in range(K)])]
print(a[K - 1])
Problem p03160 (C++)

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

using namespace std;

int n, dp[10002], v[100002];

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


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

using namespace std;

ifstream fin("carte.in");
ofstream fout("carte.out");

int n, dp[100002], v[100002];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
  }
  dp[1] = 0;
  for (int i = 2; i <= n; i++) {
    if (i == 2)
      dp[i] = dp[i - 1] + abs(v[i] - v[i - 1]);
    else
      dp[i] = min(dp[i - 1] + abs(v[i] - v[i - 1]),
                  dp[i - 2] + abs(v[i] - v[i - 2]));
  }
  cout << dp[n];
  return 0;
}
Problem p03160 (Python)

Buggy submission (status: Runtime Error):
import sys

# import numpy as np


def main():
    f = sys.stdin.buffer
    N, K = map(int, f.readline().split())
    H = list(map(int, f.readline().split()))
    dp = [0] * N
    for i in range(1, N):
        dp[i] = min(
            dp[i - k - 1] + abs(H[i] - H[i - k - 1]) for k in range(K) if i - k - 1 >= 0
        )
    print(dp[-1])


if __name__ == "__main__":
    main()


Fixed submission:
n = int(input())
h = list(map(int, input().split()))
p0, p1 = 0, abs(h[1] - h[0])
for i in range(2, n):
    p2 = min(p1 + abs(h[i] - h[i - 1]), p0 + abs(h[i] - h[i - 2]))
    p0, p1 = p1, p2
print(p1)
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);
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define mod 1000000007
#define inf 1000000000000000007
#define eps 0.000001
#define pi 3.141592653589793
#define pii pair<int, int>
#define pdd pair<ld, ld>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define vii vector<int>
#define vpl vector<pll>
#define vll vector<ll>
#define sti stack<int>
#define stll stack<ll>
#define mseti set<ll>
#define msetd multiset<ll, greater<ll>>
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define stp setprecision(20) // use fixed before stp
#define endl '\n'
int main() {
  FAST
#ifndef ONLINE_JUDGE
      freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  ll n;
  cin >> n;
  ll a[n], dp[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    dp[i] = inf;
  }
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (ll i = 2; i < n; i++) {
    dp[i] =
        min(abs(a[i] - a[i - 1]) + dp[i - 1], abs(a[i] - a[i - 2]) + dp[i - 2]);
  }
  cout << dp[n - 1];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define mod 1000000007
#define inf 1000000000000000007
#define eps 0.000001
#define pi 3.141592653589793
#define pii pair<int, int>
#define pdd pair<ld, ld>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define vii vector<int>
#define vpl vector<pll>
#define vll vector<ll>
#define sti stack<int>
#define stll stack<ll>
#define mseti set<ll>
#define msetd multiset<ll, greater<ll>>
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define stp setprecision(20) // use fixed before stp
#define endl '\n'
int main() {
  FAST ll n;
  cin >> n;
  ll a[n], dp[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    dp[i] = inf;
  }
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (ll i = 2; i < n; i++) {
    dp[i] =
        min(abs(a[i] - a[i - 1]) + dp[i - 1], abs(a[i] - a[i - 2]) + dp[i - 2]);
  }
  cout << dp[n - 1];
}
Problem p03160 (C++)

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

int INF = 1001001001;

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

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

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

int INF = 1001001001;

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

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

Buggy submission (status: Runtime Error):
/******************************************
 * AUTHOR : HRITIK AGGARWAL *
 ******************************************/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100005
#define MOD 1000000007
#define dd double
#define vi vector<int>
#define vll vector<ll>
#define forr(i, n) for (int i = 0; i < n; i++)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep1(i, b) 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 ms(s, n) memset(s, n, sizeof(s))
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define int ll
ll po(ll a, ll x, ll m) {
  if (x == 0) {
    return 1;
  }
  ll ans = 1;
  ll k = 1;
  while (k <= x) {
    if (x & k) {
      ans = ((ans * a) % m);
    }
    k <<= 1;
    a *= a;
    a %= m;
  }
  return ans;
}
// DEKHIYE JI  WALID JO THE ABBA HAMARE WO YEH function CHHOD KR GYE THE
int n;
vector<int> a;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("a.txt", "w", stdout);
#endif
  cin >> n;
  a = vector<int>(n);
  forr(i, n) cin >> a[i];
  int dp[n];
  dp[0] = 0;
  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[1] << "\n";
  cout << dp[n - 1] << "\n";
  return 0;
}

Fixed submission:
/******************************************
 * AUTHOR : HRITIK AGGARWAL *
 ******************************************/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100005
#define MOD 1000000007
#define dd double
#define vi vector<int>
#define vll vector<ll>
#define forr(i, n) for (int i = 0; i < n; i++)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep1(i, b) 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 ms(s, n) memset(s, n, sizeof(s))
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define int ll
ll po(ll a, ll x, ll m) {
  if (x == 0) {
    return 1;
  }
  ll ans = 1;
  ll k = 1;
  while (k <= x) {
    if (x & k) {
      ans = ((ans * a) % m);
    }
    k <<= 1;
    a *= a;
    a %= m;
  }
  return ans;
}
// DEKHIYE JI  WALID JO THE ABBA HAMARE WO YEH function CHHOD KR GYE THE
int n;
vector<int> a;

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

  cin >> n;
  a = vector<int>(n);
  forr(i, n) cin >> a[i];
  int dp[n];
  dp[0] = 0;
  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[1] << "\n";
  cout << dp[n - 1] << "\n";
  return 0;
}
Problem p03160 (C++)

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

int h[105], dp[105];

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

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

const int MAXN = 100000 + 10;
int h[MAXN], dp[MAXN];

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

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

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef map<string, int> msi;
typedef set<ll> sl;
typedef set<char> sc;
typedef pair<int, int> pii;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const int INF = 1 << 29;
#define MOD (ll)(1e9 + 7)
#define eps 1e-6
#define pb push_back
#define mp make_pair
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define F first
#define S second

int main() {
  ios::sync_with_stdio(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  int h[n];
  REP(i, n) cin >> h[i];

  int dp[n + 5];
  REP(i, n + 5) dp[i] = INT_MAX;
  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;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef map<string, int> msi;
typedef set<ll> sl;
typedef set<char> sc;
typedef pair<int, int> pii;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const int INF = 1 << 29;
#define MOD (ll)(1e9 + 7)
#define eps 1e-6
#define pb push_back
#define mp make_pair
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define F first
#define S second

int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  int h[n];
  REP(i, n) cin >> h[i];

  int dp[n + 5];
  REP(i, n + 5) dp[i] = INT_MAX;
  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 <bits/stdc++.h>

#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define FASTIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define IN freopen("input.txt", "r", stdin);
#define OUT freopen("output.txt", "w", stdout);
#define LL long long
#define MOD 1000000007
#define INF 1000000000000000000
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)

using namespace std;

const int N = 1e5 + 1;
int n, F[N], h[N];

int main() {
  IN;
  FASTIO;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  h[0] = -1e8;
  for (int i = 2; i <= n; i++) {
    F[i] = F[i - 1] + abs(h[i] - h[i - 1]);
    F[i] = min(F[i], F[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << F[n];
  return 0;
}


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

#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define FASTIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define IN freopen("input.txt", "r", stdin);
#define OUT freopen("output.txt", "w", stdout);
#define LL long long
#define MOD 1000000007
#define INF 1000000000000000000
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)

using namespace std;

const int N = 1e5 + 1;
int n, F[N], h[N];

int main() {
  // IN;
  FASTIO;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  h[0] = -1e8;
  for (int i = 2; i <= n; i++) {
    F[i] = F[i - 1] + abs(h[i] - h[i - 1]);
    F[i] = min(F[i], F[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << F[n];
  return 0;
}
Problem p03160 (C++)

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


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

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


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

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


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

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

#define rep(i, a, b) for (int i = a; i <= b; i++)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int n, h;
vi heights;
int dp[100001];

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

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  cin.sync_with_stdio(0);
  cin.tie();
  cin.exceptions(cin.failbit);

  cin >> n;
  rep(i, 1, n) {
    cin >> h;
    heights.pb(h);
  }
  memset(dp, sizeof dp, -1);
  cout << calc(n - 1);
}

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

#define rep(i, a, b) for (int i = a; i <= b; i++)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int n, h;
vi heights;
int dp[100001];

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

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  cin.sync_with_stdio(0);
  cin.tie();
  cin.exceptions(cin.failbit);

  cin >> n;
  rep(i, 1, n) {
    cin >> h;
    heights.pb(h);
  }
  rep(i, 0, n) dp[i] = -1;
  cout << calc(n - 1);
}
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++)
typedef long long ll;

const int INF = 1001001001;

int main() {
  int N, h[10000], dp[10000];
  cin >> 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] << endl;

  return 0;
}

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

const int INF = 1001001001;

int main() {
  int N, h[100000], dp[100000];
  cin >> 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] << endl;

  return 0;
}