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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define mod 1000000007

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

  fast;

  ll n;
  cin >> n;

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define mod 1000000007

int main() {

  fast;

  ll n;
  cin >> n;

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  long long n, m;
  cin >> n;
  vector<long long> hh(10009, 0);
  vector<long long> hhh(10005, 0);
  for (int i = 0; i < n; i++) {
    cin >> hh.at(i);
  }
  hhh.at(1) = abs(hh.at(0) - hh.at(1));
  for (int i = 2; i < n; i++) {
    hhh.at(i) = min(hhh.at(i - 2) + abs(hh.at(i) - hh.at(i - 2)),
                    hhh.at(i - 1) + abs(hh.at(i) - hh.at(i - 1)));
    if (i == n - 1) {
    }
  }
  cout << hhh.at(n - 1) << '\n';
  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  long long n, m;
  cin >> n;
  vector<long long> hh(100000, 0);
  vector<long long> hhh(100000, 0);
  for (int i = 0; i < n; i++) {
    cin >> hh.at(i);
  }
  hhh.at(1) = abs(hh.at(0) - hh.at(1));
  for (int i = 2; i < n; i++) {
    hhh.at(i) = min(hhh.at(i - 2) + abs(hh.at(i) - hh.at(i - 2)),
                    hhh.at(i - 1) + abs(hh.at(i) - hh.at(i - 1)));
    if (i == n - 1) {
    }
  }
  cout << hhh.at(n - 1) << '\n';
  return (0);
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <iostream>
using namespace std;
#define ll long long
ll dp[100];
ll arr[100] = {0};
int n = 0;

ll solve(int i) {
  if (i == n - 1)
    return 0;

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

  ll op1, op2;
  op1 = abs(arr[i] - arr[i + 1]) + solve(i + 1);
  op2 = INT_MAX;
  if (i + 2 < n)
    op2 = abs(arr[i] - arr[i + 2]) + solve(i + 2);

  return dp[i] = min(op1, op2);
}

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


Fixed submission:
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <iostream>
using namespace std;
#define ll long long
ll dp[100009];
ll arr[100009] = {0};
int n = 0;

ll solve(int i) {
  if (i == n - 1)
    return 0;

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

  ll op1, op2;
  op1 = abs(arr[i] - arr[i + 1]) + solve(i + 1);
  op2 = INT_MAX;
  if (i + 2 < n)
    op2 = abs(arr[i] - arr[i + 2]) + solve(i + 2);

  return dp[i] = min(op1, op2);
}

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

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

long long int jump(int n, int h[], int start, int end, long long int y[]) {

  if (y[n] != -1) {
    return y[n];
  }
  if (end == start + 1) {
    int k = abs(h[end] - h[start]);
    y[n] = k;
    return y[n];
  }
  if (end == start) {
    return 0;
  }
  int d = abs(h[start + 2] - h[start]);
  int e = abs(h[start + 1] - h[start]);
  y[n] = min(d + jump(n - 2, h, start + 2, end, y),
             e + jump(n - 1, h, start + 1, end, y));
  return y[n];
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int g = n;
  int h[n];
  long long int y[1000001];
  memset(y, -1, sizeof(y));
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  int f = jump(n, h, 0, g - 1, y);
  cout << f;

  // #ifndef ONLINE_JUDGE
  //     cout<<"\nTime Elapsed : "<< 1.0*clock() / CLOCKS_PER_SEC << " s\n";
  // #endif

  return 0;
}

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

long long int jump(int n, int h[], int start, int end, long long int y[]) {

  if (y[n] != -1) {
    return y[n];
  }
  if (end == start + 1) {
    int k = abs(h[end] - h[start]);
    y[n] = k;
    return y[n];
  }
  if (end == start) {
    return 0;
  }
  int d = abs(h[start + 2] - h[start]);
  int e = abs(h[start + 1] - h[start]);
  y[n] = min(d + jump(n - 2, h, start + 2, end, y),
             e + jump(n - 1, h, start + 1, end, y));
  return y[n];
}

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int g = n;
  int h[n];
  long long int y[1000001];
  memset(y, -1, sizeof(y));
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  int f = jump(n, h, 0, g - 1, y);
  cout << f;

  // #ifndef ONLINE_JUDGE
  //     cout<<"\nTime Elapsed : "<< 1.0*clock() / CLOCKS_PER_SEC << " s\n";
  // #endif

  return 0;
}
Problem p03160 (C++)

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

using namespace std;

long long min(long long a, long long b);

int main(void) {
  long long N, h[10001], sum[10001], i;

  cin >> N;

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

  if (i == 1) {
    cout << 0 << endl;
  }

  else if (i == 2) {
    cout << abs(h[1] - h[2]) << endl;
  }

  else {
    sum[1] = 0;

    sum[2] = abs(h[1] - h[2]);

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

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

  return 0;
}

long long min(long long a, long long b) {
  if (a < b) {
    return a;
  }

  else {
    return b;
  }
}

Fixed submission:
#include <iostream>
#include <math.h>

using namespace std;

long long min(long long a, long long b);

int main(void) {
  long long N, h[100001], sum[100001], i;

  cin >> N;

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

  if (i == 1) {
    cout << 0 << endl;
  }

  else if (i == 2) {
    cout << abs(h[1] - h[2]) << endl;
  }

  else {
    sum[1] = 0;

    sum[2] = abs(h[1] - h[2]);

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

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

  return 0;
}

long long min(long long a, long long b) {
  if (a < b) {
    return a;
  }

  else {
    return b;
  }
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = 0; i > (int)(n); i--)
#define ALL(v) v.begin(), v.end()
#define INF 1e9
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using namespace std;

int h[10001], dp[10001];

int main(void) {
  int N;
  cin >> N;
  REP(i, N) { cin >> h[i]; }
  REP(i, N) { dp[i] = INF; }
  dp[0] = 0;

  REP(i, N) {
    chmin(dp[i + 1], dp[i] + abs(h[i] - h[i + 1]));
    chmin(dp[i + 2], dp[i] + abs(h[i] - h[i + 2]));
  }

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


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = 0; i > (int)(n); i--)
#define ALL(v) v.begin(), v.end()
#define INF 1e9
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using namespace std;

int h[100001], dp[100001];

int main(void) {
  int N;
  cin >> N;
  REP(i, N) { cin >> h[i]; }
  REP(i, N) { dp[i] = INF; }
  dp[0] = 0;

  REP(i, N) {
    chmin(dp[i + 1], dp[i] + abs(h[i] - h[i + 1]));
    chmin(dp[i + 2], dp[i] + abs(h[i] - h[i + 2]));
  }

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

Buggy submission (status: Runtime Error):

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <vector>
using namespace std;

#define int long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define FF first
#define SS second
#define sz(x) (int)x.size()
#define endl '\n'
#define rep(i, a, b) for (int i = a; i < b; i++)
const int hell = 1000000007;

int h[100010] = {0};
int dp[100010];
int n;

int frog(int index) {
  if (index >= n)
    return 0;
  if (dp[index] != -1)
    return dp[index];

  int ans = hell;
  rep(i, 1, 3) {
    if (index + i <= n) {
      ans = min(ans, abs(h[index + i] - h[index]) + frog(index + i));
    }
  }
  // if(dp[index]!=-1)dp[index]=ans;
  // else dp[index]=min(dp[index],ans);

  return dp[index] = ans;
}

int solve() {

  memset(dp, -1, sizeof(dp));
  cin >> n;
  rep(i, 0, n) cin >> h[i + 1];
  cout << frog(1);

  return 0;
}

signed main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

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

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

  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <vector>
using namespace std;

#define int long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define FF first
#define SS second
#define sz(x) (int)x.size()
#define endl '\n'
#define rep(i, a, b) for (int i = a; i < b; i++)
const int hell = 1000000007;

int h[100010] = {0};
int dp[100010];
int n;

int frog(int index) {
  if (index >= n)
    return 0;
  if (dp[index] != -1)
    return dp[index];

  int ans = hell;
  rep(i, 1, 3) {
    if (index + i <= n) {
      ans = min(ans, abs(h[index + i] - h[index]) + frog(index + i));
    }
  }
  // if(dp[index]!=-1)dp[index]=ans;
  // else dp[index]=min(dp[index],ans);

  return dp[index] = ans;
}

int solve() {

  memset(dp, -1, sizeof(dp));
  cin >> n;
  rep(i, 0, n) cin >> h[i + 1];
  cout << frog(1);

  return 0;
}

signed main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

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

  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF (long long)(1e18)
#define MOD (int)(1e9 + 7)

#define yn(f) ((f) ? "Yes" : "No")
#define YN(f) ((f) ? "YES" : "NO")

#define MAX 11000

int dp[MAX], h[MAX];

signed main() {
  cout << fixed << setprecision(7);

  int N;

  cin >> N;

  for (int i = 1; i <= N; i++) {
    cin >> h[i];
    dp[i] = INF;
  }
  dp[1] = 0;
  for (int i = 1; i < N; 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] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF (long long)(1e18)
#define MOD (int)(1e9 + 7)

#define yn(f) ((f) ? "Yes" : "No")
#define YN(f) ((f) ? "YES" : "NO")

#define MAX 110000

int dp[MAX], h[MAX];

signed main() {
  cout << fixed << setprecision(7);

  int N;

  cin >> N;

  for (int i = 1; i <= N; i++) {
    cin >> h[i];
    dp[i] = INF;
  }
  dp[1] = 0;
  for (int i = 1; i < N; 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] << endl;

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define ll long long
#define inf (1LL << 61)
#define MOD 1000000007
const ll N = 2e5 + 5;
template <typename T> T gcd(T a, T b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
template <typename T> T pow(T a, T b, long long m) {
  T ans = 1;
  while (b > 0) {
    if (b % 2 == 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans % m;
}
ll n;
ll h[100005];
ll cache[100005];
ll dp(ll i) {
  if (i > n)
    return inf;
  if (i == n)
    return 0;
  ll &ans = cache[i];
  ans = inf;
  ans = min(ans, dp(i + 1) + abs(h[i + 1] - h[i]));
  ans = min(ans, dp(i + 2) + abs(h[i + 2] - h[i]));

  return ans;
}
int main() {
  IOS;
  cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> h[i];

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

  ll ans = dp(1);
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define ll long long
#define inf (1LL << 61)
#define MOD 1000000007
const ll N = 2e5 + 5;
template <typename T> T gcd(T a, T b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
template <typename T> T pow(T a, T b, long long m) {
  T ans = 1;
  while (b > 0) {
    if (b % 2 == 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans % m;
}
ll n;
ll h[100005];
ll cache[100005];
ll dp(ll i) {
  if (i > n)
    return inf;
  if (i == n)
    return 0;
  ll &ans = cache[i];
  if (ans != -1)
    return ans;
  ans = inf;
  ans = min(ans, dp(i + 1) + abs(h[i + 1] - h[i]));
  ans = min(ans, dp(i + 2) + abs(h[i + 2] - h[i]));

  return ans;
}
int main() {
  IOS;
  cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> h[i];

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

  ll ans = dp(1);
  cout << ans << endl;

  return 0;
}
Problem p03160 (C++)

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

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

  d[1] = 0;
  d[2] = abs(h[2] - h[1]);
  for (int i = 3; i <= n; ++i) {
    auto cost1 = abs(h[i] - h[i - 1]) + d[i - 1];
    auto cost2 = abs(h[i] - h[i - 2]) + d[i - 2];
    d[i] = min(cost1, cost2);
  }
  cout << d[n] << endl;

  return 0;
}

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

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

  d[1] = 0;
  d[2] = abs(h[2] - h[1]);
  for (int i = 3; i <= n; ++i) {
    auto cost1 = abs(h[i] - h[i - 1]) + d[i - 1];
    auto cost2 = abs(h[i] - h[i - 2]) + d[i - 2];
    d[i] = min(cost1, cost2);
  }
  cout << d[n] << endl;

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int solve(vector<int> h, vector<int> a, int i) {
  if (a.at(i) > -1) {
    return a.at(i);
  } else {
    a.at(i) = min(a.at(i - 2) + abs(h.at(i) - h.at(i - 2)),
                  a.at(i - 1) + abs(h.at(i) - h.at(i - 1)));
    return a.at(i);
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> h(n);
  for (size_t i = 0; i < n; i++) {
    /* code */
    cin >> h.at(i);
  }
  vector<int> hyo(n, -1);
  hyo.at(0) = 0;
  hyo.at(1) = abs(h.at(1) - h.at(0));
  for (int i = 2; i < n; i++) {
    /* code */
    hyo.at(i) = solve(h, hyo, i);
  }

  int ans = hyo.at(n - 1);
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int solve(vector<int> &h, vector<int> &a, int i) {
  if (a.at(i) > -1) {
    return a.at(i);
  } else {
    a.at(i) = min(a.at(i - 2) + abs(h.at(i) - h.at(i - 2)),
                  a.at(i - 1) + abs(h.at(i) - h.at(i - 1)));
    return a.at(i);
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> h(n);
  for (size_t i = 0; i < n; i++) {
    /* code */
    cin >> h.at(i);
  }
  vector<int> hyo(n, -1);
  hyo.at(0) = 0;
  hyo.at(1) = abs(h.at(1) - h.at(0));
  for (int i = 2; i < n; i++) {
    /* code */
    hyo.at(i) = solve(h, hyo, i);
  }

  int ans = hyo.at(n - 1);
  cout << ans << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define per(i, n) for (long long i = (long long)(n - 1); i >= 0; --i)
#define REP(i, s, t) for (long long i = s; i < t; i++)
#define PER(i, s, t) for (long long i = s; i > t; --i)
#define all(x) x.begin(), x.end()
#define sp(n) setprecision(n)
typedef long long ll;
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(10010, 0), dp(10010, 0);
  rep(i, n) { cin >> a[i]; }
  dp[1] = abs(a[0] - a[1]);
  REP(i, 2, n) {
    dp[i] =
        min(dp[i - 2] + abs(a[i - 2] - a[i]), dp[i - 1] + abs(a[i - 1] - a[i]));
  }
  cout << dp[n - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define per(i, n) for (long long i = (long long)(n - 1); i >= 0; --i)
#define REP(i, s, t) for (long long i = s; i < t; i++)
#define PER(i, s, t) for (long long i = s; i > t; --i)
#define all(x) x.begin(), x.end()
#define sp(n) setprecision(n)
typedef long long ll;
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(100100, 0), dp(100100, 0);
  rep(i, n) { cin >> a[i]; }
  dp[1] = abs(a[0] - a[1]);
  REP(i, 2, n) {
    dp[i] =
        min(dp[i - 2] + abs(a[i - 2] - a[i]), dp[i - 1] + abs(a[i - 1] - a[i]));
  }
  cout << dp[n - 1] << endl;
}
Problem p03160 (C++)

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

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

const long long INF = 1LL << 60;
long long dp[10010];

int h[100010];
long long rec(int i) {
  if (dp[i] < INF)
    return dp[i];

  if (i == 0)
    return 0;

  long long res = INF;
  chmin(res, rec(i - 1) + abs(h[i] - h[i - 1]));
  if (i > 1)
    chmin(res, rec(i - 2) + abs(h[i] - h[i - 2]));

  return dp[i] = res;
}

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

  for (auto &x : dp)
    x = INF;
  cout << rec(n - 1) << endl;
}


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

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

const long long INF = 1LL << 60;
long long dp[100010];

int h[100010];
long long rec(int i) {
  if (dp[i] < INF)
    return dp[i];

  if (i == 0)
    return 0;

  long long res = INF;
  chmin(res, rec(i - 1) + abs(h[i] - h[i - 1]));
  if (i > 1)
    chmin(res, rec(i - 2) + abs(h[i] - h[i - 2]));

  return dp[i] = res;
}

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

  for (auto &x : dp)
    x = INF;
  cout << rec(n - 1) << endl;
}
Problem p03160 (C++)

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

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(a) (a).begin(), (a).end()
#define PI 3.141592653589793238
#define INF 1050000000

using vi = vector<int>;

int N;

vector<long long> h(N + 1);

long long dp[110000];

long long dfs(int k) {

  if (k >= N)
    return INF;
  if (k == N - 1)
    return 0;
  if (dp[k] != -1)
    return dp[k];

  long long step1 = dfs(k + 1) + abs(h[k + 1] - h[k]);
  long long step2 = dfs(k + 2) + abs(h[k + 2] - h[k]);

  return dp[k] = min(step1, step2);
}

int main() {

  cin >> N;

  REP(i, N) { cin >> h[i]; }

  REP(i, 110000) { dp[i] = -1; }

  cout << dfs(0) << endl;

  return 0;
}

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

using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(a) (a).begin(), (a).end()
#define PI 3.141592653589793238
#define INF 1050000000

using vi = vector<int>;

int N;

vector<long long> h(110000);

long long dp[110000];

long long dfs(int k) {

  if (k >= N)
    return INF;
  if (k == N - 1)
    return 0;
  if (dp[k] != -1)
    return dp[k];

  long long step1 = dfs(k + 1) + abs(h[k + 1] - h[k]);
  long long step2 = dfs(k + 2) + abs(h[k + 2] - h[k]);

  return dp[k] = min(step1, step2);
}

int main() {

  cin >> N;

  REP(i, N) { cin >> h[i]; }

  REP(i, 110000) { dp[i] = -1; }

  cout << dfs(0) << endl;

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define FASTIO                                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false)
#define ll long long int
#define F first
#define S second
#define pll pair<ll, ll>
#define vl vector<ll>
#define pb push_back

struct compare {
  bool operator()(const pair<int, int> &a, const pair<int, int> &b) const {
    if ((a.S - a.F) != (b.S - b.F))
      return ((a.S - a.F) > (b.S - b.F));
    else
      return (a.F < b.F);
  }
};

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  FASTIO;
  ll n, i;
  cin >> n;
  vl v(n), dp(n, LLONG_MAX);
  for (i = 0; i < n; i++)
    cin >> v[i];
  dp[0] = 0;
  for (i = 0; i < n; i++) {
    if ((i + 1) < n)
      dp[i + 1] = min(dp[i + 1], (dp[i] + abs(v[i + 1] - v[i])));
    if ((i + 2) < n)
      dp[i + 2] = min(dp[i + 2], (dp[i] + abs(v[i + 2] - v[i])));
  }
  cout << dp[n - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define FASTIO                                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false)
#define ll long long int
#define F first
#define S second
#define pll pair<ll, ll>
#define vl vector<ll>
#define pb push_back

struct compare {
  bool operator()(const pair<int, int> &a, const pair<int, int> &b) const {
    if ((a.S - a.F) != (b.S - b.F))
      return ((a.S - a.F) > (b.S - b.F));
    else
      return (a.F < b.F);
  }
};

int main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif
  FASTIO;
  ll n, i;
  cin >> n;
  vl v(n), dp(n, LLONG_MAX);
  for (i = 0; i < n; i++)
    cin >> v[i];
  dp[0] = 0;
  for (i = 0; i < n; i++) {
    if ((i + 1) < n)
      dp[i + 1] = min(dp[i + 1], (dp[i] + abs(v[i + 1] - v[i])));
    if ((i + 2) < n)
      dp[i + 2] = min(dp[i + 2], (dp[i] + abs(v[i + 2] - v[i])));
  }
  cout << dp[n - 1] << endl;
}
Problem p03160 (C++)

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

void chmin(int &a, int b) {
  if (a > b) {
    a = b;
  }
}

int INF = 1000000000;
int N;
vector<int> dp(N), h(N);

int rec(int i) {
  if (dp.at(i) < INF) {
    return dp.at(i);
  }
  if (i == 0) {
    return 0;
  }
  int res = INF;
  chmin(res, rec(i - 1) + abs(h.at(i - 1) - h.at(i)));
  if (i > 1) {
    chmin(res, rec(i - 2) + abs(h.at(i - 2) - h.at(i)));
  }
  return dp.at(i) = res;
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> h.at(i);
    dp.at(i) = INF;
  }
  cout << rec(N - 1) << endl;
}

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

void chmin(int &a, int b) {
  if (a > b) {
    a = b;
  }
}

int INF = 1000000000;
int N;
vector<int> dp(100010), h(100010);

int rec(int i) {
  if (dp.at(i) < INF) {
    return dp.at(i);
  }
  if (i == 0) {
    return 0;
  }
  int res = INF;
  chmin(res, rec(i - 1) + abs(h.at(i - 1) - h.at(i)));
  if (i > 1) {
    chmin(res, rec(i - 2) + abs(h.at(i - 2) - h.at(i)));
  }
  return dp.at(i) = res;
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> h.at(i);
    dp.at(i) = INF;
  }
  cout << rec(N - 1) << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/stack:256000000")
// #pragma GCC optimize("Ofast")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <ccomplex>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define mp make_pair
#define X first
#define Y second

using namespace std;
typedef long long ll;
typedef long double ld;

mt19937_64 gen(time(NULL));
ll const maxn = 1e4 + 4;
ll dp[maxn];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  vector<ll> h(1 + n);
  for (ll i = 1; i <= n; i++) {
    cin >> h[i];
  }
  for (ll i = 0; i < maxn; i++)
    dp[i] = 1e9;
  dp[1] = 0;
  for (ll i = 2; i <= n; i++) {
    dp[i] = min(dp[i], dp[i - 1] + abs(h[i] - h[i - 1]));
    if (i == 2)
      continue;
    dp[i] = min(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << dp[n] << endl;
  return 0;
}
/*
7 27 2
9 1 1 27 3 27 3

7 60 4
30 1 1 3 10 6 4
*/

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/stack:256000000")
// #pragma GCC optimize("Ofast")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <ccomplex>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define mp make_pair
#define X first
#define Y second

using namespace std;
typedef long long ll;
typedef long double ld;

mt19937_64 gen(time(NULL));
ll const maxn = 1e5 + 4;
ll dp[maxn];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  vector<ll> h(1 + n);
  for (ll i = 1; i <= n; i++) {
    cin >> h[i];
  }
  for (ll i = 0; i < maxn; i++)
    dp[i] = 1e9;
  dp[1] = 0;
  for (ll i = 2; i <= n; i++) {
    dp[i] = min(dp[i], dp[i - 1] + abs(h[i] - h[i - 1]));
    if (i == 2)
      continue;
    dp[i] = min(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << dp[n] << endl;
  return 0;
}
/*
7 27 2
9 1 1 27 3 27 3

7 60 4
30 1 1 3 10 6 4
*/
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll dp[11009];
ll val[11009];
ll n;
ll frog(ll i) {
  if (i == n - 1)
    return 0;
  if (i > n - 1)
    return 989898;
  // cout<<"I = "<<i<<endl;

  //    if(i==n-1)return (abs(val[i]-val[n-1]));
  //    if(i==n-2)return (abs(val[i]-val[n-2]));
  if (dp[i] != -1)
    return dp[i];
  ll f1, f2;
  f1 = frog(i + 1) + abs(val[i] - val[i + 1]);
  //  cout<<"f1 = "<<f1<<endl;
  f2 = frog(i + 2) + abs(val[i] - val[i + 2]);
  // cout<<" f2 = "<<f2<<endl;

  dp[i] = min(f1, f2);
  return dp[i];
}
int main() {
  cin >> n;
  for (ll i = 0; i < n; i++) {
    ll d;
    cin >> d;
    val[i] = d;
  }
  memset(dp, -1, sizeof(dp));
  ll ans = frog(0);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll dp[100009];
ll val[100009];
ll n;
ll frog(ll i) {
  if (i == n - 1)
    return 0;
  if (i > n - 1)
    return 989898;
  // cout<<"I = "<<i<<endl;

  //    if(i==n-1)return (abs(val[i]-val[n-1]));
  //    if(i==n-2)return (abs(val[i]-val[n-2]));
  if (dp[i] != -1)
    return dp[i];
  ll f1, f2;
  f1 = frog(i + 1) + abs(val[i] - val[i + 1]);
  //  cout<<"f1 = "<<f1<<endl;
  f2 = frog(i + 2) + abs(val[i] - val[i + 2]);
  // cout<<" f2 = "<<f2<<endl;

  dp[i] = min(f1, f2);
  return dp[i];
}
int main() {
  cin >> n;
  for (ll i = 0; i < n; i++) {
    ll d;
    cin >> d;
    val[i] = d;
  }
  memset(dp, -1, sizeof(dp));
  ll ans = frog(0);
  cout << ans << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):

//													\[T]/
//Praise
//													 (*)
//The 													 / \  Sun

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

typedef long long ll;
typedef long double dbl;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<long long> vl;
typedef vector<pair<int, int>> vii;
#define mp make_pair
#define pb push_back
#define invec(a, n)                                                            \
  for (ll i = 0; i < n; i++) {                                                 \
    cin >> a[i];                                                               \
  }
#define rep(n) for (ll i = 0; i < n; i++)
#define fr(i, j, k) for (ll i = j; i < k; i++)
#define FR(i, j, k) for (ll i = j; i >= 0; i--)
#define MOD 1000000007
#define all(v) v.begin(), v.end()
#define INF 1000000
#define xx first
#define yy second

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("/home/ram/Competitive Programming/IO/in.txt", "r", stdin);
  freopen("/home/ram/Competitive Programming/IO/out.txt", "w", stdout);
#endif

  int n;
  cin >> n;

  vl a(n), dp(n);

  invec(a, n);

  dp[0] = 0;

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

  fr(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] << "\n";
}

Fixed submission:

//													\[T]/
//Praise
//													 (*)
//The 													 / \  Sun

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

typedef long long ll;
typedef long double dbl;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<long long> vl;
typedef vector<pair<int, int>> vii;
#define mp make_pair
#define pb push_back
#define invec(a, n)                                                            \
  for (ll i = 0; i < n; i++) {                                                 \
    cin >> a[i];                                                               \
  }
#define rep(n) for (ll i = 0; i < n; i++)
#define fr(i, j, k) for (ll i = j; i < k; i++)
#define FR(i, j, k) for (ll i = j; i >= 0; i--)
#define MOD 1000000007
#define all(v) v.begin(), v.end()
#define INF 1000000
#define xx first
#define yy second

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

int main() {

  // #ifndef ONLINE_JUDGE
  // freopen("/home/ram/Competitive Programming/IO/in.txt", "r", stdin);
  // freopen("/home/ram/Competitive Programming/IO/out.txt", "w", stdout);
  // #endif

  int n;
  cin >> n;

  vl a(n), dp(n);

  invec(a, n);

  dp[0] = 0;

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

  fr(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] << "\n";
}
Problem p03160 (C++)

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

using namespace std;

// template<typename... T>
// void read(T&... args) {
//     ((cin >> args), ...);
// }

// template<typename... T>
// void write(T&&... args) {
//     ((cout << args << " "), ...);
// }

// #define deb(x) cerr << #x << " " << x <<endl;

// #define debs(args ...) cerr << __LINE__ << ": ", err(new
// istringstream(string(#args)), args), cerr << '\n' void err(istringstream
// *iss) {} template<typename T, typename ... Args> void err(istringstream *iss,
// const T &_val, const Args & ... args) {
//     string _name;
//     *iss >> _name;
//     if (_name.back()==',') _name.pop_back();
//     cerr << _name << " = " << _val << "; ", err(iss, args ...);
// }

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n;
  while (cin >> n) {
    vector<int> lst(n);
    for (int i = 0; i < n; cin >> lst[i++])
      ;
    vector<long long> dp(n + 2, 0);
    dp[2] = abs(lst[1] - lst[0]);

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

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

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;

// template<typename... T>
// void read(T&... args) {
//     ((cin >> args), ...);
// }

// template<typename... T>
// void write(T&&... args) {
//     ((cout << args << " "), ...);
// }

// #define deb(x) cerr << #x << " " << x <<endl;

// #define debs(args ...) cerr << __LINE__ << ": ", err(new
// istringstream(string(#args)), args), cerr << '\n' void err(istringstream
// *iss) {} template<typename T, typename ... Args> void err(istringstream *iss,
// const T &_val, const Args & ... args) {
//     string _name;
//     *iss >> _name;
//     if (_name.back()==',') _name.pop_back();
//     cerr << _name << " = " << _val << "; ", err(iss, args ...);
// }

int main() {

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

  int n;
  while (cin >> n) {
    vector<int> lst(n);
    for (int i = 0; i < n; cin >> lst[i++])
      ;
    vector<long long> dp(n + 2, 0);
    dp[2] = abs(lst[1] - lst[0]);

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

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

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <unordered_map>
#define M 1000000007
#define T 998244353
#define PI 3.142
#define ll long long

using namespace std;

int ab(int a, int b) {
  if (a < b) {
    return b - a;
  }
  return a - b;
}

int mini(int a, int b) {
  if (a > b) {
    return b;
  }
  return a;
}

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

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

  int n, i;
  cin >> n;
  int a[n], cost[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  cost[n - 1] = ab(a[n - 1], a[n - 2]);
  cost[n - 2] =
      mini(cost[n - 1] + ab(a[n - 2], a[n - 3]), ab(a[n - 1], a[n - 3]));
  for (i = n - 3; i > 0; i--) {
    cost[i] = mini(cost[i + 1] + ab(a[i], a[i - 1]),
                   cost[i + 2] + ab(a[i - 1], a[i + 1]));
  }
  cout << cost[1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <unordered_map>
#define M 1000000007
#define T 998244353
#define PI 3.142
#define ll long long

using namespace std;

int ab(int a, int b) {
  if (a < b) {
    return b - a;
  }
  return a - b;
}

int mini(int a, int b) {
  if (a > b) {
    return b;
  }
  return a;
}

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

  int n, i;
  cin >> n;
  int a[n], cost[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  cost[n - 1] = ab(a[n - 1], a[n - 2]);
  cost[n - 2] =
      mini(cost[n - 1] + ab(a[n - 2], a[n - 3]), ab(a[n - 1], a[n - 3]));
  for (i = n - 3; i > 0; i--) {
    cost[i] = mini(cost[i + 1] + ab(a[i], a[i - 1]),
                   cost[i + 2] + ab(a[i - 1], a[i + 1]));
  }
  cout << cost[1] << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1LL << 60;
const int MOD = 1000000007;

template <typename T> void print(const T &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << ' ';
    cout << v[i];
  }
  cout << endl;
}

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

const int MAX = 10010;
int dp[MAX];

int main() {
  int n;
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1LL << 60;
const int MOD = 1000000007;

template <typename T> void print(const T &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i)
      cout << ' ';
    cout << v[i];
  }
  cout << endl;
}

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

const int MAX = 100010;
ll dp[MAX];

int main() {
  int n;
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);

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

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

#define ll long long

int dp[100001];
int n, arr[100001];
int pound(int pos) {
  if (dp[pos] != -1)
    return dp[pos];

  return dp[pos] = min(abs(arr[pos] - arr[pos - 1]) + pound(pos - 1),
                       abs(arr[pos] - arr[pos - 2]) + pound(pos - 2));
}

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

#ifndef ONLINE_JUDGE
  freopen("frog_in.txt", "r", stdin);
#endif

  cin >> n;

  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; ++i)
    cin >> arr[i];

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

  cout << pound(n - 1);

  return 0;
}


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

#define ll long long

int dp[100001];
int n, arr[100001];
int pound(int pos) {
  if (dp[pos] != -1)
    return dp[pos];

  return dp[pos] = min(abs(arr[pos] - arr[pos - 1]) + pound(pos - 1),
                       abs(arr[pos] - arr[pos - 2]) + pound(pos - 2));
}

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

  cin >> n;

  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; ++i)
    cin >> arr[i];

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

  cout << pound(n - 1);

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, h[100001], min_cost[100001];
int mincost(int i) {
  if (i == 0)
    return 0;
  if (i == 1)
    return abs(h[1] - h[0]);
  if (min_cost[i - 1] == -1)
    min_cost[i - 1] = mincost(i - 1);
  if (min_cost[i - 2] == -1)
    min_cost[i - 2] = mincost(i - 2);
  return min(abs(h[i] - h[i - 1]) + min_cost[i - 1],
             abs(h[i] - h[i - 2]) + min_cost[i - 2]);
}
int32_t 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
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    min_cost[i] = -1;
  cout << mincost(n - 1);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, h[100001], min_cost[100001];
int mincost(int i) {
  if (i == 0)
    return 0;
  if (i == 1)
    return abs(h[1] - h[0]);
  if (min_cost[i - 1] == -1)
    min_cost[i - 1] = mincost(i - 1);
  if (min_cost[i - 2] == -1)
    min_cost[i - 2] = mincost(i - 2);
  return min(abs(h[i] - h[i - 1]) + min_cost[i - 1],
             abs(h[i] - h[i - 2]) + min_cost[i - 2]);
}
int32_t 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
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    min_cost[i] = -1;
  cout << mincost(n - 1);
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// slight modification similar logic errichto's code

#include <bits/stdc++.h>

using namespace std;

const int INF = 1e9 + 5;

int main() {

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

  int n;
  vector<int> h(n);

  for (int &x : h) {
    cin >> x;
  }

  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[i] - h[j]));
      }
    }
  }

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

  return 0;
}

Fixed submission:
// slight modification similar logic errichto's code

#include <bits/stdc++.h>

using namespace std;

const int INF = 1e9 + 5;

int main() {

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

  int n;
  cin >> n;
  vector<int> h(n);

  for (int &x : h) {
    cin >> x;
  }

  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[i] - h[j]));
      }
    }
  }

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

  return 0;
}
Problem p03160 (C++)

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

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) FOR(i, 0, n - 1)
/* #define INF 1e10 */
typedef long long ll;
const ll INF = 1LL << 60;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

void solve(long long N, vector<long long> h) {
  vector<ll> dp(N + 100); //, INF);
  for (int i = 0; i < 100010; ++i)
    dp[i] = INF;

  // initial point
  dp[0] = 0;

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

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

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  cin >> N;
  vector<long long> h(N + 100, 0);
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  solve(N, move(h));
}


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

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) FOR(i, 0, n - 1)
/* #define INF 1e10 */
typedef long long ll;
const ll INF = 1LL << 60;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

void solve(long long N, vector<long long> h) {
  vector<ll> dp(N + 10000, INF);

  // initial point
  dp[0] = 0;

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

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

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  cin >> N;
  vector<long long> h(N + 100, 0);
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  solve(N, move(h));
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
ll mod = 1e9 + 7;
#define PI 3.1415926535897932385
#define INF 100000000000000000ll
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#include <string>
string char_to_str(char c) {
  string tem(1, c);
  return tem;
}

typedef pair<int, int> ii;
#define S second
#define F first
ll max(ll a, ll b) {
  if (a > b) {
    return a;
  }
  return b;
}
ll min(ll a, ll b) {
  if (a < b) {
    return a;
  }
  return b;
}
// string to integer stoi() Remember: it takes string not character
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

// To compile--> g++ -std=c++0x -o output one.cpp
// To run--> ./output
int main() {
  fastio;
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int ans[n];
  ans[0] = 0;
  ans[1] = abs(a[1] - a[0]);
  for (int i = 2; i < n; ++i) {
    ans[i] = min(ans[i - 1] + abs(a[i - 1] - a[i]),
                 ans[i - 2] + abs(a[i - 2] - a[i]));
  }
  cout << ans[n - 1];

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
ll mod = 1e9 + 7;
#define PI 3.1415926535897932385
#define INF 100000000000000000ll
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#include <string>
string char_to_str(char c) {
  string tem(1, c);
  return tem;
}

typedef pair<int, int> ii;
#define S second
#define F first
ll max(ll a, ll b) {
  if (a > b) {
    return a;
  }
  return b;
}
ll min(ll a, ll b) {
  if (a < b) {
    return a;
  }
  return b;
}
// string to integer stoi() Remember: it takes string not character
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

// To compile--> g++ -std=c++0x -o output one.cpp
// To run--> ./output
int main() {
  fastio;
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int ans[n];
  ans[0] = 0;
  ans[1] = abs(a[1] - a[0]);
  for (int i = 2; i < n; ++i) {
    ans[i] = min(ans[i - 1] + abs(a[i - 1] - a[i]),
                 ans[i - 2] + abs(a[i - 2] - a[i]));
  }
  cout << ans[n - 1];

  return 0;
}