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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fr(i, a, b) for (int i = a; i < b; i++)
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n;
  cin >> n;
  int h[n + 1];
  fr(i, 1, n + 1) cin >> h[i];
  int dp[n + 1];
  dp[1] = 0;
  fr(i, 2, n + 1) {
    int one = abs(h[i] - h[i - 1]) + dp[i - 1];
    int two = (i == 2) ? INT_MAX : abs(h[i] - h[i - 2]) + dp[i - 2];
    dp[i] = min(one, two);
  }
  cout << dp[n] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fr(i, a, b) for (int i = a; i < b; i++)
int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt", "r", stdin);
  //     freopen("output.txt", "w", stdout);
  // #endif

  int n;
  cin >> n;
  int h[n + 1];
  fr(i, 1, n + 1) cin >> h[i];
  int dp[n + 1];
  dp[1] = 0;
  fr(i, 2, n + 1) {
    int one = abs(h[i] - h[i - 1]) + dp[i - 1];
    int two = (i == 2) ? INT_MAX : abs(h[i] - h[i - 2]) + dp[i - 2];
    dp[i] = min(one, two);
  }
  cout << dp[n] << endl;
}
Problem p03160 (C++)

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

using namespace std;

const int N = 1e3 + 6, M = 1e5 + 5, OO = 0x3f3f3f3f;
int n, h[N], op1, op2, dp[N];
int frog() {
  dp[1] = 0;
  dp[2] = abs(h[2] - h[1]);

  for (int i = 3; i <= n; i++) {

    dp[i] =
        min(dp[i - 2] + abs(h[i] - h[i - 2]), dp[i - 1] + abs(h[i] - h[i - 1]));
  }

  return dp[n];
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", h + i);
  printf("%d\n", frog());
  return 0;
}


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

using namespace std;

const int N = 1e5 + 6, M = 1e5 + 5, OO = 0x3f3f3f3f;
int n, h[N], op1, op2, dp[N];
int frog() {
  dp[1] = 0;
  dp[2] = abs(h[2] - h[1]);

  for (int i = 3; i <= n; i++) {

    dp[i] =
        min(dp[i - 2] + abs(h[i] - h[i - 2]), dp[i - 1] + abs(h[i] - h[i - 1]));
  }

  return dp[n];
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", h + i);
  printf("%d\n", frog());
  return 0;
}
Problem p03160 (C++)

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

const ll mod = 1e9 + 7;
ll h[1000000]{0};
ll dp[1000000]{0};
ll n;
ll solve(ll i) {
  if (i == n)
    return 0;
  if (i > n)
    return INT_MAX;
  ll ans = INT_MAX;
  ans = solve(i + 1) + abs(h[i + 1] - h[i]);
  if (i + 2 <= n) {
    ans = min(ans, solve(i + 2) + abs(h[i + 2] - h[i]));
  }
  return dp[i] = ans;
}

int main() {

  memset(dp, -1, sizeof(dp));
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> h[i + 1];
  }
  cout << solve(1) << endl;
}


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

const ll mod = 1e9 + 7;
ll h[1000000]{0};
ll dp[1000000]{0};
ll n;
ll solve(ll i) {
  if (i == n)
    return 0;
  if (i > n)
    return INT_MAX;
  if (dp[i] != -1)
    return dp[i];
  ll ans = INT_MAX;
  ans = solve(i + 1) + abs(h[i + 1] - h[i]);
  if (i + 2 <= n) {
    ans = min(ans, solve(i + 2) + abs(h[i + 2] - h[i]));
  }
  return dp[i] = ans;
}

int main() {

  memset(dp, -1, sizeof(dp));
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> h[i + 1];
  }
  cout << solve(1) << endl;
}
Problem p03160 (C++)

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

using namespace std;

int dp[10001];
int h[10001];

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

  int n;
  cin >> n;
  if (n == 1) {
    cout << 0;
    return 0;
  }

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

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

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

  cout << dp[n - 1];

  return 0;
}


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

using namespace std;

int dp[100001];
int h[100001];

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

  int n;
  cin >> n;
  if (n == 1) {
    cout << 0;
    return 0;
  }

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

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

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

  cout << dp[n - 1];

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// Om Sree Sai Ram
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
using namespace std;
vector<int> dp;
int dfs(int i, int ar[], int n) {
  if (dp[i] != INT_MAX) {
    return dp[i];
  }
  if (i == n - 1) {
    return 0;
  }
  if (i + 1 < n) {
    int tmp = abs(ar[i + 1] - ar[i]) + dfs(i + 1, ar, n);
    dp[i] = min(dp[i], tmp);
  }

  if (i + 2 < n) {
    int tmp = abs(ar[i + 2] - ar[i]) + dfs(i + 2, ar, n);
    dp[i] = min(dp[i], tmp);
  }
  return dp[i];
}

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

  // cout<<" Hello world"<<endl;
  int n;
  cin >> n;
  int ar[n];
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
  }
  dp.assign(n, INT_MAX);
  dfs(0, ar, n);
  cout << dp[0] << endl;
  return 0;
}


Fixed submission:
// Om Sree Sai Ram
#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
using namespace std;
vector<int> dp;
int dfs(int i, int ar[], int n) {
  if (dp[i] != INT_MAX) {
    return dp[i];
  }
  if (i == n - 1) {
    return 0;
  }
  if (i + 1 < n) {
    int tmp = abs(ar[i + 1] - ar[i]) + dfs(i + 1, ar, n);
    dp[i] = min(dp[i], tmp);
  }

  if (i + 2 < n) {
    int tmp = abs(ar[i + 2] - ar[i]) + dfs(i + 2, ar, n);
    dp[i] = min(dp[i], tmp);
  }
  return dp[i];
}

int main() {
  // cout<<" Hello world"<<endl;
  int n;
  cin >> n;
  int ar[n];
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
  }
  dp.assign(n, INT_MAX);
  dfs(0, ar, n);
  cout << dp[0] << endl;
  return 0;
}
Problem p03160 (C++)

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

vector<ll> h(100000 + 100, -1);
vector<ll> dp(100000 + 100, -1);

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

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


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

vector<ll> h(100000 + 100, -1);
vector<ll> dp(100000 + 100, -1);

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

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

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

using namespace std;
int a[1005], f[1005], n;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  f[0] = 0;
  f[1] = 0;
  f[2] = abs(a[2] - a[1]);
  for (int i = 3; i <= n; i++)
    f[i] =
        min(abs(a[i] - a[i - 1]) + f[i - 1], abs(a[i] - a[i - 2]) + f[i - 2]);
  cout << f[n];
  return 0;
}


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

using namespace std;
int a[100005], f[10005], n;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  f[0] = 0;
  f[1] = 0;
  f[2] = abs(a[2] - a[1]);
  for (int i = 3; i <= n; i++)
    f[i] =
        min(abs(a[i] - a[i - 1]) + f[i - 1], abs(a[i] - a[i - 2]) + f[i - 2]);
  cout << f[n];
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
#include <stdlib.h>

#define MAX 2147483647

int h[100000];
int dp[100000];

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

int rec(int i) {
  if (dp[i] != MAX) {
    return dp[i];
  }
  if (i == 0) {
    return 0;
  }

  int res = MAX;
  chmin(&res, rec(i - 1) + abs(h[i - 1] - h[i]));
  if (i > 1) {
    chmin(&res, rec(i - 2) + abs(h[i - 2] - h[i]));
  }
  return res;
}
/*
int rec(int i){
    if(i==0){
        return 0;
    }

    int res = MAX;
    chmin(&res,rec(i-1)+abs(h[i-1]-h[i]));
    if(i>1){
        chmin(&res,rec(i-2)+abs(h[i-2]-h[i]));
    }
    return res;
}*/

int main(void) {
  // Your code here!
  int N;
  int i;

  scanf("%d", &N);
  for (i = 0; i < N; i++) {
    scanf("%d", &h[i]);
    dp[i] = MAX;
  }
  printf("%d\n", rec(N - 1));
  return 0;
}


Fixed submission:
#include <stdio.h>
#include <stdlib.h>

#define MAX 2147483647

int h[100000];
int dp[100000];

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

int rec(int i) {
  if (dp[i] != MAX) {
    return dp[i];
  }
  if (i == 0) {
    return 0;
  }

  int res = MAX;
  chmin(&res, rec(i - 1) + abs(h[i - 1] - h[i]));
  if (i > 1) {
    chmin(&res, rec(i - 2) + abs(h[i - 2] - h[i]));
  }
  return dp[i] = res;
}
/*
int rec(int i){
    if(i==0){
        return 0;
    }

    int res = MAX;
    chmin(&res,rec(i-1)+abs(h[i-1]-h[i]));
    if(i>1){
        chmin(&res,rec(i-2)+abs(h[i-2]-h[i]));
    }
    return res;
}*/

int main(void) {
  // Your code here!
  int N;
  int i;

  scanf("%d", &N);
  for (i = 0; i < N; i++) {
    scanf("%d", &h[i]);
    dp[i] = MAX;
  }
  printf("%d\n", rec(N - 1));
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;

int main() {
  int n;
  vector<int> h(10010);
  vector<long long> dp(100010);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    dp[i] = INF;
  dp[0] = 0;

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

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;

int main() {
  int n;
  vector<int> h(100010);
  vector<long long> dp(100010);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i < n; i++)
    dp[i] = INF;
  dp[0] = 0;

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define pb push_back
#define fi first`
#define se second
void solve() {
  ll n;
  cin >> n;
  std::vector<ll> v(n), ans(n);
  for (auto &i : v)
    cin >> i;
  ans[1] = abs(v[1] - v[0]);
  // ans[2]=min(ans[0]+abs(v[2]-v[1]),abs(v[2]-v[0]));
  for (int i = 2; i < n; i++)
    ans[i] = min(ans[i - 1] + abs(v[i] - v[i - 1]),
                 ans[i - 2] + abs(v[i] - v[i - 2]));
  cout << ans[n - 1];
}

int main() {

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
    // cout << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define pb push_back
#define fi first`
#define se second
void solve() {
  ll n;
  cin >> n;
  std::vector<ll> v(n), ans(n);
  for (auto &i : v)
    cin >> i;
  ans[1] = abs(v[1] - v[0]);
  // ans[2]=min(ans[0]+abs(v[2]-v[1]),abs(v[2]-v[0]));
  for (int i = 2; i < n; i++)
    ans[i] = min(ans[i - 1] + abs(v[i] - v[i - 1]),
                 ans[i - 2] + abs(v[i] - v[i - 2]));
  cout << ans[n - 1];
}

int main() {

  // #ifndef ONLINE_JUDGE
  //     // for getting input from input.txt
  //     freopen("input.txt", "r", stdin);
  //     // for writing output to output.txt
  //     freopen("output.txt", "w", stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
    // cout << endl;
  }
}
Problem p03160 (C++)

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

using namespace std;

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

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

  vector<int> dp(n);
  for (int i = 0; i < n; ++i) {
    if (i == 0)
      dp[0] = 0;
    else if (i == 1) {
      dp[1] = abs(costs[0] - costs[1]);
    } else {
      int step_1 = dp[i - 1] + abs(costs[i - 1] - costs[i]);
      int step_2 = dp[i - 2] + abs(costs[i - 2] - costs[i]);
      dp[i] = min(step_1, step_2);
    }
    // cout << dp[i] << endl;
  }
  cout << dp.back() << endl;
  return dp.back();
}


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

using namespace std;

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

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

  vector<int> dp(n);
  for (int i = 0; i < n; ++i) {
    if (i == 0)
      dp[0] = 0;
    else if (i == 1) {
      dp[1] = abs(costs[0] - costs[1]);
    } else {
      int step_1 = dp[i - 1] + abs(costs[i - 1] - costs[i]);
      int step_2 = dp[i - 2] + abs(costs[i - 2] - costs[i]);
      dp[i] = min(step_1, step_2);
    }
    // cout << dp[i] << endl;
  }
  cout << dp.back() << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define L "\n"
#define S " "
#define ll long long
#define pb push_back
#define mp make_pair
#define vi std::vector<int>
#define si std::set<int>
#define vl std::vector<long long>
#define qi std::queue<int>
#define de(a) cout << #a << "  ------>  " << a << "\n"
#define SIZE 100002
#define INF INT_MAX
#define FOR(i, n) for (i = 1; i <= n; i++)
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

using namespace std;
int dp[SIZE], N, h[SIZE];
bool flag[SIZE];

int findcost(int i) {
  int cost1, cost2;
  if (i == N)
    return 0;
  else if (i == N - 1) {
    dp[i] = abs(h[N] - h[N - 1]);
    flag[i] = 1;
    return dp[i];
  }
  if (flag[i + 1])
    cost1 = abs(h[i] - h[i + 1]) + dp[i + 1];
  else
    cost1 = abs(h[i] - h[i + 1]) + findcost(i + 1);
  if (flag[i + 2])
    cost2 = abs(h[i] - h[i + 2]) + dp[i + 2];
  else
    cost2 = abs(h[i] - h[i + 2]) + findcost(i + 2);
  return dp[i] = min(cost1, cost2);
}

void solve() {
  int i;
  cin >> N;
  FOR(i, N)
  cin >> h[i];
  int cost = findcost(1);
  cout << cost << L;
}
int main() {
  FASTIO;
  // #ifndef ONLINE_JUDGE
  //     freopen("in.txt", "r", stdin);
  // #endif
  int t;
  // cin >> t;
  // while (t--)
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define L "\n"
#define S " "
#define ll long long
#define pb push_back
#define mp make_pair
#define vi std::vector<int>
#define si std::set<int>
#define vl std::vector<long long>
#define qi std::queue<int>
#define de(a) cout << #a << "  ------>  " << a << "\n"
#define SIZE 100002
#define INF INT_MAX
#define FOR(i, n) for (i = 1; i <= n; i++)
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

using namespace std;
int dp[SIZE], N, h[SIZE];
bool flag[SIZE];

int findcost(int i) {
  int cost1, cost2;
  if (i == N)
    return 0;
  else if (i == N - 1) {
    dp[i] = abs(h[N] - h[N - 1]);
    flag[i] = 1;
    return dp[i];
  }
  if (flag[i + 1])
    cost1 = abs(h[i] - h[i + 1]) + dp[i + 1];
  else
    cost1 = abs(h[i] - h[i + 1]) + findcost(i + 1);
  if (flag[i + 2])
    cost2 = abs(h[i] - h[i + 2]) + dp[i + 2];
  else
    cost2 = abs(h[i] - h[i + 2]) + findcost(i + 2);
  flag[i] = 1;
  return dp[i] = min(cost1, cost2);
}

void solve() {
  int i;
  cin >> N;
  FOR(i, N)
  cin >> h[i];
  int cost = findcost(1);
  cout << cost << L;
}
int main() {
  FASTIO;
  // #ifndef ONLINE_JUDGE
  //     freopen("in.txt", "r", stdin);
  // #endif
  int t;
  // cin >> t;
  // while (t--)
  solve();
  return 0;
}
Problem p03160 (C++)

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

#define pb push_back
#define mp make_pair
#define ll long long int
#define mod 1000000007
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t while (t--)
#define vi vector<int>
#define vll vector<ll>
#define pi pair<int, int>
#define pll pair<ll, ll>
#define fastIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define loop(i, k, n) for (ll i = k; i < n; i += 1)

void take_fileIO() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  return;
}

void solve() {
  ll n;
  cin >> n;
  vll heights(n);

  loop(i, 0, n) cin >> heights[i];

  vll dp(n + 1, 0);

  loop(i, 1, n) {
    if (i == 1) { // because for reaching i=1 there is only one option i.e. from
                  // 0 to 1.
      dp[1] = abs(heights[1] - heights[0]);
      continue;
    }
    ll option1 =
        dp[i - 1] + abs(heights[i - 1] - heights[i]); // jump from i to i+1
    ll option2 =
        dp[i - 2] + abs(heights[i] - heights[i - 2]); // jump from i to i+2

    dp[i] = min(option1, option2);
  }

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

int main() {

  fastIO;
  take_fileIO();

  solve();

  return 0;
}

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

#define pb push_back
#define mp make_pair
#define ll long long int
#define mod 1000000007
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t while (t--)
#define vi vector<int>
#define vll vector<ll>
#define pi pair<int, int>
#define pll pair<ll, ll>
#define fastIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define loop(i, k, n) for (ll i = k; i < n; i += 1)

void take_fileIO() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  return;
}

void solve() {
  ll n;
  cin >> n;
  vll heights(n);

  loop(i, 0, n) cin >> heights[i];

  vll dp(n + 1, 0);

  loop(i, 1, n) {
    if (i == 1) { // because for reaching i=1 there is only one option i.e. from
                  // 0 to 1.
      dp[1] = abs(heights[1] - heights[0]);
      continue;
    }
    ll option1 =
        dp[i - 1] + abs(heights[i - 1] - heights[i]); // jump from i to i+1
    ll option2 =
        dp[i - 2] + abs(heights[i] - heights[i - 2]); // jump from i to i+2

    dp[i] = min(option1, option2);
  }

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

int main() {

  fastIO;
  // take_fileIO();

  solve();

  return 0;
}
Problem p03160 (C++)

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

using namespace std;

int min(int a, int b) { return (a < b) ? a : b; }

int main() {
  int n;

  cin >> n;

  vector<int> v(n);

  for (auto &i : v)
    cin >> i;

  vector<int> dp(n);

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

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

  cout << dp[n - 1];

  return 1;
}

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

using namespace std;

int min(int a, int b) { return (a < b) ? a : b; }

int main() {
  int n;

  cin >> n;

  vector<int> v(n);

  for (auto &i : v)
    cin >> i;

  vector<int> dp(n);

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))
#define abs(x) ((x) < 0 ? (-(x)) : (x))
#define rep(i, a, m) for ((i) = (a); (i) < (m); ++(i))
#define REP(i, n) for ((i) = 0; (i) < (int)(n); (i)++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.end(), x.begin()
#define sz(x) ((int)x.size())
#define PI 3.14159265

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pi;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t, T;
  cin >> T;
  vl vec(T);
  vl A(T, 0);
  for (t = 1; t <= T; ++t) {
    cin >> vec[t - 1];
  }
  A[0] = vec[0];
  A[1] = abs(vec[0] - vec[1]);
  if (T == 2)
    return A[1];
  A[0] = 0LL;
  // A[1] = 0LL;
  for (int i = 2; i < T; ++i) {
    ll x = A[i - 1] + abs(vec[i] - vec[i - 1]);
    ll y = A[i - 2] + abs(vec[i] - vec[i - 2]);
    A[i] = min(x, y);
  }
  cout << A[T - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))
#define abs(x) ((x) < 0 ? (-(x)) : (x))
#define rep(i, a, m) for ((i) = (a); (i) < (m); ++(i))
#define REP(i, n) for ((i) = 0; (i) < (int)(n); (i)++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.end(), x.begin()
#define sz(x) ((int)x.size())
#define PI 3.14159265

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pi;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t, T;
  cin >> T;
  vl vec(T);
  vl A(T, 0);
  for (t = 1; t <= T; ++t) {
    cin >> vec[t - 1];
  }
  A[0] = vec[0];
  A[1] = abs(vec[0] - vec[1]);
  if (T == 2) {
    cout << A[1] << endl;
    return 0;
  }
  A[0] = 0LL;
  // A[1] = 0LL;
  for (int i = 2; i < T; ++i) {
    ll x = A[i - 1] + abs(vec[i] - vec[i - 1]);
    ll y = A[i - 2] + abs(vec[i] - vec[i - 2]);
    A[i] = min(x, y);
  }
  cout << A[T - 1] << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int a[10010], dp[10010];
int main() {
  int n;
  scanf("%d", &n);
  scanf("%d", &a[1]);
  scanf("%d", &a[2]);
  dp[2] = max(a[1], a[2]) - min(a[1], a[2]);
  for (int i = 3; i <= n; i++) {
    ;
    scanf("%d", &a[i]);
    dp[i] = min(dp[i - 1] + (max(a[i], a[i - 1]) - min(a[i], a[i - 1])),
                dp[i - 2] + (max(a[i], a[i - 2]) - min(a[i], a[i - 2])));
  }
  printf("%d", dp[n]);
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int a[100100], dp[100100];
int main() {
  int n;
  scanf("%d", &n);
  scanf("%d", &a[1]);
  scanf("%d", &a[2]);
  dp[2] = max(a[1], a[2]) - min(a[1], a[2]);
  for (int i = 3; i <= n; i++) {
    ;
    scanf("%d", &a[i]);
    dp[i] = min(dp[i - 1] + (max(a[i], a[i - 1]) - min(a[i], a[i - 1])),
                dp[i - 2] + (max(a[i], a[i - 2]) - min(a[i], a[i - 2])));
  }
  printf("%d", dp[n]);
}
Problem p03160 (C++)

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

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

int h[10010];
int dp[100010];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &h[i]);
  }
  const int INF = 1 << 29;
  for (int i = 0; i < n; i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);
  for (int i = 2; i < n; i++) {
    chmin(dp[i], dp[i - 1] + abs(h[i] - h[i - 1]));
    chmin(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
  }
  printf("%d\n", dp[n - 1]);
}


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

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

int h[100010];
int dp[100010];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &h[i]);
  }
  const int INF = 1 << 29;
  for (int i = 0; i < n; i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);
  for (int i = 2; i < n; i++) {
    chmin(dp[i], dp[i - 1] + abs(h[i] - h[i - 1]));
    chmin(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
  }
  printf("%d\n", dp[n - 1]);
}
Problem p03160 (C++)

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

using namespace std;

int vals[10005];
int dp[10005];

int f(int n) {
  if (n == 0) {
    return 0;
  }

  if (dp[n] != -1) {
    return dp[n];
  }

  int left = INT_MAX;
  int right = INT_MAX;

  if (n - 1 >= 0) {
    left = f(n - 1) + abs(vals[n - 1] - vals[n]);
  }
  if (n - 2 >= 0) {
    right = f(n - 2) + abs(vals[n - 2] - vals[n]);
  }

  dp[n] = min(left, right);

  return dp[n];
}

int main() {
  int n;

  cin >> n;

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

  cout << f(n - 1) << endl;
}

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

using namespace std;

int vals[100005];
int dp[100005];

int f(int n) {
  if (n == 0) {
    return 0;
  }

  if (dp[n] != -1) {
    return dp[n];
  }

  int left = INT_MAX;
  int right = INT_MAX;

  if (n - 1 >= 0) {
    left = f(n - 1) + abs(vals[n - 1] - vals[n]);
  }
  if (n - 2 >= 0) {
    right = f(n - 2) + abs(vals[n - 2] - vals[n]);
  }

  dp[n] = min(left, right);

  return dp[n];
}

int main() {
  int n;

  cin >> n;

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

  cout << f(n - 1) << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007ll
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define db(x) cout << #x << " = " << x << '\n'
#define sep cout << "********************************************\n"
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL)
ll n, a[100005], ans[100005];
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  ans[0] = 0;
  ans[1] = abs(a[0] - a[1]);
  for (int i = 2; i < n; ++i) {
    ans[i] = min(abs(a[i] - a[i - 1]) + ans[i - 1],
                 abs(a[i] - a[i - 2]) + ans[i - 2]);
  }

  cout << ans[n - 1];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007ll
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define db(x) cout << #x << " = " << x << '\n'
#define sep cout << "********************************************\n"
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL)
ll n, a[100005], ans[100005];
int main(int argc, char const *argv[]) {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  //    	freopen("output.txt", "w", stdout);
  // #endif
  fast;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  ans[0] = 0;
  ans[1] = abs(a[0] - a[1]);
  for (int i = 2; i < n; ++i) {
    ans[i] = min(abs(a[i] - a[i - 1]) + ans[i - 1],
                 abs(a[i] - a[i - 2]) + ans[i - 2]);
  }

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

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

using namespace std;

int rec(int i, const vector<int> &h, vector<int> *memo) {
  if (i == 1)
    return 0;
  if (i == 2)
    return abs(h[i - 1] - h[i - 2]);

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

  int cost1 = abs(h[i - 1] - h[i - 2]);
  int cost2 = abs(h[i - 1] - h[i - 3]);

  return min(rec(i - 1, h, memo) + cost1, rec(i - 2, h, memo) + cost2);
}

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

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

  vector<int> memo(N + 1, -1);
  cout << rec(N, h, &memo) << endl;
}

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

using namespace std;

int rec(int i, const vector<int> &h, vector<int> *memo) {
  if (i == 1)
    return 0;
  if (i == 2)
    return abs(h[i - 1] - h[i - 2]);

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

  int cost1 = abs(h[i - 1] - h[i - 2]);
  int cost2 = abs(h[i - 1] - h[i - 3]);

  return (*memo)[i] =
             min(rec(i - 1, h, memo) + cost1, rec(i - 2, h, memo) + cost2);
}

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

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

  vector<int> memo(N + 1, -1);
  cout << rec(N, h, &memo) << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define maxx 100010
#define mp make_pair
typedef pair<int, int> ii;
int dp[maxx];
vector<int> peso;

int x;
int main() {
  cin >> x;
  int n;
  for (int i = 0; i < n; i++) {
    cin >> n;
    peso.push_back(n);
  }

  dp[0] = 0;
  dp[1] = dp[0] + abs(peso[0] - peso[1]);
  for (int i = 2; i < x; i++)
    dp[i] = min(dp[i - 1] + abs(peso[i - 1] - peso[i]),
                dp[i - 2] + abs(peso[i - 2] - peso[i]));

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define maxx 100010
#define mp make_pair
typedef pair<int, int> ii;
int dp[maxx];
vector<int> peso;

int x;
int main() {
  cin >> x;
  int n;
  for (int i = 0; i < x; i++) {
    cin >> n;
    peso.push_back(n);
  }

  dp[0] = 0;
  dp[1] = dp[0] + abs(peso[0] - peso[1]);
  for (int i = 2; i < x; i++)
    dp[i] = min(dp[i - 1] + abs(peso[i - 1] - peso[i]),
                dp[i - 2] + abs(peso[i - 2] - peso[i]));

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

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

using namespace std;

int main() {
  int n;
  vector<int> h;
  int elem;
  vector<int> dp(10000 + 7);

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> elem;
    h.push_back(elem);
  }

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


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

using namespace std;

int main() {
  int n;
  vector<int> h;
  int elem;
  vector<int> dp(100000 + 7);

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> elem;
    h.push_back(elem);
  }

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

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

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

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

  cout << f[n] << endl;

  return 0;
}


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

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

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

  cout << f[n] << endl;

  return 0;
}
Problem p03160 (C++)

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

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define vi vector<int>
#include <algorithm>
#include <iostream>
#include <iterator>

using namespace std;

string s;
int main() {
  int a[10000];
  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);

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

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

  cout << dp[n - 1];

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define vi vector<int>
#include <algorithm>
#include <iostream>
#include <iterator>

using namespace std;

string s;
int main() {
  int a[100000];
  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);

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

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

  cout << dp[n - 1];

  return 0;
}
Problem p03160 (C++)

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

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

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

  int n;
  cin >> n;

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

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

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

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

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

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define f(i, x, n) for (int i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define int ll
#define print(x) cerr << (#x) << "is " << x << "\n"
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 1e5 + 10;

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

int dfs(int i) {
  if (i > n)
    return 1e9;
  if (i == n)
    return 0;
  int &ans = dp[i];
  if (ans != -1)
    return ans;
  return ans = min(dfs(i + 2) + abs(h[i + 2] - h[i]),
                   dfs(i + 1) + abs(h[i + 1] - h[i]));
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> n;
  f(i, 1, n + 1) { cin >> h[i]; }
  h[n + 2] = h[n + 1] = 1e9;
  f(i, 0, n + 10) dp[i] = -1;
  cout << dfs(1) << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define f(i, x, n) for (int i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define int ll
#define print(x) cerr << (#x) << "is " << x << "\n"
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 1e5 + 10;

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

int dfs(int i) {
  if (i > n)
    return 1e9;
  if (i == n)
    return 0;
  int &ans = dp[i];
  if (ans != -1)
    return ans;
  return ans = min(dfs(i + 2) + abs(h[i + 2] - h[i]),
                   dfs(i + 1) + abs(h[i + 1] - h[i]));
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  // #ifndef ONLINE_JUDGE
  //    freopen("input.txt", "r", stdin);
  //    freopen("output.txt", "w", stdout);
  // #endif
  cin >> n;
  f(i, 1, n + 1) { cin >> h[i]; }
  h[n + 2] = h[n + 1] = 1e9;
  f(i, 0, n + 10) dp[i] = -1;
  cout << dfs(1) << '\n';
  return 0;
}
Problem p03160 (C++)

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

#define ARRAY_LENGTH (10010)

// Infinity value
const long long INF = 1LL << 60;

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

int main(void) {
  int N;
  long long H[ARRAY_LENGTH];
  long long dp[ARRAY_LENGTH];

  std::cin >> N;

  // Input costs
  for (int i = 0; i < N; i++) {
    std::cin >> H[i];
  }

  // Initialize DP
  for (int i = 0; i < ARRAY_LENGTH; i++) {
    dp[i] = INF;
  }

  // Initial condition
  dp[0] = 0;

  // Loop
  for (int i = 1; i < N; i++) {
    chmin(dp[i], dp[i - 1] + abs(H[i] - H[i - 1]));
    if (i > 1) {
      chmin(dp[i], dp[i - 2] + abs(H[i] - H[i - 2]));
    }
  }

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

  return 0;
}


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

#define ARRAY_LENGTH (100010)

// Infinity value
const long long INF = 1LL << 60;

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

int main(void) {
  int N;
  long long H[ARRAY_LENGTH];
  long long dp[ARRAY_LENGTH];

  std::cin >> N;

  // Input costs
  for (int i = 0; i < N; i++) {
    std::cin >> H[i];
  }

  // Initialize DP
  for (int i = 0; i < ARRAY_LENGTH; i++) {
    dp[i] = INF;
  }

  // Initial condition
  dp[0] = 0;

  // Loop
  for (int i = 1; i < N; i++) {
    chmin(dp[i], dp[i - 1] + abs(H[i] - H[i - 1]));
    if (i > 1) {
      chmin(dp[i], dp[i - 2] + abs(H[i] - H[i - 2]));
    }
  }

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

  return 0;
}
Problem p03160 (C++)

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

const long long INF = 1LL << 60;

int N;
int H[10010];

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

  long long dp[100010];
  for (int i = 0; i < 100010; ++i)
    dp[i] = INF;
  dp[0] = 0;

  for (int i = 1; i < N; ++i) {
    dp[i] = min(dp[i], dp[i - 1] + abs(H[i] - H[i - 1]));
    if (i >= 2)
      dp[i] = min(dp[i], dp[i - 2] + abs(H[i] - H[i - 2]));
  }

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

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

const long long INF = 1LL << 60;

int N;
int H[100010];

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

  long long dp[100010];
  for (int i = 0; i < 100010; ++i)
    dp[i] = INF;
  dp[0] = 0;

  for (int i = 1; i < N; ++i) {
    dp[i] = min(dp[i], dp[i - 1] + abs(H[i] - H[i - 1]));
    if (i >= 2)
      dp[i] = min(dp[i], dp[i - 2] + abs(H[i] - H[i - 2]));
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#if 1
#define int long long
#define MAX LLONG_MAX
#define MIN LLONG_MIN
#define stoi stoll
#else
#define MAX INT_MAX
#define MIN INT_MIN
#endif
#define INF MAX / 2
#define ALL(obj) (obj).begin(), (obj).end()
#define fi first
#define se second
const int NIL = -1;
const int MOD = 1000000007;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;

int pow(int x, int n) {
  int res = 1;
  while (n) {
    if (n & 1)
      res *= x;
    x *= x;
    n >>= 1;
  }
  return res;
}
int get_gcd(int a, int b) {
  while (1) {
    if (a < b)
      swap(a, b);
    if (b == 0)
      return a;
    a %= b;
  }
}
int get_lcm(int a, int b) { return a * b / get_gcd(a, b); }
bool chmin(int &a, int b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
bool chmax(int &a, int b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

//-------------------------テンプレここまで-------------------------//

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

  int N;
  cin >> N;
  static int h[10000];
  REP(i, N) cin >> h[i];

  // dp[i] ... 足場i番目(0-index)にいるときの最小コスト
  static int dp[100000];
  REP(i, N) dp[i] = INF;
  dp[0] = 0;

  // 配るdp
  REP(i, N - 1) {
    // 1つ飛ぶとき
    chmin(dp[i + 1], dp[i] + abs(h[i + 1] - h[i]));
    // 2つ飛ぶとき
    if (i + 2 < N)
      chmin(dp[i + 2], dp[i] + abs(h[i + 2] - h[i]));
  }

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

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#if 1
#define int long long
#define MAX LLONG_MAX
#define MIN LLONG_MIN
#define stoi stoll
#else
#define MAX INT_MAX
#define MIN INT_MIN
#endif
#define INF MAX / 2
#define ALL(obj) (obj).begin(), (obj).end()
#define fi first
#define se second
const int NIL = -1;
const int MOD = 1000000007;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;

int pow(int x, int n) {
  int res = 1;
  while (n) {
    if (n & 1)
      res *= x;
    x *= x;
    n >>= 1;
  }
  return res;
}
int get_gcd(int a, int b) {
  while (1) {
    if (a < b)
      swap(a, b);
    if (b == 0)
      return a;
    a %= b;
  }
}
int get_lcm(int a, int b) { return a * b / get_gcd(a, b); }
bool chmin(int &a, int b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
bool chmax(int &a, int b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

//-------------------------テンプレここまで-------------------------//

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

  int N;
  cin >> N;
  static int h[100000];
  REP(i, N) cin >> h[i];

  // dp[i] ... 足場i番目(0-index)にいるときの最小コスト
  static int dp[100000];
  REP(i, N) dp[i] = INF;
  dp[0] = 0;

  // 配るdp
  REP(i, N - 1) {
    // 1つ飛ぶとき
    chmin(dp[i + 1], dp[i] + abs(h[i + 1] - h[i]));
    // 2つ飛ぶとき
    if (i + 2 < N)
      chmin(dp[i + 2], dp[i] + abs(h[i + 2] - h[i]));
  }

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

  return 0;
}
Problem p03160 (C++)

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

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<PII> VP;

// rep
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(x) cerr << #x << ": " << x << '\n'
#define DEBUGP(x) cerr << #x << ": " << x.first << " \t" << x.second << '\n'

int n;
VI h(10001);

signed main() {
  cin >> n;
  REP(i, n) { cin >> h[i]; }
  h.resize(n);

  VI dp(n, 1000000001);
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);

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

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

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

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<PII> VP;

// rep
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(x) cerr << #x << ": " << x << '\n'
#define DEBUGP(x) cerr << #x << ": " << x.first << " \t" << x.second << '\n'

int n;
VI h(100010);

signed main() {
  cin >> n;
  REP(i, n) { cin >> h[i]; }
  h.resize(n);

  VI dp(n, 1000000001);
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);

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

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

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

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define ll long long
#define inf 1e18
#define pi acos(-1.0)

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

ll f(int i) {
  if (i == n - 1)
    return dp[i] = abs(a[n] - a[i]);
  if (i == n)
    return dp[i] = 0;
  if (dp[i] != -1)
    return dp[i];

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

int main() {
  fastio;

  ll i;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  memset(dp, -1, sizeof(dp));
  cout << f(1);

  return 0;
}


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

#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define ll long long
#define inf 1e18
#define pi acos(-1.0)

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

ll f(int i) {
  if (i == n - 1)
    return dp[i] = abs(a[n] - a[i]);
  if (i == n)
    return dp[i] = 0;
  if (dp[i] != -1)
    return dp[i];

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

int main() {
  fastio;

  ll i;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  memset(dp, -1, sizeof(dp));
  cout << f(1);

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int dp[1000000];
int a[1000000];
int getans(int i, int n) {
  if (i == n) {
    return 0;
  }
  if (dp[i] != -1) {
    return dp[i];
  }
  int cost = abs(a[i] - a[i + 1]) + getans(i + 1, n);
  if (i + 2 <= n) {
    cost = min(cost, abs(a[i] - a[i + 2]) + getans(i + 2, n));
    // return cost;
  }
  return cost;
  // return dp[i];
}
int main() {
  int n;
  cin >> n;
  // int a[n];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  memset(dp, -1, sizeof(dp));
  int ans = getans(1, n);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[1000000];
int a[1000000];
int getans(int i, int n) {
  if (i == n) {
    return 0;
  }
  if (dp[i] != -1) {
    return dp[i];
  }
  int cost = abs(a[i] - a[i + 1]) + getans(i + 1, n);
  if (i + 2 <= n) {
    cost = min(cost, abs(a[i] - a[i + 2]) + getans(i + 2, n));
    // return cost;
  }
  return dp[i] = cost;
  // return dp[i];
}
int main() {
  int n;
  cin >> n;
  // int a[n];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  memset(dp, -1, sizeof(dp));
  int ans = getans(1, n);
  cout << ans << endl;
}