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

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;
int a[50], arr[50], n;
int topdown(int x);
int main() {
  int ans;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i < n; i++) {
    a[i] = -1;
  }
  ans = topdown(0);
  cout << ans;
  return 0;
}
int topdown(int x) {
  if (a[x] != -1) {
    return a[x];
  }
  if (x == n - 3 || x == n - 2) {
    a[x] = abs(arr[n - 1] - arr[x]);
    return a[x];
  }
  int q;
  q = min(topdown(x + 1) + abs(arr[x + 1] - arr[x]),
          topdown(x + 2) + abs(arr[x + 2] - arr[x]));
  a[x] = q;
  return q;
}


Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
int a[100000], arr[100000], n;
int topdown(int x);
int main() {
  int ans;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i < n; i++) {
    a[i] = -1;
  }
  ans = topdown(0);
  cout << ans;
  return 0;
}
int topdown(int x) {
  if (a[x] != -1) {
    return a[x];
  }
  if (x == n - 3 || x == n - 2) {
    a[x] = abs(arr[n - 1] - arr[x]);
    return a[x];
  }
  int q;
  q = min(topdown(x + 1) + abs(arr[x + 1] - arr[x]),
          topdown(x + 2) + abs(arr[x + 2] - arr[x]));
  a[x] = q;
  return q;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int INF = 0x3f3f3f3f;
;
vector<int> h(1e5 + 10);
vector<int> dp(1e5 + 10, INF);

int dfs(int j) {
  if (j <= 0)
    return 0;
  if (j == 1) {
    return abs(h[1] - h[0]);
  }
  if (j == 2) {
    return abs(h[2] - h[0]);
  }
  if (dp[j] != INF)
    return dp[j];

  return min(abs(h[j] - h[j - 1]) + dfs(j - 1),
             abs(h[j] - h[j - 2]) + dfs(j - 2));
}
int main() {
  int n;
  cin >> n;

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

  cout << dfs(n - 1) << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int INF = 0x3f3f3f3f;
;
vector<int> h(1e5 + 10);
vector<int> dp(1e5 + 10, INF);

int dfs(int j) {
  if (j <= 0)
    return 0;
  if (j == 1) {
    return abs(h[1] - h[0]);
  }
  if (j == 2) {
    return abs(h[2] - h[0]);
  }
  if (dp[j] != INF)
    return dp[j];

  dp[j] =
      min(abs(h[j] - h[j - 1]) + dfs(j - 1), abs(h[j] - h[j - 2]) + dfs(j - 2));
  return dp[j];
}
int main() {
  int n;
  cin >> n;

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

  cout << dfs(n - 1) << '\n';
}
Problem p03160 (C++)

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

int main() {
  int N;
  vector<int> h(10010);
  vector<int> dp(10010);

  int res = 0;

  cin >> N;
  rep(i, N) { cin >> h.at(i); }

  dp.at(1) = abs(h.at(1) - h.at(0));

  for (int i = 2; i < N; i++) {
    dp.at(i) = min(dp.at(i - 1) + abs(h.at(i) - h.at(i - 1)),
                   dp.at(i - 2) + abs(h.at(i) - h.at(i - 2)));
  }

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

  return 0;
}

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

int main() {
  int N;
  vector<int> h(100010);
  vector<int> dp(100010);

  int res = 0;

  cin >> N;
  rep(i, N) { cin >> h.at(i); }

  dp.at(1) = abs(h.at(1) - h.at(0));

  for (int i = 2; i < N; i++) {
    dp.at(i) = min(dp.at(i - 1) + abs(h.at(i) - h.at(i - 1)),
                   dp.at(i - 2) + abs(h.at(i) - h.at(i - 2)));
  }

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

  return 0;
}
Problem p03160 (C++)

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

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

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define uint unsigned int
#define ull unsigned long long
typedef string str;
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<bool> vb;
#define pb push_back
#define fi first
#define se second
#define in insert
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
const int INF = 1e9 + 5;
const int N = 2e5 + 123;
int main() {
  speed;
  int n;
  cin >> n;
  vi v(n);
  vi dp(n + 1, 0);
  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
  }
  dp[1] = 0;
  dp[2] = abs(v[2] - v[1]);
  for (int i = 3; i <= n; ++i) {
    dp[i] =
        min(dp[i - 2] + abs(v[i] - v[i - 2]), dp[i - 1] + abs(v[i] - v[i - 1]));
  }
  cout << dp[n];
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define uint unsigned int
#define ull unsigned long long
typedef string str;
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<bool> vb;
#define pb push_back
#define fi first
#define se second
#define in insert
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
const int INF = 1e9 + 5;
const int N = 2e5 + 123;
int main() {
  speed;
  int n;
  cin >> n;
  vi v(n + 1);
  vi dp(n + 1, 0);
  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
  }
  dp[1] = 0;
  dp[2] = abs(v[2] - v[1]);
  for (int i = 3; i <= n; ++i) {
    dp[i] =
        min(dp[i - 2] + abs(v[i] - v[i - 2]), dp[i - 1] + abs(v[i] - v[i - 1]));
  }
  cout << dp[n];
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int a[100001];
int fib(int i, int n, int arr[]) {
  if (i == n - 1) {
    return 0;
  }
  int p = INT_MAX, q = INT_MAX;
  if (i + 2 <= n - 1) {
    p = fib(i + 2, n, arr) + abs(arr[i] - arr[i + 2]);
  }
  if (i + 1 <= n - 1) {
    q = fib(i + 1, n, arr) + abs(arr[i] - arr[i + 1]);
  }
  a[i] = min(p, q);
  return a[i];
}
int main() {
  int n;
  cin >> n;
  int arr[n];
  memset(a, -1, sizeof(a));
  a[n - 1] = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  cout << fib(0, n, arr) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int a[100001];
int fib(int i, int n, int arr[]) {
  if (i == n - 1) {
    return 0;
  }
  int p = INT_MAX, q = INT_MAX;
  if (a[i] != -1) {
    return a[i];
  }
  if (i + 2 <= n - 1) {
    p = fib(i + 2, n, arr) + abs(arr[i] - arr[i + 2]);
  }
  if (i + 1 <= n - 1) {
    q = fib(i + 1, n, arr) + abs(arr[i] - arr[i + 1]);
  }
  a[i] = min(p, q);
  return a[i];
}
int main() {
  int n;
  cin >> n;
  int arr[n];
  memset(a, -1, sizeof(a));
  a[n - 1] = 0;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  cout << fib(0, n, arr) << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <set>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define int long long
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
#define mod 1000000007
#define INF 10e10
template <typename T>

void print(T x) {
  std::cout << x << " ";
}
template <typename T>

void println(T x) {
  std::cout << x << "\n";
}

int dp[10001];

signed main(void) {
  int n;
  cin >> n;
  vector<int> p(n);
  rep(i, n) { cin >> p[i]; }

  rep(i, 10001) { dp[i] = INF; }
  dp[0] = 0;
  dp[1] = abs(p[1] - p[0]);

  FOR(i, 2, n) {
    dp[i] = min((dp[i - 2] + abs(p[i - 2] - p[i])),
                (dp[i - 1] + abs(p[i - 1] - p[i])));
  }
  print(dp[n - 1]);
}

Fixed submission:
#include "bits/stdc++.h"
#include <set>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define int long long
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
#define mod 1000000007
#define INF 10e10
template <typename T>

void print(T x) {
  std::cout << x << " ";
}
template <typename T>

void println(T x) {
  std::cout << x << "\n";
}

int dp[100001];

signed main(void) {
  int n;
  cin >> n;
  vector<int> p(n);
  rep(i, n) { cin >> p[i]; }

  rep(i, 10001) { dp[i] = INF; }
  dp[0] = 0;
  dp[1] = abs(p[1] - p[0]);

  FOR(i, 2, n) {
    dp[i] = min((dp[i - 2] + abs(p[i - 2] - p[i])),
                (dp[i - 1] + abs(p[i - 1] - p[i])));
  }
  print(dp[n - 1]);
}
Problem p03160 (C++)

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


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

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

using namespace std;

const int N = 1e4 + 10;

int dp[N];

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

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

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

  cout << dp[n - 1];
}

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

using namespace std;

const int N = 1e5 + 10;

int dp[N];

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

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define ld long double
#define mx 200005
#define fr first
#define se second
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
ll dp[1005], coin[1000];
ll n;
ll func(ll i) {
  if (i >= n)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  ll ans1 = INT_MAX, ans2 = INT_MAX;
  if (i + 1 <= n)
    ans1 = abs(coin[i] - coin[i + 1]) + func(i + 1);
  if (i + 2 <= n)
    ans2 = abs(coin[i] - coin[i + 2]) + func(i + 2);
  return dp[i] = min(ans1, ans2);
}

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define ld long double
#define mx 200005
#define fr first
#define se second
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
ll dp[100005], coin[100005];
ll n;
ll func(ll i) {
  if (i >= n)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  ll ans1 = INT_MAX, ans2 = INT_MAX;
  if (i + 1 <= n)
    ans1 = abs(coin[i] - coin[i + 1]) + func(i + 1);
  if (i + 2 <= n)
    ans2 = abs(coin[i] - coin[i + 2]) + func(i + 2);
  return dp[i] = min(ans1, ans2);
}

int main() {
  fast;
  cin >> n;
  memset(dp, -1, sizeof dp);
  for (ll i = 1; i <= n; i++)
    cin >> coin[i];
  cout << func(1) << "\n";
  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>
// #include <ext/pb_ds/detail/standard_policies.hpp>

using namespace std;
// using namespace __gnu_pbds;

#define int ll
#define endl "\n"
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(c) c.begin(), c.end()
#define vi vector<int>
#define pii pair<int, int>
#define For(i, s, e) for (ll i = (s); i < (e); i++)
#define MAX 500005
#define initialize(arr, elem) memset(arr, elem, sizeof(arr))

typedef long long ll;
// typedef tree< int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;
int memo[MAX], arr[MAX];
int k = 2;
int solve(int i) {
  if (memo[i] != -1)
    return memo[i];
  if (i == 1)
    return memo[i] = 0;
  int dp = 100000000000000;
  for (int j = i - 1; j >= i - k && j > 0; j--) {
    dp = min(dp, solve(j) + abs(arr[i] - arr[j]));
  }
  return memo[i] = dp;
}
signed 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 n;
  cin >> n;
  initialize(memo, -1);
  For(i, 1, n + 1) cin >> arr[i];
  cout << solve(n) << endl;
  // For(i,1,3) cout<<memo[i]<<" ";

  return 0;
}


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

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>

using namespace std;
// using namespace __gnu_pbds;

#define int ll
#define endl "\n"
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(c) c.begin(), c.end()
#define vi vector<int>
#define pii pair<int, int>
#define For(i, s, e) for (ll i = (s); i < (e); i++)
#define MAX 500005
#define initialize(arr, elem) memset(arr, elem, sizeof(arr))

typedef long long ll;
// typedef tree< int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;
int memo[MAX], arr[MAX];
int k = 2;
int solve(int i) {
  if (memo[i] != -1)
    return memo[i];
  if (i == 1)
    return memo[i] = 0;
  int dp = 100000000000000;
  for (int j = i - 1; j >= i - k && j > 0; j--) {
    dp = min(dp, solve(j) + abs(arr[i] - arr[j]));
  }
  return memo[i] = dp;
}
signed 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 n;
  cin >> n;
  initialize(memo, -1);
  For(i, 1, n + 1) cin >> arr[i];
  cout << solve(n) << endl;
  // For(i,1,3) cout<<memo[i]<<" ";

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <memory.h>
#include <stdio.h>
int max(int a, int b) {
  if (a > b)
    return a;
  return b;
}
int min(int a, int b) {
  if (a < b)
    return a;
  return b;
}
int abs(int a) {
  if (a < 0)
    return -a;
  return a;
}
int h[10000];
int dp[10000];
int main() {
  int N;
  // freopen("input.txt", "r", stdin);
  while (scanf("%d", &N) != EOF) {
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < N; i++) {
      scanf("%d", &h[i]);
      int pre1 = max(0, i - 1);
      int pre2 = max(0, i - 2);
      dp[i] =
          min(dp[pre1] + abs(h[i] - h[pre1]), dp[pre2] + abs(h[i] - h[pre2]));
    }
    printf("%d\n", dp[N - 1]);
  }
  return 0;
}

Fixed submission:
#include <memory.h>
#include <stdio.h>
int max(int a, int b) {
  if (a > b)
    return a;
  return b;
}
int min(int a, int b) {
  if (a < b)
    return a;
  return b;
}
int abs(int a) {
  if (a < 0)
    return -a;
  return a;
}
int h[100000];
int dp[100000];
int main() {
  int N;
  // freopen("input.txt", "r", stdin);
  while (scanf("%d", &N) != EOF) {
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < N; i++) {
      scanf("%d", &h[i]);
      int pre1 = max(0, i - 1);
      int pre2 = max(0, i - 2);
      dp[i] =
          min(dp[pre1] + abs(h[i] - h[pre1]), dp[pre2] + abs(h[i] - h[pre2]));
    }
    printf("%d\n", dp[N - 1]);
  }
  return 0;
}
Problem p03160 (C++)

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

using namespace std;

const int MAXN = 1e5 + 5;
int mem[MAXN], a[MAXN];
int n;
int solve(int i) {
  if (i == n - 1)
    return 0;
  if (i >= n)
    return 1e9;

  int &ret = mem[i];
  if (ret != -1)
    return ret;
  int c1 = 1e9, c2 = 1e9;
  if (i + 1 < n)
    c1 = solve(i + 1) + abs(a[i] - a[i + 1]);
  if (i + 2 < n)
    c2 = solve(i + 2) + abs(a[i] - a[i + 2]);
  return min(c1, c2);
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  memset(mem, -1, sizeof(mem));
  cout << solve(0);
}

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

using namespace std;

const int MAXN = 1e5 + 5;
int mem[MAXN], a[MAXN];
int n;
int solve(int i) {
  if (i == n - 1)
    return 0;
  if (i >= n)
    return 1e9;

  int &ret = mem[i];
  if (ret != -1)
    return ret;
  int c1 = 1e9, c2 = 1e9;
  if (i + 1 < n)
    c1 = solve(i + 1) + abs(a[i] - a[i + 1]);
  if (i + 2 < n)
    c2 = solve(i + 2) + abs(a[i] - a[i + 2]);
  ret = min(c1, c2);
  return ret;
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long a[10005], b[1005], x, y;
  long long n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  b[n - 1] = abs(a[n] - a[n - 1]);
  b[n] = 0;
  for (int i = n - 2; i >= 1; i--) {
    x = abs(a[i] - a[i + 1]) + b[i + 1];
    y = abs(a[i] - a[i + 2]) + b[i + 2];
    if (x >= y)
      b[i] = y;
    if (x < y)
      b[i] = x;
  }
  cout << b[1];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long a[100005], b[100005], x, y;
  long long n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  b[n - 1] = abs(a[n] - a[n - 1]);
  b[n] = 0;
  for (int i = n - 2; i >= 1; i--) {
    x = abs(a[i] - a[i + 1]) + b[i + 1];
    y = abs(a[i] - a[i + 2]) + b[i + 2];
    if (x >= y)
      b[i] = y;
    if (x < y)
      b[i] = x;
  }
  cout << b[1];
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll mod = 1e9 + 7;
#define rep(i, a, b) for (ll i = a; i < b; i++)

void swap(ll &a, ll &b) {
  ll t = a;
  a = b;
  b = t;
}

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

ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}

ll pow(ll a, ll b) {
  a %= mod;
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return ans;
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  /// 1 2 3 4  ....
  ll n;
  cin >> n;
  ll a[n];
  rep(i, 0, n) { cin >> a[i]; }
  vector<ll> dp(n, INT_MAX);
  dp[0] = 0;

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll mod = 1e9 + 7;
#define rep(i, a, b) for (ll i = a; i < b; i++)

void swap(ll &a, ll &b) {
  ll t = a;
  a = b;
  b = t;
}

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

ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}

ll pow(ll a, ll b) {
  a %= mod;
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return ans;
}

int main() {

  //   #ifndef ONLINE_JUDGE
  //      freopen("input.txt", "r", stdin);
  //   #endif
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  /// 1 2 3 4  ....
  ll n;
  cin >> n;
  ll a[n];
  rep(i, 0, n) { cin >> a[i]; }
  vector<ll> dp(n, INT_MAX);
  dp[0] = 0;

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define sz(v) (v).size()
#define debug(x) cerr << #x << "_=_" << x << "\n";
#define sum(n) ((n) * ((n) + 1) >> 1)
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<pair<int, int>, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vi> vvi;

const int MAXN = 1e5 + 5;
int dp[MAXN], arr[MAXN];

int f(int i) {
  if (i == 0)
    return 0;
  if (i == 1)
    return abs(arr[1] - arr[0]);
  if (~dp[i])
    return dp[i];
  int ans = min(f(i - 1) + abs(arr[i] - arr[i - 1]),
                f(i - 2) + abs(arr[i] - arr[i - 2]));
  return dp[i] = ans;
}

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

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

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

  return 0;
}

/*
1
9 3
111100000
 */

Fixed submission:
#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define sz(v) (v).size()
#define debug(x) cerr << #x << "_=_" << x << "\n";
#define sum(n) ((n) * ((n) + 1) >> 1)
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<pair<int, int>, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vi> vvi;

const int MAXN = 1e5 + 5;
int dp[MAXN], arr[MAXN];

int f(int i) {
  if (i == 0)
    return 0;
  if (i == 1)
    return abs(arr[1] - arr[0]);
  if (~dp[i])
    return dp[i];
  int ans = min(f(i - 1) + abs(arr[i] - arr[i - 1]),
                f(i - 2) + abs(arr[i] - arr[i - 2]));
  return dp[i] = ans;
}

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

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

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

  return 0;
}

/*
1
9 3
111100000
 */
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <vector>
using namespace std;
#define read freopen("in.in", "r", stdin)
#define write freopen("out.out", "w", stdout)
#define FastIO ios::sync_with_stdio(0)
typedef long long ll;
int n;
int cost[100005];
int memo[100005];
int dp(int idx) {
  if (idx > n)
    return 9999999;
  if (idx == n - 1)
    return 0;
  int &ret = memo[idx];

  if (ret != -1)
    return ret;
  ret = min(dp(idx + 1) + abs(cost[idx + 1] - cost[idx]),
            dp(idx + 2) + abs(cost[idx] - cost[idx + 2]));
  return ret;
}
int main() {
  FastIO;
#ifndef ONLINE_JUDGE
  write;
  read;
#endif
  memset(memo, -1, sizeof memo);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> cost[i];
  cout << dp(0) << endl;

  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <vector>
using namespace std;
#define read freopen("in.in", "r", stdin)
#define write freopen("out.out", "w", stdout)
#define FastIO ios::sync_with_stdio(0)
typedef long long ll;
int n;
int cost[100005];
int memo[100005];
int dp(int idx) {
  if (idx > n)
    return 9999999;
  if (idx == n - 1)
    return 0;
  int &ret = memo[idx];

  if (ret != -1)
    return ret;
  ret = min(dp(idx + 1) + abs(cost[idx + 1] - cost[idx]),
            dp(idx + 2) + abs(cost[idx] - cost[idx + 2]));
  return ret;
}
int main() {
  FastIO;
#ifndef ONLINE_JUDGE
  // write; read;
#endif
  memset(memo, -1, sizeof memo);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> cost[i];
  cout << dp(0) << endl;

  return 0;
}
Problem p03160 (C++)

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

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

  return dp[n - 1];
}

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define n 10005
using namespace std;
ll dp[n];
ll height[n];
ll solve(int N) {
  dp[1] = 0;
  for (int i = 2; i <= N; i++) {
    ll opt1 = abs(height[i] - height[i - 1]) + dp[i - 1];
    ll opt2 = (i == 2) ? INT_MAX : abs(height[i] - height[i - 2]) + dp[i - 2];
    dp[i] = min(opt1, opt2);
  }
  return dp[N];
}
int main() {
  memset(dp, 0, sizeof dp);
  memset(height, 0, sizeof height);
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> height[i];
  }
  cout << solve(N);
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define n 100005
using namespace std;
ll dp[n];
ll height[n];
ll solve(int N) {
  dp[1] = 0;
  for (int i = 2; i <= N; i++) {
    ll opt1 = abs(height[i] - height[i - 1]) + dp[i - 1];
    ll opt2 = (i == 2) ? INT_MAX : abs(height[i] - height[i - 2]) + dp[i - 2];
    dp[i] = min(opt1, opt2);
  }
  return dp[N];
}
int main() {
  memset(dp, 0, sizeof dp);
  memset(height, 0, sizeof height);
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> height[i];
  }
  cout << solve(N);
}
Problem p03160 (C++)

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

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

const long long INF = 1LL << 60;

/* bがa以上のとき、置き換え */
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
  } else {
    return false;
  }
  return true;
}

/* bがa以下のとき、置き換え */
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
  } else {
    return false;
  }
  return true;
}

// テーブルの用意
std::vector<long long> dp(100005, INF); // dp[i] = 足場iまで飛ぶための最小コスト
std::vector<long long> h(10005);

int main() {
  int N;
  std::cin >> N;
  rep(i, N) std::cin >> h[i];

  // 見ていく
  rep(i, N) {
    if (i == 0) {
      dp[i] = 0;
      continue;
    } else if (i == 1) {
      dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]);
      continue;
    }

    // 1つ前から
    dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]);

    // 2つ前はどう?
    chmin(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
  }

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

  //	printf("===dp[]===\n");
  //	rep(i, N)
  //	{
  //		printf("dp[%d]:%lld\n", i, dp[i]);
  //	}

  return 0;
}


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

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

const long long INF = 1LL << 60;

/* bがa以上のとき、置き換え */
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
  } else {
    return false;
  }
  return true;
}

/* bがa以下のとき、置き換え */
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
  } else {
    return false;
  }
  return true;
}

// テーブルの用意
std::vector<long long> dp(100005, INF); // dp[i] = 足場iまで飛ぶための最小コスト
std::vector<long long> h(100005);

int main() {
  int N;
  std::cin >> N;
  rep(i, N) std::cin >> h[i];

  // 見ていく
  rep(i, N) {
    if (i == 0) {
      dp[i] = 0;
      continue;
    } else if (i == 1) {
      dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]);
      continue;
    }

    // 1つ前から
    dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]);

    // 2つ前はどう?
    chmin(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
  }

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

  //	printf("===dp[]===\n");
  //	rep(i, N)
  //	{
  //		printf("dp[%d]:%lld\n", i, dp[i]);
  //	}

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb;
typedef vector<ull> vul;
typedef vector<pair<ll, ll>> vpl;
typedef pair<ll, ll> pl;
#define For(i, n) for (ll i = 0; i < n; i++)
#define len(n) (ll)(n).size()
#define Sort(a) sort(a.begin(), a.end())
ll dp[110];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  vl h(n + 2);
  For(i, n) {
    cin >> h[i];
    dp[i] = 1e12;
  }
  h[n] = 0;
  h[n + 1] = 0;
  dp[0] = 0;
  For(i, n) {
    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];
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb;
typedef vector<ull> vul;
typedef vector<pair<ll, ll>> vpl;
typedef pair<ll, ll> pl;
#define For(i, n) for (ll i = 0; i < n; i++)
#define len(n) (ll)(n).size()
#define Sort(a) sort(a.begin(), a.end())
ll dp[100010];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  vl h(n + 2);
  For(i, n) {
    cin >> h[i];
    dp[i] = 1e12;
  }
  h[n] = 0;
  h[n + 1] = 0;
  dp[0] = 0;
  For(i, n) {
    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];
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int h[N], n, memo[N];
int solve(int idx) {
  if (idx == n - 1)
    return 0;
  if (memo[idx] != -1)
    return memo[idx];
  int awal, tany = 1e9 + 5;
  awal = abs(h[idx] - h[idx + 1]) + solve(idx + 1);
  if (idx + 2 <= n - 1)
    tany = abs(h[idx] - h[idx + 2]) + solve(idx + 2);
  return memo[idx] = min(awal, tany);
}
int main() {
  memset(memo, -1, sizeof(memo));
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  cout << solve(0);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int h[N], n, memo[N];
int solve(int idx) {
  if (idx == n - 1)
    return 0;
  if (memo[idx] != -1)
    return memo[idx];
  int awal, tany = 1e9 + 5;
  awal = abs(h[idx] - h[idx + 1]) + solve(idx + 1);
  if (idx + 2 <= n - 1)
    tany = abs(h[idx] - h[idx + 2]) + solve(idx + 2);
  return memo[idx] = min(awal, tany);
}
int main() {
  memset(memo, -1, sizeof(memo));
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  cout << solve(0);
  return 0;
}
Problem p03160 (C++)

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

using namespace std;

int h[10004], dp[10004];

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

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

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

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

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

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

using namespace std;

int h[100005], dp[100005];

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

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

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
vector<ll> dp(100002, -1);
ll n;
ll solve(vector<ll> &v, ll i) {
  if (i == n - 1)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  ll ans = abs(v[i] - v[i + 1]) + solve(v, i + 1);
  if (i + 2 < n) {
    ans = min(ans, abs(v[i] - v[i + 2]) + solve(v, i + 2));
  }
  dp[i] = ans;
}
int main() {

  cin >> n;
  vector<ll> v(n);
  for (ll i = 0; i < n; i++) {
    cin >> v[i];
  }
  cout << solve(v, 0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
vector<ll> dp(100002, -1);
ll n;
ll solve(vector<ll> &v, ll i) {
  if (i == n - 1)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  ll ans = abs(v[i] - v[i + 1]) + solve(v, i + 1);
  if (i + 2 < n) {
    ans = min(ans, abs(v[i] - v[i + 2]) + solve(v, i + 2));
  }
  return dp[i] = ans;
}
int main() {

  cin >> n;
  vector<ll> v(n);
  for (ll i = 0; i < n; i++) {
    cin >> v[i];
  }
  cout << solve(v, 0);
}
Problem p03160 (C++)

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

using namespace std;
int memo[10005], h[10005], n;
int dp(int i) {
  if (i > n)
    return 1e5;
  if (i == n)
    return 0;
  if (memo[i] != -1)
    return memo[i];
  int res, res1;
  res = abs(h[i] - h[i + 1]) + dp(i + 1);
  res1 = abs(h[i] - h[i + 2]) + dp(i + 2);
  memo[i] = min(res, res1);
  return memo[i];
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  h[n + 1] = h[n + 2] = 1e5;
  memset(memo, -1, sizeof(memo));
  cout << dp(1);

  return 0;
}


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

using namespace std;
int memo[100005], h[100005], n;
int dp(int i) {
  if (i > n)
    return 1e5;
  if (i == n)
    return 0;
  if (memo[i] != -1)
    return memo[i];
  int res, res1;
  res = abs(h[i] - h[i + 1]) + dp(i + 1);
  res1 = abs(h[i] - h[i + 2]) + dp(i + 2);
  memo[i] = min(res, res1);
  return memo[i];
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  h[n + 1] = h[n + 2] = 1e5;
  memset(memo, -1, sizeof(memo));
  cout << dp(1);

  return 0;
}
Problem p03160 (C++)

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long int
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 5;

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS int n;
  cin >> n;
  int arr[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  int dp[n + 1];
  memset(dp, MAXN, sizeof(dp));
  dp[1] = 0;
  // for(auto it :dp) cout<<it<<" ";
  for (int i = 1; i <= n; i++) {
    for (int j : {i + 1, i + 2}) {
      if (j <= n) {
        dp[j] = min(dp[j], dp[i] + abs(arr[i] - arr[j]));
      }
    }
  }

  cout << dp[n] << '\n';
  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 int long long int
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 5;

int32_t main() {

  IOS int n;
  cin >> n;
  int arr[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  int dp[n + 1];
  memset(dp, MAXN, sizeof(dp));
  dp[1] = 0;
  // for(auto it :dp) cout<<it<<" ";
  for (int i = 1; i <= n; i++) {
    for (int j : {i + 1, i + 2}) {
      if (j <= n) {
        dp[j] = min(dp[j], dp[i] + abs(arr[i] - arr[j]));
      }
    }
  }

  cout << dp[n] << '\n';
  return 0;
}
Problem p03160 (C++)

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

ll fun(ll a[], ll ind, ll dp[]) {
  if (ind == 0)
    return dp[0] = 0;
  if (ind == 1)
    return dp[1] = abs(a[0] - a[1]);
  ll &ans = dp[ind];
  return ans = min(abs(a[ind] - a[ind - 1]) + fun(a, ind - 1, dp),
                   abs(a[ind] - a[ind - 2]) + fun(a, ind - 2, dp));
}

int main() {
  ll n;
  cin >> n;
  ll a[n];
  ll dp[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    dp[i] = -1;
  }
  cout << fun(a, n - 1, dp);
}

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

ll fun(ll a[], ll ind, ll dp[]) {
  if (ind == 0)
    return dp[0] = 0;
  if (ind == 1)
    return dp[1] = abs(a[0] - a[1]);
  ll &ans = dp[ind];
  if (ans != -1)
    return ans;
  return ans = min(abs(a[ind] - a[ind - 1]) + fun(a, ind - 1, dp),
                   abs(a[ind] - a[ind - 2]) + fun(a, ind - 2, dp));
}

int main() {
  ll n;
  cin >> n;
  ll a[n];
  ll dp[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    dp[i] = -1;
  }
  cout << fun(a, n - 1, dp);
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll dp[100005];
ll ar[100005];
int n;
ll f(ll ind) {
  if (ind == n)
    return 0;
  if (dp[ind] > 0)
    return dp[ind];
  ll a = f(ind + 1) + abs(ar[ind] - ar[ind + 1]);
  ll b = 100000000000;
  if (ind != n - 1) {
    b = f(ind + 2) + abs(ar[ind] - ar[ind + 2]);
  }
  return dp[ind] = min(a, b);
}

int main() {
  cin >> n;
  fill(dp, dp + n, -1);
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  cout << f(1);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll dp[100005];
ll ar[100005];
int n;
ll f(ll ind) {
  if (ind == n)
    return 0;
  if (dp[ind] >= 0)
    return dp[ind];
  ll a = f(ind + 1) + abs(ar[ind] - ar[ind + 1]);
  ll b = 100000000000;
  if (ind != n - 1) {
    b = f(ind + 2) + abs(ar[ind] - ar[ind + 2]);
  }
  return dp[ind] = min(a, b);
}

int main() {
  cin >> n;
  fill(dp, dp + n, -1);
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  cout << f(1);
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 10000;
ll dp[N];
ll h[N];
const int mx = 1000000;
int main() {
  ll n;
  cin >> n;

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1000000;
ll dp[N];
ll h[N];
const int mx = 1000000;
int main() {
  ll n;
  cin >> n;

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

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

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

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}

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

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

  vector<long long> dp(
      10010, INF); // long long型にしておかないと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 >= 2)
      chmin(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
  }

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


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

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

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b; // aをbで更新
    return true;
  }
  return false;
}

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

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

  vector<long long> dp(
      100100, INF); // long long型にしておかないと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 >= 2)
      chmin(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
  }

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

Buggy submission (status: Runtime Error):
// In The Name Of Allah
// Mehrkish Makuei

#include <bits/stdc++.h>

#define pb push_back
#define debug1(x) cerr << #x << " : " << x << endl;
#define debug2(x, y)                                                           \
  cerr << #x << " : " << x << "  " << #y << " : " << y << endl;

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef vector<ll> vll;
typedef set<char> sch;
typedef set<ll> sll;

const ll MOD = 1e9 + 7;
const ll MAXN = 5e3 + 19;

ll n, a[MAXN], dp[MAXN];

int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  dp[1] = abs(a[1] - a[0]);
  for (int 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] << endl;
  return 0;
}


Fixed submission:
// In The Name Of Allah
// Mehrkish Makuei

#include <bits/stdc++.h>

#define pb push_back
#define debug1(x) cerr << #x << " : " << x << endl;
#define debug2(x, y)                                                           \
  cerr << #x << " : " << x << "  " << #y << " : " << y << endl;

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef vector<ll> vll;
typedef set<char> sch;
typedef set<ll> sll;

const ll MOD = 1e9 + 7;
const ll MAXN = 1e5 + 19;

ll n, a[MAXN], dp[MAXN];

int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  dp[1] = abs(a[1] - a[0]);
  for (int 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] << endl;
  return 0;
}
Problem p03160 (C++)

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

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


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

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

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

using namespace std;

#define MOD 1e9 + 7
#define ll long long int
#define ff first
#define ss second

template <typename T, typename G>
ostream &operator<<(ostream &out, const pair<T, G> &a) {
  return out << "( " << a.ff << ", " << a.ss << ")";
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[ ";
  for (const auto &i : a)
    out << i << ", ";
  return out << "]" << endl;
}

template <typename T> ostream &operator<<(ostream &out, const set<T> &a) {
  out << "{ ";
  for (const auto &i : a)
    out << i << ", ";
  return out << "}" << endl;
}

template <typename T, typename G>
ostream &operator<<(ostream &out, const map<T, G> &a) {
  out << "< ";
  for (const auto &i : a)
    out << i << ", ";
  return out << ">" << endl;
}

void solve() {
  ll n;
  cin >> n;
  vector<ll> h(n), c(n);

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

  for (int i = 2; i < n; i++)
    c[i] =
        min(abs(h[i] - h[i - 1]) + c[i - 1], abs(h[i] - h[i - 2]) + c[i - 2]);
  cout << c[n - 1];
}

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

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

  // your code goes here...
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

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

using namespace std;

#define MOD 1e9 + 7
#define ll long long int
#define ff first
#define ss second

template <typename T, typename G>
ostream &operator<<(ostream &out, const pair<T, G> &a) {
  return out << "( " << a.ff << ", " << a.ss << ")";
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[ ";
  for (const auto &i : a)
    out << i << ", ";
  return out << "]" << endl;
}

template <typename T> ostream &operator<<(ostream &out, const set<T> &a) {
  out << "{ ";
  for (const auto &i : a)
    out << i << ", ";
  return out << "}" << endl;
}

template <typename T, typename G>
ostream &operator<<(ostream &out, const map<T, G> &a) {
  out << "< ";
  for (const auto &i : a)
    out << i << ", ";
  return out << ">" << endl;
}

void solve() {
  ll n;
  cin >> n;
  vector<ll> h(n), c(n);

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

  for (int i = 2; i < n; i++)
    c[i] =
        min(abs(h[i] - h[i - 1]) + c[i - 1], abs(h[i] - h[i - 2]) + c[i - 2]);
  cout << c[n - 1];
}

int32_t main() {

  // your code goes here...
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}