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

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

using namespace std;

const int INF = 1 << 29;

int main() {
  int n;
  cin >> n;
  vector<int> vec;

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

  for (int i = 0; i < n; i++) {
    // cout << vec[i] << endl;
  }

  int dp[10001];
  for (int i = 0; i < 10001; i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    dp[i + 1] = dp[i + 1] < abs(vec[i + 1] - vec[i]) + dp[i]
                    ? dp[i + 1]
                    : abs(vec[i + 1] - vec[i]) + dp[i];
    dp[i + 2] = dp[i + 2] < abs(vec[i + 2] - vec[i]) + dp[i]
                    ? dp[i + 2]
                    : abs(vec[i + 2] - vec[i]) + dp[i];
  }

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

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

using namespace std;

const int INF = 1 << 29;

int main() {
  int n;
  cin >> n;
  vector<int> vec;

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

  for (int i = 0; i < n; i++) {
    // cout << vec[i] << endl;
  }

  int dp[100010];
  for (int i = 0; i < 100010; i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    dp[i + 1] = dp[i + 1] < abs(vec[i + 1] - vec[i]) + dp[i]
                    ? dp[i + 1]
                    : abs(vec[i + 1] - vec[i]) + dp[i];
    dp[i + 2] = dp[i + 2] < abs(vec[i + 2] - vec[i]) + dp[i]
                    ? dp[i + 2]
                    : abs(vec[i + 2] - vec[i]) + dp[i];
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
vector<ll> hieghts;
#define INF 1000000007
const int N = 1e7;
vector<ll> dp(N);
ll total_size;
int min_cost(int curr_position) {

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

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> total_size;
  for (ll i = 0; i < total_size; i++) {
    ll temp;
    cin >> temp;
    hieghts.pb(temp);
  }
  min_cost(0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
vector<ll> hieghts;
#define INF 1000000007
const int N = 1e7;
vector<ll> dp(N);
ll total_size;
int min_cost(int curr_position) {

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

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  // #endif
  cin >> total_size;
  for (ll i = 0; i < total_size; i++) {
    ll temp;
    cin >> temp;
    hieghts.pb(temp);
  }
  min_cost(0);
}
Problem p03160 (C++)

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

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define E 2.71828182845905
#define rep(i, a, b) for (int i = a; i < b; i++)
#define debug(x) cout << #x << " is " << x << endl;
#define PI 3.14159265
#define mem(x) memset(x, 0, sizeof(x));
using namespace std;

void sol() {
  int n;
  cin >> n;
  int h[n];
  rep(i, 0, n) cin >> h[i];
  ll dp[n];
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  rep(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;
}

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

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

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define E 2.71828182845905
#define rep(i, a, b) for (int i = a; i < b; i++)
#define debug(x) cout << #x << " is " << x << endl;
#define PI 3.14159265
#define mem(x) memset(x, 0, sizeof(x));
using namespace std;

void sol() {
  int n;
  cin >> n;
  int h[n];
  rep(i, 0, n) cin >> h[i];
  ll dp[n];
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  rep(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;
}

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

const ll MOD = 1000000007;
const ll INF = 1000000000;

using namespace std;
int main(void) {
  int n;
  ll dp[10010], h[10010];

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

  dp[1] = 0;

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

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


Fixed submission:
#include <algorithm>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

const ll MOD = 1000000007;
const ll INF = 1000000000;

using namespace std;
int main(void) {
  int n;
  int dp[100010], h[100010];

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

  dp[1] = 0;

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

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

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

#ifndef ONLINE_JUDGE
bool debug = true;
#endif

typedef long long unsigned llu;
typedef long long lld;
typedef long ld;

#define N 100005
int a[N], dp[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  if (debug)
    freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  int i, n;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  dp[1] = 0;
  dp[2] = abs(a[1] - a[2]);
  for (i = 3; 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] << endl;
  return 0;
}


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

#ifndef ONLINE_JUDGE
bool debug = true;
#endif

typedef long long unsigned llu;
typedef long long lld;
typedef long ld;

#define N 100005
int a[N], dp[N];

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

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

using namespace std;

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

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

const long long INF = 1LL << 60;
const int MAX_N = 1e5;

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

  vector<int64_t> dp(MAX_N);
  for (int i = 0; i < MAX_N; ++i)
    dp.at(i) = INF;

  vector<int> h(N + 2);
  for (int i = 0; i < N; i++) {
    cin >> h.at(i);
  }
  dp.at(0) = 0;
  for (int i = 0; i < N; i++) {
    chmin(dp.at(i + 1), dp.at(i) + abs(h.at(i) - h.at(i + 1)));
    chmin(dp.at(i + 2), dp.at(i) + abs(h.at(i) - h.at(i + 2)));
  }
  cout << dp.at(N - 1) << endl;
}

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

using namespace std;

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

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

const long long INF = 1LL << 60;
const int MAX_N = 1e6;

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

  vector<int64_t> dp(MAX_N);
  for (int i = 0; i < MAX_N; ++i)
    dp.at(i) = INF;

  vector<int> h(N + 2);
  for (int i = 0; i < N; i++) {
    cin >> h.at(i);
  }
  dp.at(0) = 0;
  for (int i = 0; i < N; i++) {
    chmin(dp.at(i + 1), dp.at(i) + abs(h.at(i) - h.at(i + 1)));
    chmin(dp.at(i + 2), dp.at(i) + abs(h.at(i) - h.at(i + 2)));
  }
  cout << dp.at(N - 1) << endl;
}
Problem p03160 (C++)

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

using namespace std;

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

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

const long long INF = 1LL << 60;

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

  vector<int64_t> dp(100000);
  for (int i = 0; i < 100000; ++i)
    dp.at(i) = INF;

  vector<int> h(N + 2);
  for (int i = 0; i < N; i++) {
    cin >> h.at(i);
  }
  dp.at(0) = 0;
  for (int i = 0; i < N; i++) {
    chmin(dp.at(i + 1), dp.at(i) + abs(h.at(i) - h.at(i + 1)));
    chmin(dp.at(i + 2), dp.at(i) + abs(h.at(i) - h.at(i + 2)));
  }
  cout << dp.at(N - 1) << endl;
}

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

using namespace std;

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

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

const long long INF = 1LL << 60;

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

  vector<int64_t> dp(100010);
  for (int i = 0; i < 100010; ++i)
    dp.at(i) = INF;

  vector<int> h(N + 2);
  for (int i = 0; i < N; i++) {
    cin >> h.at(i);
  }
  dp.at(0) = 0;
  for (int i = 0; i < N; i++) {
    chmin(dp.at(i + 1), dp.at(i) + abs(h.at(i) - h.at(i + 1)));
    chmin(dp.at(i + 2), dp.at(i) + abs(h.at(i) - h.at(i + 2)));
  }
  cout << dp.at(N - 1) << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define rep(i, x, n) for (int i = x; i < n; i++)
#define reprev(i, n, x) for (int i = n; i >= x; i--)
#define mp(a, b) make_pair(a, b)
#define mod 998244353
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#pragma GCC target("sse4.2")
using namespace std;
void grayserge() {
  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];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    grayserge();
  }
  cerr << "time taken: " << (float)clock() / CLOCKS_PER_SEC << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define rep(i, x, n) for (int i = x; i < n; i++)
#define reprev(i, n, x) for (int i = n; i >= x; i--)
#define mp(a, b) make_pair(a, b)
#define mod 998244353
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#pragma GCC target("sse4.2")
using namespace std;
void grayserge() {
  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];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  int t = 1;
  // cin>>t;
  while (t--) {
    grayserge();
  }
  cerr << "time taken: " << (float)clock() / CLOCKS_PER_SEC << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define clr cin.ignore(numeric_limits<streamsize>::max(), '\n')
#define _val(x) cerr << #x << " is " << x << "\n";
#define all(container) container.begin(), container.end()
#define rall(container) container.rbegin(), container.rend()
#define rep(i, a, b, c) for (ll i = a; i <= b; i += c)
#define rrep(i, a, b, c) for (ll i = a; i >= b; i -= c)
#define pb push_back
#define mp make_pair

using namespace std;

const double eps = 1e-10;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
const int INF = 0x3f3f3f3f;

int main(void) {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
  ll n;
  cin >> n;
  ll heights[n], dp[n];
  rep(i, 0, n - 1, 1) cin >> heights[i];
  dp[0] = 0;
  dp[1] = abs(heights[0] - heights[1]);
  rep(i, 2, n - 1, 1) dp[i] = min(dp[i - 2] + abs(heights[i - 2] - heights[i]),
                                  dp[i - 1] + abs(heights[i] - heights[i - 1]));
  cout << dp[n - 1] << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define clr cin.ignore(numeric_limits<streamsize>::max(), '\n')
#define _val(x) cerr << #x << " is " << x << "\n";
#define all(container) container.begin(), container.end()
#define rall(container) container.rbegin(), container.rend()
#define rep(i, a, b, c) for (ll i = a; i <= b; i += c)
#define rrep(i, a, b, c) for (ll i = a; i >= b; i -= c)
#define pb push_back
#define mp make_pair

using namespace std;

const double eps = 1e-10;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
const int INF = 0x3f3f3f3f;

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
  ll n;
  cin >> n;
  ll heights[n], dp[n];
  rep(i, 0, n - 1, 1) cin >> heights[i];
  dp[0] = 0;
  dp[1] = abs(heights[0] - heights[1]);
  rep(i, 2, n - 1, 1) dp[i] = min(dp[i - 2] + abs(heights[i - 2] - heights[i]),
                                  dp[i - 1] + abs(heights[i] - heights[i - 1]));
  cout << dp[n - 1] << "\n";
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
/**
 * Title: A - Frog 1
 * Url: https://atcoder.jp/contests/dp/tasks/dp_a
 */
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int MAX_N = 10000;

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

  cout << cost[n] << endl;

  return 0;
}

Fixed submission:
/**
 * Title: A - Frog 1
 * Url: https://atcoder.jp/contests/dp/tasks/dp_a
 */
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int MAX_N = 100000;

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

  cout << cost[n] << endl;

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> H(N);
  for (int i = 0; i < N; ++i) {
    cin >> H[i];
  }
  vector<int> c(N, 0);
  for (int i = 1; i < N; ++i) {
    if (i = 1) {
      c[i] = c[i - 1] + abs(H[i] - H[i - 1]);
    } else {
      c[i] =
          min(c[i - 1] + abs(H[i] - H[i - 1]), c[i - 2] + abs(H[i] - H[i - 2]));
    }
  }
  cout << c[N - 1] << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> H(N);
  for (int i = 0; i < N; ++i) {
    cin >> H[i];
  }
  vector<int> c(N, 0);
  for (int i = 1; i < N; ++i) {
    if (i == 1) {
      c[i] = c[i - 1] + abs(H[i] - H[i - 1]);
    } else {
      c[i] =
          min(c[i - 1] + abs(H[i] - H[i - 1]), c[i - 2] + abs(H[i] - H[i - 2]));
    }
  }
  cout << c[N - 1] << endl;
}
Problem p03160 (C++)

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

using namespace std;

#pragma GCC oprimize("Ofast")
#define rep(i, a, b) for (__typeof(b) i = a; i < b; ++i)

typedef long long ll;
typedef vector<ll> vl;

const ll inf = 100000000000;

ll n;
ll mem[100010];
vl l;

ll dp(ll at) {
  if (at > n - 1)
    return inf;
  if (at == n - 1)
    return 0;
  if (mem[at] != -1)
    return mem[at];
  ll ans = inf;
  ans = min(ans, abs(l[at] - l[at + 1]) + dp(at + 1));
  ans = min(ans, abs(l[at] - l[at + 2]) + dp(at + 2));
  return ans;
}

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

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

  cin >> n;
  l.resize(n);
  rep(i, 0, n) { cin >> l[i]; }
  cout << dp(0);
  return 0;
}


Fixed submission:
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#pragma GCC oprimize("Ofast")
#define rep(i, a, b) for (__typeof(b) i = a; i < b; ++i)

typedef long long ll;
typedef vector<ll> vl;

const ll inf = 100000000000;

ll n;
ll mem[100010];
vl l;

ll dp(ll at) {
  if (at > n - 1)
    return inf;
  if (at == n - 1)
    return 0;
  if (mem[at] != -1)
    return mem[at];
  ll ans = inf;
  ans = min(ans, abs(l[at] - l[at + 1]) + dp(at + 1));
  ans = min(ans, abs(l[at] - l[at + 2]) + dp(at + 2));
  return mem[at] = ans;
}

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

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

  cin >> n;
  l.resize(n);
  rep(i, 0, n) { cin >> l[i]; }
  cout << dp(0);
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

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

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio();
  int n;
  cin >> n;
  int a[n];
  for (int &x : a)
    cin >> x;
  int ans = costmin(n, a);
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

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

int main() {
  fastio();
  int n;
  cin >> n;
  int a[n];
  for (int &x : a)
    cin >> x;
  int ans = costmin(n, a);
  cout << ans << endl;

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef map<int, int> mii;
typedef pair<int, int> pii;
#define INF 1000000000
#define MOD 1000000007
#define itr ::iterator it
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define remin(a, b) (a) = min((a), (b))
#define remax(a, b) (a) = max((a), (b))
#define ll long long
#define db double
#define el "\n"
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, i;
  cin >> n;
  int a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  int ans[n];
  ans[0] = 0;
  ans[1] = abs(a[1] - a[0]);
  for (i = 2; i < n; i++) {
    ans[i] = min(ans[i - 1] + abs(a[i] - a[i - 1]),
                 ans[i - 2] + abs(a[i] - a[i - 2]));
  }
  cout << ans[n - 1];
}

Fixed submission:
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef map<int, int> mii;
typedef pair<int, int> pii;
#define INF 1000000000
#define MOD 1000000007
#define itr ::iterator it
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define remin(a, b) (a) = min((a), (b))
#define remax(a, b) (a) = max((a), (b))
#define ll long long
#define db double
#define el "\n"
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, i;
  cin >> n;
  int a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  int ans[n];
  ans[0] = 0;
  ans[1] = abs(a[1] - a[0]);
  for (i = 2; i < n; i++) {
    ans[i] = min(ans[i - 1] + abs(a[i] - a[i - 1]),
                 ans[i - 2] + abs(a[i] - a[i - 2]));
  }
  cout << ans[n - 1];
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
//*-- ILSH --*//
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define pop pop_back
#define mp make_pair
#define ve vector
#define vii vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpi vector<pii>
#define vpl vector<pll>
#define viii vector<vii>
#define vlll vector<vll>
#define fi first
#define sz size
#define len length
#define se second
// const ll mod=998244353;
const ll mod = 1000000000 + 7;
const ll N = 200000 + 6;
const ll inf = 1e17;
ll powm(ll a, ll b) {
  a = a % mod;
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return (res % mod);
}
ll modInverse(ll a, ll m) {
  ll m0 = m;
  ll y = 0, x = 1;
  if (m == 1)
    return 0;
  while (a > 1) {
    ll q = a / m;
    ll t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0)
    x += m0;
  return x;
}
void solve() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int brr[n];
  brr[0] = 0;
  brr[1] = abs(arr[0] - arr[1]);
  for (int i = 2; i < n; i++)
    brr[i] = std::min(brr[i - 1] + abs(arr[i] - arr[i - 1]),
                      brr[i - 2] + abs(arr[i] - arr[i - 2]));
  cout << brr[n - 1] << '\n';
}

int main() {
  ios::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 t = 1;
  // cin>>t;
  while (t--)
    solve();
}

Fixed submission:
//*-- ILSH --*//
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define pop pop_back
#define mp make_pair
#define ve vector
#define vii vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpi vector<pii>
#define vpl vector<pll>
#define viii vector<vii>
#define vlll vector<vll>
#define fi first
#define sz size
#define len length
#define se second
// const ll mod=998244353;
const ll mod = 1000000000 + 7;
const ll N = 200000 + 6;
const ll inf = 1e17;
ll powm(ll a, ll b) {
  a = a % mod;
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return (res % mod);
}
ll modInverse(ll a, ll m) {
  ll m0 = m;
  ll y = 0, x = 1;
  if (m == 1)
    return 0;
  while (a > 1) {
    ll q = a / m;
    ll t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0)
    x += m0;
  return x;
}
void solve() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int brr[n];
  brr[0] = 0;
  brr[1] = abs(arr[0] - arr[1]);
  for (int i = 2; i < n; i++)
    brr[i] = std::min(brr[i - 1] + abs(arr[i] - arr[i - 1]),
                      brr[i - 2] + abs(arr[i] - arr[i - 2]));
  cout << brr[n - 1] << '\n';
}

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define all(v) ((v).begin()), ((v).end())
#define rall(v) ((v).rbegin()), ((v).rend())
#define clr(v, line) memset(v, line, sizeof(v))
#define endl '\n'
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *coma = strchr(names + 1, ',');
  cerr.write(names, coma - names) << " : " << arg1 << " | ";
  __f(coma + 1, args...);
}
#else
#define trace(...)
#endif

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

void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}

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

int solve(int idx = 0) {
  if (idx == N)
    return 0;

  int &ret = dp[idx];

  if (~ret)
    return ret;

  return ret = min(
             ((idx + 1) <= N) * (solve(idx + 1) + abs(h[idx] - h[idx + 1])),
             ((idx + 2) <= N) * (solve(idx + 2) + abs(h[idx] - h[idx + 2])));
}
int main(int argc, char const *argv[]) {
  file();
  fast();

  cin >> N;
  fill(h, h + 100005, INT_MAX);
  for (int i = 0; i < N; ++i) {
    cin >> h[i];
  }

  clr(dp, -1);

  cout << solve();
}

Fixed submission:
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define all(v) ((v).begin()), ((v).end())
#define rall(v) ((v).rbegin()), ((v).rend())
#define clr(v, line) memset(v, line, sizeof(v))
#define endl '\n'
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *coma = strchr(names + 1, ',');
  cerr.write(names, coma - names) << " : " << arg1 << " | ";
  __f(coma + 1, args...);
}
#else
#define trace(...)
#endif

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

void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}

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

int solve(int idx = 0) {
  if (idx == N)
    return 0;

  int &ret = dp[idx];

  if (~ret)
    return ret;

  return ret = min(
             ((idx + 1) <= N) * (solve(idx + 1) + abs(h[idx] - h[idx + 1])),
             ((idx + 2) <= N) * (solve(idx + 2) + abs(h[idx] - h[idx + 2])));
}
int main(int argc, char const *argv[]) {
  // file();
  fast();

  cin >> N;
  fill(h, h + 100005, INT_MAX);
  for (int i = 0; i < N; ++i) {
    cin >> h[i];
  }

  clr(dp, -1);

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

Buggy submission (status: Runtime Error):
/*
   TASK: nocows
   LANG: C++14
 */

#include "bits/stdc++.h"
typedef long long ll;
typedef long double ld;

using namespace std;
#define finish(x) return cout << x, 0;

void setIn(string s) { freopen(s.c_str(), "r", stdin); }
void setOut(string s) { freopen(s.c_str(), "w", stdout); }

const string USCAO_TAG = "nocows";
void usaco() {
  setIn(USCAO_TAG + ".in");
  setOut(USCAO_TAG + ".out");
}
/*
template<typename... Args>
void print(Args... args) {
        ((std::cout << args << " "), ...);
}
*/
ll a[10001];
ll dp[10001][2];

int main() {
// usaco();
#ifndef ONLINE_JUDGE
// setIn("in.txt");
#endif

  int n;
  cin >> n;

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

  a[0] = a[1];

  dp[n][1] = abs(a[n] - a[n - 1]);
  if (n > 1)
    dp[n][0] = abs(a[n] - a[n - 2]);

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

  cout << dp[1][1];
}

Fixed submission:
/*
   TASK: nocows
   LANG: C++14
 */

#include "bits/stdc++.h"
typedef long long ll;
typedef long double ld;

using namespace std;
#define finish(x) return cout << x, 0;

void setIn(string s) { freopen(s.c_str(), "r", stdin); }
void setOut(string s) { freopen(s.c_str(), "w", stdout); }

const string USCAO_TAG = "nocows";
void usaco() {
  setIn(USCAO_TAG + ".in");
  setOut(USCAO_TAG + ".out");
}
/*
template<typename... Args>
void print(Args... args) {
        ((std::cout << args << " "), ...);
}
*/
ll a[100001];
ll dp[100001][2];

int main() {
// usaco();
#ifndef ONLINE_JUDGE
// setIn("in.txt");
#endif

  int n;
  cin >> n;

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

  a[0] = a[1];

  dp[n][1] = abs(a[n] - a[n - 1]);
  if (n > 1)
    dp[n][0] = abs(a[n] - a[n - 2]);

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000009
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define set0(a) memset((a), 0, sizeof(a))
#define set1(a) memset((a), -1, sizeof(a))
#define pi pair<int, int>
#define ps pair<string, string>
#define pl pair<long, long>
#define pll pair<long long, long long>
#define vll vector<long long>
#define vl vector<long>
#define vi vector<int>
#define vs vector<string>
#define vps vector<ps>
#define vpi vector<pi>
#define vpl vector<pl>
#define vpll vector<pll>
#define flash                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define tc(t, T) for (long long t = 0; t < T; t++)
#define rep(i, s, n, d) for (long long i = s; i < n; i = i + d)
#define meax 100007
vll a(meax, 0);
vll b(meax, meax);
ll sum1 = 0;
ll maximize(ll i, ll n, ll prev) {

  if (b[i] != meax) {
    return b[i];
  }
  if (i >= n) {
    return meax;
  }

  if (i == n - 1) {
    b[i] = 0;
    return b[i];
  }
  ll d1 = maximize(i + 1, n, a[i]);
  ll d2 = maximize(i + 2, n, a[i]);
  b[i] = min(abs(a[i] - a[i + 1]) + d1, abs(a[i + 2] - a[i]) + d2);
}
int main() {
  ll n;
  cin >> n;

  rep(i, 0, n, 1) { cin >> a[i]; }
  ll d = maximize(0, n, a[0]);
  cout << d << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000009
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define set0(a) memset((a), 0, sizeof(a))
#define set1(a) memset((a), -1, sizeof(a))
#define pi pair<int, int>
#define ps pair<string, string>
#define pl pair<long, long>
#define pll pair<long long, long long>
#define vll vector<long long>
#define vl vector<long>
#define vi vector<int>
#define vs vector<string>
#define vps vector<ps>
#define vpi vector<pi>
#define vpl vector<pl>
#define vpll vector<pll>
#define flash                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define tc(t, T) for (long long t = 0; t < T; t++)
#define rep(i, s, n, d) for (long long i = s; i < n; i = i + d)
#define meax 100007
vll a(meax, 0);
vll b(meax, meax);
ll sum1 = 0;
ll maximize(ll i, ll n, ll prev) {

  if (b[i] != meax) {
    return b[i];
  }
  if (i >= n) {
    return meax;
  }

  if (i == n - 1) {
    b[i] = 0;
    return b[i];
  }
  ll d1 = maximize(i + 1, n, a[i]);
  ll d2 = maximize(i + 2, n, a[i]);
  b[i] = min(abs(a[i] - a[i + 1]) + d1, abs(a[i + 2] - a[i]) + d2);
  return b[i];
}
int main() {
  ll n;
  cin >> n;

  rep(i, 0, n, 1) { cin >> a[i]; }
  ll d = maximize(0, n, a[0]);
  cout << d << endl;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
   TASK: nocows
   LANG: C++14
 */

#include "bits/stdc++.h"
typedef long long ll;
typedef long double ld;

using namespace std;
#define finish(x) return cout << x, 0;

void setIn(string s) { freopen(s.c_str(), "r", stdin); }
void setOut(string s) { freopen(s.c_str(), "w", stdout); }

const string USCAO_TAG = "nocows";
void usaco() {
  setIn(USCAO_TAG + ".in");
  setOut(USCAO_TAG + ".out");
}
/*
template<typename... Args>
void print(Args... args) {
        ((std::cout << args << " "), ...);
}
*/
ll a[100001];
ll dp[100001][101];

int main() {
// usaco();
#ifndef ONLINE_JUDGE
// setIn("in.txt");
#endif

  int n, k;
  cin >> n;

  k = 2;
  memset(dp, 0x3f3f3f3f, sizeof dp);
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  a[0] = a[1];

  for (int i = k; i >= 1; i--) {
    dp[n][i] = abs(a[n] - a[n - i]);
  }

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

  ll mn = 1e9;
  for (int i = 1; i <= k; i++) {
    mn = min(mn, dp[1][i]);
  }
  cout << mn;
}

Fixed submission:
/*
   TASK: nocows
   LANG: C++14
 */

#include "bits/stdc++.h"
typedef long long ll;
typedef long double ld;

using namespace std;
#define finish(x) return cout << x, 0;

void setIn(string s) { freopen(s.c_str(), "r", stdin); }
void setOut(string s) { freopen(s.c_str(), "w", stdout); }

const string USCAO_TAG = "nocows";
void usaco() {
  setIn(USCAO_TAG + ".in");
  setOut(USCAO_TAG + ".out");
}
/*
template<typename... Args>
void print(Args... args) {
        ((std::cout << args << " "), ...);
}
*/
ll a[100001];
ll dp[100001][101];

int main() {
// usaco();
#ifndef ONLINE_JUDGE
// setIn("in.txt");
#endif

  int n, k;
  cin >> n;

  k = 2;
  memset(dp, 0x3f3f3f3f, sizeof dp);
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  a[0] = a[1];

  for (int i = k; i >= 1; i--) {
    dp[n][i] = abs(a[n] - a[n - i]);
  }

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

  ll mn = 1e9;
  for (int i = 1; i <= k; i++) {
    mn = min(mn, dp[1][i]);
  }
  cout << mn;
}
Problem p03160 (C++)

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

int findMin(int h[], int n) {
  int cost[n];
  cost[0] = 0;
  cost[1] = abs(h[0] - h[1]);

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

  return cost[n - 1];
}

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

  cout << findMin(h, n) << endl;
}

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

int findMin(int h[], int n) {
  int cost[n];
  cost[0] = 0;
  cost[1] = abs(h[0] - h[1]);

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

  return cost[n - 1];
}

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

  cout << findMin(h, n) << endl;
}
Problem p03160 (C++)

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

int main() {
  int n, h[10000], dp[10000];

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

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

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

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

  return 0;
}

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

int main() {
  int n, h[100000], dp[100000];

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

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

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

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

  return 0;
}
Problem p03160 (C++)

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

#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define lli long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()

#define forn(i, n) for (int i = 0; i < n; ++i)
#define rforn(i, start) for (int i = start; i >= 0; --i)
#define forsn(i, start, end) for (int i = start; i < end; ++i)
#define rforsn(i, start, end) for (int i = start; i >= end; --i)
#define debug(x) cout << #x << " = " << x << "\n";
const int CONST = 1e9 + 7;

int main() {
  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 n;
  cin >> n;
  int ht[n];
  forn(i, n) { cin >> ht[i]; }
  int dp[n];
  dp[n - 1] = 0;

  rforsn(i, n - 2, 0) {
    dp[i] = INT_MAX;
    for (int j = 1; j <= 2 && (i + j) < n; ++j) {
      dp[i] = min(dp[i + j] + abs(ht[i + j] - ht[i]), dp[i]);
    }
  }
  cout << dp[0];
  return 0;
}


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

#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define lli long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()

#define forn(i, n) for (int i = 0; i < n; ++i)
#define rforn(i, start) for (int i = start; i >= 0; --i)
#define forsn(i, start, end) for (int i = start; i < end; ++i)
#define rforsn(i, start, end) for (int i = start; i >= end; --i)
#define debug(x) cout << #x << " = " << x << "\n";
const int CONST = 1e9 + 7;

int main() {
  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 n;
  cin >> n;
  int ht[n];
  forn(i, n) { cin >> ht[i]; }
  int dp[n];
  dp[n - 1] = 0;

  rforsn(i, n - 2, 0) {
    dp[i] = INT_MAX;
    for (int j = 1; j <= 2 && (i + j) < n; ++j) {
      dp[i] = min(dp[i + j] + abs(ht[i + j] - ht[i]), dp[i]);
    }
  }
  cout << dp[0];
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int p = 10e5 + 5;
int h[p], dp[p];
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    dp[i] = 10e5;
  }
  dp[0] = 0;
  dp[1] = 0;
  dp[2] = abs(h[1] - h[2]);

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int p = 10e5 + 5;
int h[p], dp[p];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    dp[i] = 10e5;
  }
  dp[0] = 0;
  dp[1] = 0;
  dp[2] = abs(h[1] - h[2]);

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

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
ll dp[10009];
int arr[100009];
ll sum = 0;
long long Frog1(int n) {

  if (n == 0)
    return 0;
  else if (n == 1)
    return abs(arr[n] - arr[n - 1]);
  if (dp[n] != -1)
    return dp[n];
  else {
    ll x = Frog1(n - 1);
    ll y = Frog1(n - 2);
    dp[n] = min(abs(arr[n - 1] - arr[n]) + x, abs(arr[n - 2] - arr[n]) + y);
    // cout << n << " = " << dp[n] << endl;
    return dp[n];
  }
}

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


Fixed submission:
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
ll dp[100009];
int arr[100009];
ll sum = 0;
long long Frog1(int n) {

  if (n == 0)
    return 0;
  else if (n == 1)
    return abs(arr[n] - arr[n - 1]);
  if (dp[n] != -1)
    return dp[n];
  else {
    ll x = Frog1(n - 1);
    ll y = Frog1(n - 2);
    dp[n] = min(abs(arr[n - 1] - arr[n]) + x, abs(arr[n - 2] - arr[n]) + y);
    // cout << n << " = " << dp[n] << endl;
    return dp[n];
  }
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define l(i, a, b) for (auto i = a; i < b; i++)
#define lr(i, a, b) for (auto i = a; i >= b; i--)
#define ll long long
#define lli long long int
#define ld long double
#define vec vector<int>
#define mii map<ll, ll>
#define vl vector<long long>
#define se set<long long int>
#define pi pair<long long, long long>
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define all(v) v.begin(), v.end()
#define in(x) cin >> x
#define o(x) cout << x
#define SYNC                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
int ar[200000], dp[200000], n;
int solve(int i) {
  dp[i] = min(abs(ar[i] - ar[i - 1]) + dp[i - 1],
              abs(ar[n] - ar[i - 2]) + dp[i - 2]);
  if (i == n || i == 1 || i == 2)
    return dp[n];
  solve(i + 1);
}
int main() {
  SYNC in(n);
  l(i, 1, n + 1) in(ar[i]);
  dp[1] = 0;
  dp[2] = abs(ar[1] - ar[2]);
  o(solve(3));
}


Fixed submission:
#include <bits/stdc++.h>
#define l(i, a, b) for (auto i = a; i < b; i++)
#define lr(i, a, b) for (auto i = a; i >= b; i--)
#define ll long long
#define lli long long int
#define ld long double
#define vec vector<int>
#define mii map<ll, ll>
#define vl vector<long long>
#define se set<long long int>
#define pi pair<long long, long long>
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define all(v) v.begin(), v.end()
#define in(x) cin >> x
#define o(x) cout << x
#define SYNC                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
int ar[200000], dp[200000], n;
int solve(int i) {
  dp[i] = min(abs(ar[i] - ar[i - 1]) + dp[i - 1],
              abs(ar[i] - ar[i - 2]) + dp[i - 2]);
  if (i == n || n == 1 || n == 2)
    return dp[n];
  solve(i + 1);
}
int main() {
  SYNC in(n);
  l(i, 1, n + 1) in(ar[i]);
  dp[1] = 0;
  dp[2] = abs(ar[1] - ar[2]);
  o(solve(3));
}
Problem p03160 (C++)

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

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


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

const int MAXN = 100010;
int h[MAXN], dp[MAXN];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  memset(dp, 0xff, sizeof(dp));
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    if (dp[i + 1] == -1 || dp[i + 1] > dp[i] + abs(h[i] - h[i + 1]))
      dp[i + 1] = dp[i] + abs(h[i] - h[i + 1]);
    if (dp[i + 2] == -1 || dp[i + 2] > dp[i] + abs(h[i] - h[i + 2]))
      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;

using ll = long long;
#define pb push_back
#define f first
#define s second
int dp[10000];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    dp[i] = 1e9;
  }

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

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

using ll = long long;
#define pb push_back
#define f first
#define s second
int dp[1000000];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    dp[i] = 1e9;
  }

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