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

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

const int MAX_N = 100005;
const int MAX = 2000000005;

int N;
int mem[MAX_N];
int heights[MAX_N];

int dp(int stone) {
  if (stone == N)
    return 0;
  if (stone > N)
    return MAX;

  int best = MAX;
  if (stone + 1 <= N)
    best = std::min(
        dp(stone + 1) + std::abs(heights[stone] - heights[stone + 1]), best);
  if (stone + 2 <= N)
    best = std::min(
        dp(stone + 2) + std::abs(heights[stone] - heights[stone + 2]), best);
  mem[stone] = best;
  return best;
}

int main() {
  memset(mem, -1, sizeof(mem));
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", heights + i + 1);
  }

  printf("%d\n", dp(1));
}


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

const int MAX_N = 100005;
const int MAX = 2000000005;

int N;
int mem[MAX_N];
int heights[MAX_N];

int dp(int stone) {
  if (stone == N)
    return 0;
  if (stone > N)
    return MAX;
  if (mem[stone] != -1)
    return mem[stone];

  int best = MAX;
  if (stone + 1 <= N)
    best = std::min(
        dp(stone + 1) + std::abs(heights[stone] - heights[stone + 1]), best);
  if (stone + 2 <= N)
    best = std::min(
        dp(stone + 2) + std::abs(heights[stone] - heights[stone + 2]), best);
  mem[stone] = best;
  return best;
}

int main() {
  memset(mem, -1, sizeof(mem));
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", heights + i + 1);
  }

  printf("%d\n", dp(1));
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int getCost(int *arr, int index, vector<int> mem) {
  if (mem[index] != -1)
    return mem[index];

  int cost = INT_MAX;
  if (index + 1 < mem.size())
    cost = min(cost,
               abs(arr[index] - arr[index + 1]) + getCost(arr, index + 1, mem));
  if (index + 2 < mem.size())
    cost = min(cost,
               abs(arr[index] - arr[index + 2]) + getCost(arr, index + 2, mem));

  if (cost == INT_MAX)
    cost = 0;
  mem[index] = cost;
  return cost;
}
int main() {
  int N;
  cin >> N;

  int *arr = new int[N];

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

  vector<int> mem(N, -1);

  int ans = getCost(arr, 0, mem);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int getCost(int *arr, int index, vector<int> &mem) {
  if (mem[index] != -1)
    return mem[index];

  int cost = INT_MAX;
  if (index + 1 < mem.size())
    cost = min(cost,
               abs(arr[index] - arr[index + 1]) + getCost(arr, index + 1, mem));
  if (index + 2 < mem.size())
    cost = min(cost,
               abs(arr[index] - arr[index + 2]) + getCost(arr, index + 2, mem));

  if (cost == INT_MAX)
    cost = 0;
  mem[index] = cost;
  return cost;
}
int main() {
  int N;
  cin >> N;

  int *arr = new int[N];

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

  vector<int> mem(N, -1);

  int ans = getCost(arr, 0, mem);
  cout << ans << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
/***
**   AUTHOR::ASHUTOSH MOUDGIL
***/

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

#define pb push_back
#define mp make_pair
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define F first
#define S second
// #define int long long
#define ll long long
#define all(x) x.begin(), x.end()
#define endl '\n'
#define vi vector<int>
#define vlli vector<long long>
#define pii pair<int, int>
#define mod 1000000007
#define LMAX 1e18

int h[10005];
int dp[10005];
int solve(int n, int i) {
  if (i == n)
    return 0;
  int a = INT_MIN, b = INT_MIN;
  if (i + 1 <= n)
    a = solve(n, i + 1);
  if (i + 2 <= n)
    b = solve(n, i + 2);
  return min(a + abs(h[i] - h[i - 1]), b + abs(h[i - 1] - h[i + 1]));
}

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

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

  // cout << solve(n,1);
  return 0;
}


Fixed submission:
/***
**   AUTHOR::ASHUTOSH MOUDGIL
***/

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

#define pb push_back
#define mp make_pair
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define F first
#define S second
// #define int long long
#define ll long long
#define all(x) x.begin(), x.end()
#define endl '\n'
#define vi vector<int>
#define vlli vector<long long>
#define pii pair<int, int>
#define mod 1000000007
#define LMAX 1e18

int h[100005];
int dp[100005];
int solve(int n, int i) {
  if (i == n)
    return 0;
  int a = INT_MIN, b = INT_MIN;
  if (i + 1 <= n)
    a = solve(n, i + 1);
  if (i + 2 <= n)
    b = solve(n, i + 2);
  return min(a + abs(h[i] - h[i - 1]), b + abs(h[i - 1] - h[i + 1]));
}

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

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

  // cout << solve(n,1);
  return 0;
}
Problem p03160 (C++)

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

int minCost(int n, int cost[], int dp[]) {
  if (n == 1) {
    return 0;
  }
  if (n == 2) {
    return abs(cost[1] - cost[0]);
  }
  if (dp[n] != -1) {
    return dp[n];
  }
  int cost1 = abs(cost[n - 1] - cost[n - 2]) + minCost(n - 1, cost, dp);
  int cost2 = abs(cost[n - 1] - cost[n - 3]) + minCost(n - 2, cost, dp);
  dp[n] = min(cost1, cost2);
  return dp[n];
}

int main() {
  int n;
  cin >> n;
  int cost[n];
  for (int i = 0; i < n; i++) {
    cin >> cost[i];
  }
  int dp[10000];
  for (int i = 0; i < 10000; i++) {
    dp[i] = -1;
  }
  cout << minCost(n, cost, dp);
}


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

int minCost(int n, int cost[], int dp[]) {
  if (n == 1) {
    return 0;
  }
  if (n == 2) {
    return abs(cost[1] - cost[0]);
  }
  if (dp[n] != -1) {
    return dp[n];
  }
  int cost1 = abs(cost[n - 1] - cost[n - 2]) + minCost(n - 1, cost, dp);
  int cost2 = abs(cost[n - 1] - cost[n - 3]) + minCost(n - 2, cost, dp);
  dp[n] = min(cost1, cost2);
  return dp[n];
}

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

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout.tie(0);
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  vector<int> dp(n);
  for (int i = 1; i <= n; i++)
    dp[i] = oo;
  for (int i = 0; i < n; i++)
    if (dp[i] < oo) {
      if (i + 1 < n)
        dp[i + 1] = min(dp[i + 1], dp[i] + abs(h[i] - h[i + 1]));
      if (i + 2 < n)
        dp[i + 2] = min(dp[i + 2], dp[i] + abs(h[i] - h[i + 2]));
    }
  cout << dp[n - 1];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define oo int(1e9)
using namespace std;
int n;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout.tie(0);
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  vector<int> dp(n);
  for (int i = 1; i < n; i++)
    dp[i] = oo;
  for (int i = 0; i < n; i++)
    if (dp[i] < oo) {
      if (i + 1 < n)
        dp[i + 1] = min(dp[i + 1], dp[i] + abs(h[i] - h[i + 1]));
      if (i + 2 < n)
        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):
// I'm a f*cking looser
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fasino                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define asc(A) sort(A.begin(), A.end())
#define dsc(A) sort(A.begin(), A.end(), greater<ll>())
#define input(A, N)                                                            \
  for (ll i = 0; i < N; i++)                                                   \
    cin >> A[i];
int main() {
  fasino
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
#endif
  int n;
  cin >> n;
  vector<ll> a(n);
  input(a, n);
  int dp[n];
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]); // Because At step 1 : Only i+1 jump is possible.
  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:
// I'm a f*cking looser
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fasino                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define asc(A) sort(A.begin(), A.end())
#define dsc(A) sort(A.begin(), A.end(), greater<ll>())
#define input(A, N)                                                            \
  for (ll i = 0; i < N; i++)                                                   \
    cin >> A[i];
int main() {
  fasino int n;
  cin >> n;
  vector<ll> a(n);
  input(a, n);
  int dp[n];
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]); // Because At step 1 : Only i+1 jump is possible.
  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;

#define ll long long
#define mod 1000000007
#define inf 1000000000000000000
#define fr first
#define sc second
#define pb push_back
#define pf push_front
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define per(i, a, n) for (ll i = n - 1; i >= a; i--)
#define pll pair<ll, ll>
#define vll vector<ll>
#define umap unordered_map<ll, ll>
#define lpq priority_queue<ll, vector<ll>, greater<ll>>
ll powe(ll n, ll r) {
  if (!r)
    return 1;
  else if (r % 2 == 0)
    return powe(n * n, r / 2);
  else
    return n * powe(n * n, (r - 1) / 2);
}

int main() {

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

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

  ll t = 1;
  // cin>>t;
  while (t--) {
    ll n;
    cin >> n;
    vector<ll> h(n);
    vector<ll> dp(n);
    for (ll i = 0; i < n; i++)
      cin >> h[i];
    dp[0] = 0;
    dp[1] = abs(h[1] - h[0]);
    for (ll i = 2; i < n; i++)
      dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]),
                  dp[i - 2] + abs(h[i] - h[i - 2]));
    cout << dp[n - 1] << endl;
  }

  return 0;
}

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

#define ll long long
#define mod 1000000007
#define inf 1000000000000000000
#define fr first
#define sc second
#define pb push_back
#define pf push_front
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define per(i, a, n) for (ll i = n - 1; i >= a; i--)
#define pll pair<ll, ll>
#define vll vector<ll>
#define umap unordered_map<ll, ll>
#define lpq priority_queue<ll, vector<ll>, greater<ll>>
ll powe(ll n, ll r) {
  if (!r)
    return 1;
  else if (r % 2 == 0)
    return powe(n * n, r / 2);
  else
    return n * powe(n * n, (r - 1) / 2);
}

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

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

  ll t = 1;
  // cin>>t;
  while (t--) {
    ll n;
    cin >> n;
    vector<ll> h(n);
    vector<ll> dp(n);
    for (ll i = 0; i < n; i++)
      cin >> h[i];
    dp[0] = 0;
    dp[1] = abs(h[1] - h[0]);
    for (ll i = 2; i < n; i++)
      dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]),
                  dp[i - 2] + abs(h[i] - h[i - 2]));
    cout << dp[n - 1] << endl;
  }

  return 0;
}
Problem p03160 (C++)

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

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repn(i, a, b) for (int i = a; i >= b; i--)
#define F first
#define S second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define mod 1000000007
#define mod2 998244353

int solve() {
  int n;
  cin >> n;
  int a[n], b[n];
  rep(i, 0, n) {
    cin >> a[i];
    if (i == 0)
      b[i] = 0;
    if (i == 1)
      b[i] = abs(a[i] - a[i - 1]);
    if (i > 1)
      b[i] =
          min(abs(a[i] - a[i - 1]) + b[i - 1], abs(a[i] - a[i - 2]) + b[i - 2]);
  }
  cout << b[n - 1];

  return 0;
}

signed main() {
  IOS;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output4.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
    cout << "\n";
    // cout<<" * "<<t<<"\n";
  }
  //    return 0;
}

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

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repn(i, a, b) for (int i = a; i >= b; i--)
#define F first
#define S second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define mod 1000000007
#define mod2 998244353

int solve() {
  int n;
  cin >> n;
  int a[n], b[n];
  rep(i, 0, n) {
    cin >> a[i];
    if (i == 0)
      b[i] = 0;
    if (i == 1)
      b[i] = abs(a[i] - a[i - 1]);
    if (i > 1)
      b[i] =
          min(abs(a[i] - a[i - 1]) + b[i - 1], abs(a[i] - a[i - 2]) + b[i - 2]);
  }
  cout << b[n - 1];

  return 0;
}

signed main() {
  IOS;
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
    cout << "\n";
    // cout<<" * "<<t<<"\n";
  }
  //    return 0;
}
Problem p03160 (C++)

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

int64_t min_cost(int step, vector<int> &h, vector<int64_t> &memo) {
  if (step == 1)
    return 0;
  if (step == 2)
    return abs(h.at(0) - h.at(1));

  int elem = step - 1;
  int64_t near = abs(h.at(elem) - h.at(elem - 1));
  if (memo.at(elem - 1) != -1) {
    near += memo.at(elem - 1);
  } else {
    near += min_cost(step - 1, h, memo);
  }
  int64_t far = abs(h.at(elem) - h.at(elem - 2));
  if (memo.at(elem - 2) != -1) {
    far += memo.at(elem - 2);
  } else {
    far += min_cost(step - 2, h, memo);
  }
  return min(near, far);
}

int main() {
  int N;
  cin >> N;
  vector<int64_t> memo(N, -1);
  vector<int> h(N);
  for (int i = 0; i < N; i++)
    cin >> h.at(i);

  cout << min_cost(N, h, memo) << endl;
}


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

int64_t min_cost(int step, vector<int> &h, vector<int64_t> &memo) {
  if (step == 1)
    return 0;
  if (step == 2)
    return abs(h.at(0) - h.at(1));

  int elem = step - 1;
  int64_t near = abs(h.at(elem) - h.at(elem - 1));
  if (memo.at(elem - 1) != -1) {
    near += memo.at(elem - 1);
  } else {
    near += min_cost(step - 1, h, memo);
  }
  int64_t far = abs(h.at(elem) - h.at(elem - 2));
  if (memo.at(elem - 2) != -1) {
    far += memo.at(elem - 2);
  } else {
    far += min_cost(step - 2, h, memo);
  }
  return memo.at(elem) = min(near, far);
}

int main() {
  int N;
  cin >> N;
  vector<int64_t> memo(N, -1);
  vector<int> h(N);
  for (int i = 0; i < N; i++)
    cin >> h.at(i);

  cout << min_cost(N, h, memo) << endl;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

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

using namespace std;
typedef long long int ll;
const ll INF = 1000000000;
const double PI = acos(-1);
const int mod = 1000000007;

int dp[100001] = {0};

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

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


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

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

using namespace std;
typedef long long int ll;
const ll INF = 1000000000;
const double PI = acos(-1);
const int mod = 1000000007;

int dp[100001] = {0};

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

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

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

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

int main(void) {
  int N, dp[100020], h[100020];
  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(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << dp[N - 1] << endl;
  return 0;
}
Problem p03160 (C++)

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

void solve() {
  int n;
  cin >> n;
  vector<int> vec(n), dp(n, INT_MAX);
  for (int i = 0; i < n; ++i) {
    cin >> vec[i];
  }
  dp[0] = 0, dp[1] = abs(vec[0] - vec[1]);
  for (int i = 2; i < n; ++i) {
    dp[i] = min(abs(vec[i] - vec[i - 1]) + dp[i - 1],
                abs(vec[i] - vec[i - 2]) + dp[i - 2]);
  }
  cout << dp[n - 1];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  solve();
  return 0;
}


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

void solve() {
  int n;
  cin >> n;
  vector<int> vec(n), dp(n, INT_MAX);
  for (int i = 0; i < n; ++i) {
    cin >> vec[i];
  }
  dp[0] = 0, dp[1] = abs(vec[0] - vec[1]);
  for (int i = 2; i < n; ++i) {
    dp[i] = min(abs(vec[i] - vec[i - 1]) + dp[i - 1],
                abs(vec[i] - vec[i - 2]) + dp[i - 2]);
  }
  cout << dp[n - 1];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}
Problem p03160 (C++)

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

const ll MAX = 10000000;
ll h[100010];
ll dps[100010];

// n番目のジャンプ台まで到達するための最小コストを返す
ll dp(int n) {
  if (n == 0) {
    return 0;
  }
  if (dps[n] < MAX) {
    return dps[n];
  }

  ll n1 = dp(n - 1) + abs(h[n] - h[n - 1]);
  ll n2 = MAX;
  if (n > 1) {
    n2 = dp(n - 2) + abs(h[n] - h[n - 2]);
  }

  dps[n] = min(n1, n2);
  return dps[n];
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < 100010; i++) {
    dps[i] = MAX;
  }
  cout << dp(N - 1) << endl;
}


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

ll MAX = 1LL << 60;
ll h[100010];
ll dps[100010];

// n番目のジャンプ台まで到達するための最小コストを返す
ll dp(int n) {
  if (n == 0) {
    return 0;
  }
  if (dps[n] < MAX) {
    return dps[n];
  }

  ll n1 = dp(n - 1) + abs(h[n] - h[n - 1]);
  ll n2 = MAX;
  if (n > 1) {
    n2 = dp(n - 2) + abs(h[n] - h[n - 2]);
  }

  dps[n] = min(n1, n2);
  return dps[n];
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < 100010; i++) {
    dps[i] = MAX;
  }
  cout << dp(N - 1) << endl;
}
Problem p03160 (C++)

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

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <fstream>
#include <string>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> stones(n);
  for (int i = 0; i < n; i++) {
    cin >> stones[i];
  }
  vector<int> cost(10001, 1e9 + 5);
  cost[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j : {i + 1, i + 2}) {
      if (j < n) {
        cost[j] = min(cost[j], cost[i] + abs(stones[i] - stones[j]));
      }
    }
  }
  cout << cost[n - 1];
  return 0;
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool cmp(pair<int, int> x, pair<int, int> y) {
  return (abs(x.first - x.second) > abs(y.first - y.second));
}
int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  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 - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
    // cout << dp[i] << endl;
  }
  cout << dp[n - 1] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool cmp(pair<int, int> x, pair<int, int> y) {
  return (abs(x.first - x.second) > abs(y.first - y.second));
}
int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  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 - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
    // cout << dp[i] << endl;
  }
  cout << dp[n - 1] << endl;

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
const int INF = (1 << 30);
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N;
  cin >> N;
  int h[N];
  rep(i, N) cin >> h[i];
  int dp[10010];
  rep(i, 10010) dp[i] = INF;
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);
  rep(i, N) {
    chmin(dp[i + 1], dp[i] + abs(h[i] - h[i + 1]));
    chmin(dp[i + 2], dp[i] + abs(h[i] - h[i + 2]));
  }
  cout << dp[N - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
const int INF = (1 << 30);
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N;
  cin >> N;
  int h[N];
  rep(i, N) cin >> h[i];
  int dp[100100];
  rep(i, 100100) dp[i] = INF;
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);
  rep(i, N) {
    chmin(dp[i + 1], dp[i] + abs(h[i] - h[i + 1]));
    chmin(dp[i + 2], dp[i] + abs(h[i] - h[i + 2]));
  }
  cout << dp[N - 1] << endl;
  return 0;
}
Problem p03160 (C++)

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

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

int main() {
  cin >> n;
  rep(i, n) cin >> h.at(i);
  dp.at(0) = 0;
  dp.at(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;
  return 0;
}

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

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

int main() {
  cin >> n;
  rep(i, n) cin >> h.at(i);
  dp.at(0) = 0;
  dp.at(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;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

#define MAX 1e9 + 7

int dp[100000] = {};
int h[100000] = {};
int n;
int dfs(int k) {
  if (k < n - 2)
    dp[k + 2] = min(dp[k + 2], abs(h[k] - h[k + 2]) + dp[k]);
  if (k < n - 1)
    dp[k + 1] = min(dp[k + 1], abs(h[k] - h[k + 1]) + dp[k]);
  if (k < n - 2)
    dfs(k + 2);
  if (k < n - 1)
    dfs(k + 1);
  return 0;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < n; i++) {
    dp[i] = MAX;
  }
  dp[0] = 0;
  dfs(0);
  cout << dp[n - 1] << endl;
}


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

using namespace std;

#define MAX 1e9 + 7

int dp[100000] = {};
int h[100000] = {};
int n;
int dfs(int k) {
  if (k < n - 2)
    dp[k + 2] = min(dp[k + 2], abs(h[k] - h[k + 2]) + dp[k]);
  if (k < n - 1)
    dp[k + 1] = min(dp[k + 1], abs(h[k] - h[k + 1]) + dp[k]);
  if (k < n - 1)
    dfs(k + 1);
  return 0;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < n; i++) {
    dp[i] = MAX;
  }
  dp[0] = 0;
  dfs(0);
  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 ld long double
#define sf scanf
#define pf printf
#define pb push_back
#define llu unsigned long long
#define U unsigned int
#define SIZE 1000002
#define pie 3.14159265358979323
#define minuss 1e-6
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

#define BIG 100000000

ll lcm(ll a, ll b) {
  ll GCD = __gcd(a, b);
  ll lcm = (a * b) / GCD;
  return lcm;
}
ll dp[1000];
ll weight[10000], value[10000];
ll total, val;
ll call(ll n) {
  if (n >= total)
    return 0;

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

  ll res1 = BIG, res2 = BIG;

  // if(weight[n]+c<=val)
  res1 = abs(weight[n] - weight[n + 1]) + call(n + 1);
  if (n + 2 <= total)
    res2 = abs(weight[n] - weight[n + 2]) + call(n + 2);
  // cout<<n<<" "<<res1<<" "<<res2<<endl;
  dp[n] = min(res1, res2);
  return dp[n];
}
int main() {
#ifdef forthright48
  freopen("input.txt", "r", stdin);
// freopen ( "output.txt", "w", stdout );
#endif // forthright48

  cin >> total;
  for (int i = 1; i <= total; ++i)
    cin >> weight[i];

  memset(dp, -1, sizeof(dp));
  ll ret = call(1);
  cout << ret << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define sf scanf
#define pf printf
#define pb push_back
#define llu unsigned long long
#define U unsigned int
#define SIZE 1000002
#define pie 3.14159265358979323
#define minuss 1e-6
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

#define BIG 100000000

ll lcm(ll a, ll b) {
  ll GCD = __gcd(a, b);
  ll lcm = (a * b) / GCD;
  return lcm;
}
ll dp[100005];
ll weight[1000005], value[1000005];
ll total, val;
ll call(ll n) {
  if (n >= total)
    return 0;

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

  ll res1 = BIG, res2 = BIG;

  // if(weight[n]+c<=val)
  res1 = abs(weight[n] - weight[n + 1]) + call(n + 1);
  if (n + 2 <= total)
    res2 = abs(weight[n] - weight[n + 2]) + call(n + 2);
  // cout<<n<<" "<<res1<<" "<<res2<<endl;
  dp[n] = min(res1, res2);
  return dp[n];
}
int main() {
#ifdef forthright48
  freopen("input.txt", "r", stdin);
// freopen ( "output.txt", "w", stdout );
#endif // forthright48

  cin >> total;
  for (int i = 1; i <= total; ++i)
    cin >> weight[i];

  memset(dp, -1, sizeof(dp));
  ll ret = call(1);
  cout << ret << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
#define fr(i, l, r) for (int i = l; i <= r; i++)
vector<int> dp(100001, -1);
int fun(int n, int h[]) {

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

  if (n == 0) {
    dp[0] = 0;
    return 0;
  }
  if (n == 1) {
    dp[1] = abs(h[1] - h[0]);
    return dp[1];
  }

  return min(abs(h[n] - h[n - 1]) + fun(n - 1, h),
             abs(h[n] - h[n - 2]) + fun(n - 2, h));
}
signed main() { // freopen("input.txt", "r", stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;

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

  cout << fun(n - 1, h);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
#define fr(i, l, r) for (int i = l; i <= r; i++)
vector<int> dp(100001, -1);
int fun(int n, int h[]) {

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

  if (n == 0) {
    dp[0] = 0;
    return 0;
  }
  if (n == 1) {
    dp[1] = abs(h[1] - h[0]);
    return dp[1];
  }

  dp[n] = min(abs(h[n] - h[n - 1]) + fun(n - 1, h),
              abs(h[n] - h[n - 2]) + fun(n - 2, h));
  return dp[n];
}
signed main() { // freopen("input.txt", "r", stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;

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

  cout << fun(n - 1, h);
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#define INF 2100000000
using namespace std;
int absint(int a) { return max(a, -a); }
int main() {
  int n, h[10010], i, j;
  int cost[10010];
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> h[i];
    cost[i] = INF;
  }
  cost[1] = 0;
  for (i = 1; i <= n - 2; i++) {
    cost[i + 1] = min(cost[i + 1], cost[i] + absint(h[i + 1] - h[i]));
    cost[i + 2] = min(cost[i + 2], cost[i] + absint(h[i + 2] - h[i]));
  }
  cout << min(cost[n], cost[n - 1] + absint(h[n] - h[n - 1])) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#define INF 2100000000
using namespace std;
int absint(int a) { return max(a, -a); }
int main() {
  int n, h[100010], i, j;
  int cost[100010];
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> h[i];
    cost[i] = INF;
  }
  cost[1] = 0;
  for (i = 1; i <= n - 2; i++) {
    cost[i + 1] = min(cost[i + 1], cost[i] + absint(h[i + 1] - h[i]));
    cost[i + 2] = min(cost[i + 2], cost[i] + absint(h[i + 2] - h[i]));
  }
  cout << min(cost[n], cost[n - 1] + absint(h[n] - h[n - 1])) << endl;
  return 0;
}
Problem p03160 (C++)

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

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

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

int MAX = 10010010;
ll h[100010];
ll dps[100010];

// n番目のジャンプ台まで到達するための最小コストを返す
ll dp(int n) {
  if (n == 0) {
    return 0;
  }
  if (dps[n] < MAX) {
    return dps[n];
  }

  ll n1 = dp(n - 1) + abs(h[n] - h[n - 1]);
  ll n2 = MAX;
  if (n > 1) {
    n2 = dp(n - 2) + abs(h[n] - h[n - 2]);
  }

  dps[n] = min(n1, n2);
  return dps[n];
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < 100010; i++) {
    dps[i] = MAX;
  }
  cout << dp(N - 1) << endl;
}


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

const ll MAX = 1LL << 60;
ll h[100010];
ll dps[100010];

// n番目のジャンプ台まで到達するための最小コストを返す
ll dp(int n) {
  if (n == 0) {
    return 0;
  }
  if (dps[n] < MAX) {
    return dps[n];
  }

  ll n1 = dp(n - 1) + abs(h[n] - h[n - 1]);
  ll n2 = MAX;
  if (n > 1) {
    n2 = dp(n - 2) + abs(h[n] - h[n - 2]);
  }

  dps[n] = min(n1, n2);
  return dps[n];
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  for (int i = 0; i < 100010; i++) {
    dps[i] = MAX;
  }
  cout << dp(N - 1) << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
/************************************************************
/  AUTHOR :  DEVANSHU SINGLA                                /
/  NICK :  DSINGLA                                          /
/  INSTITUTE :  IITK                                        /
************************************************************/
//                     TEMPLATE                            //

#include <bits/stdc++.h>
#define ll long long
#define N 100005
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define clr(x) x.clear()
#define F first
#define S second
#define MAX(x, y) (x) > (y) ? (x) : (y)
#define MIN(x, y) (x) < (y) ? (x) : (y)

// use cin.ignore() after cin statement.
using namespace std;

void solve() {
  long n;
  cin >> n;
  long h[n];
  for (long i = 0; i < n; i++) {
    cin >> h[i];
  }
  ll p[n];
  p[0] = 0;
  p[1] = abs(h[1] - h[0]);
  for (long i = 2; i < n; i++) {
    p[i] =
        MIN(p[i - 1] + abs(h[i] - h[i - 1]), p[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << p[n - 1];
}

int main() {
#ifndef ONLINE_JUDGE

  freopen("/home/devanshu/Desktop/cp/input.txt", "r", stdin);
  freopen("/home/devanshu/Desktop/cp/output.txt", "w", stdout);

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

  solve();
}

Fixed submission:
/************************************************************
/  AUTHOR :  DEVANSHU SINGLA                                /
/  NICK :  DSINGLA                                          /
/  INSTITUTE :  IITK                                        /
************************************************************/
//                     TEMPLATE                            //

#include <bits/stdc++.h>
#define ll long long
#define N 100005
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define clr(x) x.clear()
#define F first
#define S second
#define MAX(x, y) (x) > (y) ? (x) : (y)
#define MIN(x, y) (x) < (y) ? (x) : (y)

// use cin.ignore() after cin statement.
using namespace std;

void solve() {
  long n;
  cin >> n;
  long h[n];
  for (long i = 0; i < n; i++) {
    cin >> h[i];
  }
  ll p[n];
  p[0] = 0;
  p[1] = abs(h[1] - h[0]);
  for (long i = 2; i < n; i++) {
    p[i] =
        MIN(p[i - 1] + abs(h[i] - h[i - 1]), p[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << p[n - 1];
}

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

  solve();
}
Problem p03160 (C++)

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

ifstream fin("darb.in");
ofstream fout("darb.out");
int minCost[10001], v[10001];

int main() {
  int n;

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

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

  cout << minCost[n] << " ";

  return 0;
}

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

ifstream fin("darb.in");
ofstream fout("darb.out");
int minCost[100001], v[100001];

int main() {
  int n;

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

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

  cout << minCost[n] << " ";

  return 0;
}
Problem p03160 (C++)

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


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

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

int dp[10000] = {};

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

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

int dp[100000] = {};

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define deb(x) cout << '>' << #x << ':' << x << endl;
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
typedef long long int ll;

ll go(ll *h, ll n, ll index, ll *dp) {
  if (dp[index] != 0) {

    return dp[index];
  }
  if (n < 0) {
    return 0;
  }
  if (n == 0) {
    return 0;
  }
  if (n == 1) {
    return 0;
  }
  if (n == 2) {
    return abs(h[1] - h[0]);
  }
  ll ans1 = go(h + 1, n - 1, index + 1, dp) + abs(h[1] - h[0]);
  ll ans2 = go(h + 2, n - 2, index + 2, dp) + abs(h[2] - h[0]);
  return dp[index] = min(ans1, ans2);
}

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

  ll n;
  cin >> n;
  ll *h = new ll[n];
  ll *dp = new ll[n];
  REP(i, n) {
    cin >> h[i];
    dp[i] = 0;
  }
  int x = go(h, n, 0, dp);
  cout << x << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define deb(x) cout << '>' << #x << ':' << x << endl;
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
typedef long long int ll;

ll go(ll *h, ll n, ll index, ll *dp) {
  if (dp[index] != 0) {

    return dp[index];
  }
  if (n < 0) {
    return 0;
  }
  if (n == 0) {
    return 0;
  }
  if (n == 1) {
    return 0;
  }
  if (n == 2) {
    return abs(h[1] - h[0]);
  }
  ll ans1 = go(h + 1, n - 1, index + 1, dp) + abs(h[1] - h[0]);
  ll ans2 = go(h + 2, n - 2, index + 2, dp) + abs(h[2] - h[0]);
  return dp[index] = min(ans1, ans2);
}

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

  ll n;
  cin >> n;
  ll *h = new ll[n];
  ll *dp = new ll[n];
  REP(i, n) {
    cin >> h[i];
    dp[i] = 0;
  }
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < n; i++) {
    dp[i] =
        min(dp[i - 1] + abs(h[i - 1] - h[i]), dp[i - 2] + abs(h[i - 2] - h[i]));
  }

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

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define mod 1000000007

int dp[100001];

void c() {
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int Mincost(vi &h, int n) {
  if (n == 0)
    return 0;
  if (dp[n] != -1)
    return dp[n];

  return dp[n] =
             min(Mincost(h, n - 1) + abs(h[n] - h[n - 1]),
                 (n >= 2 ? Mincost(h, n - 2) + abs(h[n] - h[n - 2]) : INT_MAX));
}

int32_t main() {

  c();
  memset(dp, -1, sizeof(dp));
  int n;
  cin >> n;
  vi a(n);
  rep(i, 0, n) cin >> a[i];

  cout << Mincost(a, n - 1);
  return 0;
}

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define mod 1000000007

int dp[100001];

void c() {
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

int Mincost(vi &h, int n) {
  if (n == 0)
    return 0;
  if (dp[n] != -1)
    return dp[n];

  return dp[n] =
             min(Mincost(h, n - 1) + abs(h[n] - h[n - 1]),
                 (n >= 2 ? Mincost(h, n - 2) + abs(h[n] - h[n - 2]) : INT_MAX));
}

int32_t main() {

  // c();
  memset(dp, -1, sizeof(dp));
  int n;
  cin >> n;
  vi a(n);
  rep(i, 0, n) cin >> a[i];

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

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

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

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

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

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

  for (int i = 3; 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] << endl;

  return 0;
}


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

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

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

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

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

  for (int i = 3; 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] << endl;

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n;
  cin >> n;
  long long int h[10000];
  for (long long int i = 0; i < n; i++)
    cin >> h[i];
  vector<long long int> dp(n);
  dp[0] = 0;
  for (long long int i = 1; i < n; i++) {
    if (i - 2 < 0) {
      dp[i] = abs(h[i] - h[i - 1]) + dp[i - 1];
    } else
      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];
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n;
  cin >> n;
  long long int h[100000];
  for (long long int i = 0; i < n; i++)
    cin >> h[i];
  vector<long long int> dp(n);
  dp[0] = 0;
  for (long long int i = 1; i < n; i++) {
    if (i - 2 < 0) {
      dp[i] = abs(h[i] - h[i - 1]) + dp[i - 1];
    } else
      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];
}