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

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

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define SORT(c) sort((c).begin(), (c).end())
#define MOD 1000000007 // S.size() 配列名.Length()
using namespace std;
typedef unsigned long long ll;

int main(void) {
  int n;
  cin >> n;
  int h[n + 1];
  rep(i, n) cin >> h[i + 1];

  int dp[10005];

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

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


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define SORT(c) sort((c).begin(), (c).end())
#define MOD 1000000007 // S.size() 配列名.Length()
using namespace std;
typedef unsigned long long ll;

int main(void) {
  int n;
  cin >> n;
  int h[n + 1];
  rep(i, n) cin >> h[i + 1];

  int dp[100000];

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

  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] << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define INF (int)1e9
#define cBits(x) __builtin_popcount(x)
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) a / gcd(a, b) * b
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define FORr(i, a, b) for (int i = a; i >= b; i--)
#define MAX3(a, b, c) max(a, max(b, c))
#define MIN3(a, b, c) min(a, min(b, c))
#define eachTest                                                               \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)                                                                  \
    solve();

using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<ll, ll> PLL;
typedef pair<int, int> PII;

const int MOD = 1000000007;

void solve() { ; }

int main() {

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

  // eachTest

  ll n;
  cin >> n;
  vector<ll> arr(n);
  FOR(i, 0, n) { cin >> arr[i]; }
  vector<ll> table(n);
  table[0] = 0;
  table[1] = abs(arr[1] - arr[0]);
  FOR(i, 2, n) {
    table[i] = min(abs(arr[i] - arr[i - 1]) + table[i - 1],
                   abs(arr[i] - arr[i - 2]) + table[i - 2]);
  }
  cout << table[n - 1] << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define INF (int)1e9
#define cBits(x) __builtin_popcount(x)
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) a / gcd(a, b) * b
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define FORr(i, a, b) for (int i = a; i >= b; i--)
#define MAX3(a, b, c) max(a, max(b, c))
#define MIN3(a, b, c) min(a, min(b, c))
#define eachTest                                                               \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)                                                                  \
    solve();

using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<ll, ll> PLL;
typedef pair<int, int> PII;

const int MOD = 1000000007;

void solve() { ; }

int main() {

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

  // eachTest

  ll n;
  cin >> n;
  vector<ll> arr(n);
  FOR(i, 0, n) { cin >> arr[i]; }
  vector<ll> table(n);
  table[0] = 0;
  table[1] = abs(arr[1] - arr[0]);
  FOR(i, 2, n) {
    table[i] = min(abs(arr[i] - arr[i - 1]) + table[i - 1],
                   abs(arr[i] - arr[i - 2]) + table[i - 2]);
  }
  cout << table[n - 1] << "\n";

  return 0;
}
Problem p03160 (C++)

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

#define endl "\n"
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define INF (long long int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define ll long long int
#define ret return
#define NUM 100020
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}

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

int main() {
  fastio();
  input();

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

  return 0;
}

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

#define endl "\n"
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define INF (long long int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define ll long long int
#define ret return
#define NUM 100020
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}

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

int main() {
  fastio();
  // input();

  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int dp[n];
  dp[0] = 0;
  dp[1] = abs(v[1] - v[0]);
  for (int i = 2; i < n; i++) {
    int x1 = dp[i - 1] + abs(v[i] - v[i - 1]);
    int x2 = dp[i - 2] + abs(v[i] - v[i - 2]);
    dp[i] = min(x1, x2);
  }
  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 unsigned long long int ull;
#define endl "\n"
#define pb push_back
#define sq(a) (a) * (a)
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugv(v)                                                              \
  cerr << #v << " : ";                                                         \
  for (auto x : v)                                                             \
    cerr << x << ' ';                                                          \
  cerr << endl;
#define MOD 1000000007
#define PI 3.141592653589793238
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll exp(ll x, ll n, ll mod) {
  ll result = 1;
  while (n > 0) {
    if (n & 1 == 1)
      result = (result * x) % mod;
    x = (x * x) % mod;
    n = n >> 1;
  }
  return result;
}

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

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

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

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

  cout << dp[0] << endl;

  return 0;
}

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

typedef long long ll;
typedef unsigned long long int ull;
#define endl "\n"
#define pb push_back
#define sq(a) (a) * (a)
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugv(v)                                                              \
  cerr << #v << " : ";                                                         \
  for (auto x : v)                                                             \
    cerr << x << ' ';                                                          \
  cerr << endl;
#define MOD 1000000007
#define PI 3.141592653589793238
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll exp(ll x, ll n, ll mod) {
  ll result = 1;
  while (n > 0) {
    if (n & 1 == 1)
      result = (result * x) % mod;
    x = (x * x) % mod;
    n = n >> 1;
  }
  return result;
}

int32_t main() {
  IOS

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

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

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

  cout << dp[0] << endl;

  return 0;
}
Problem p03160 (C++)

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

int n;
vector<int> h(n + 1);
vector<int> cost(n + 1);

int Ans(int n) {
  if (cost[n] != -1)
    return cost[n];
  cost[n] = min((Ans(n - 1) + abs(h[n - 1] - h[n])),
                (Ans(n - 2) + abs(h[n - 2] - h[n])));
  return cost[n];
}

int main() {
  cin >> n;
  rep(i, n) {
    cin >> h[i];
    cost[i] = -1;
  }
  cost[0] = 0;
  cost[1] = abs(h[1] - h[0]);
  cout << Ans(n - 1) << endl;
}

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

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

int Ans(int n) {
  if (cost[n] != -1)
    return cost[n];
  cost[n] = min((Ans(n - 1) + abs(h[n - 1] - h[n])),
                (Ans(n - 2) + abs(h[n - 2] - h[n])));
  return cost[n];
}

int main() {
  cin >> n;
  rep(i, n) {
    cin >> h[i];
    cost[i] = -1;
  }
  cost[0] = 0;
  cost[1] = abs(h[1] - h[0]);
  cout << Ans(n - 1) << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fr first
#define sc second
#define memset(X, Y) memset(X, Y, sizeof X)
#define all(X) X.begin(), X.end()
#define sz size()
#define pb push_back
#define ve vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ll long long
#define Graph vector<vector<int>>
#define Fp cout << fixed << setprecision(9)
#define Fast                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
using namespace std;
const ll M = 1e3 + 30, N = 110, MOD = 1e9 + 7, OO = 1e7;
ll m, n, k, x, y, dp[N], bla, c = 1, cnt, ans, sum, a[N], ok = 1;
int main() {

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

  cout << dp[n];
  return 0;
}
/*
6 15= max
6 5
5 6
6 4
6 6
3 5
7 2
*/


Fixed submission:
#include <bits/stdc++.h>
#define fr first
#define sc second
#define memset(X, Y) memset(X, Y, sizeof X)
#define all(X) X.begin(), X.end()
#define sz size()
#define pb push_back
#define ve vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ll long long
#define Graph vector<vector<int>>
#define Fp cout << fixed << setprecision(9)
#define Fast                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
using namespace std;
const ll M = 1e3 + 30, N = 1e5 + 30, MOD = 1e9 + 7, OO = 1e7;
ll m, n, k, x, y, dp[N], bla, c = 1, cnt, ans, sum, a[N], ok = 1;
int main() {

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

  cout << dp[n];
  return 0;
}
/*
6 15= max
6 5
5 6
6 4
6 6
3 5
7 2
*/
Problem p03160 (C++)

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


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

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

using namespace std;
#define int long long int

const int maxn = 1e3 + 5;

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  vector<int> dp(n, 1e9);
  dp[0] = 0;
  for (int i = 1; i < n; i++) {
    if (i - 2 >= 0)
      dp[i] = min(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
    if (i - 1 >= 0)
      dp[i] = min(dp[i], 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;
#define int long long int

const int maxn = 1e3 + 5;

int32_t main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  int n;
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  vector<int> dp(n, 1e9);
  dp[0] = 0;
  for (int i = 1; i < n; i++) {
    if (i - 2 >= 0)
      dp[i] = min(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
    if (i - 1 >= 0)
      dp[i] = min(dp[i], 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;
typedef long long ll;
typedef long double ld;
#define rep(i, a, b) for (ll i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
ll ans[100000] = {};
ll fun(ll a[], ll pos, ll n) {
  if (pos >= n - 1)
    return 0;
  if (ans[pos] != -1)
    return ans[pos];
  return ans[pos] = min(abs(a[pos] - a[pos + 1]) + fun(a, pos + 1, n),
                        abs(a[pos] - a[pos + 2]) + fun(a, pos + 2, n));
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  memset(ans, -1, sizeof(ans));
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  ll n;
  cin >> n;
  ll a[n];
  rep(i, 0, n) cin >> a[i];
  cout << fun(a, 0, n);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, a, b) for (ll i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
ll ans[100000] = {};
ll fun(ll a[], ll pos, ll n) {
  if (pos >= n - 1)
    return 0;
  if (ans[pos] != -1)
    return ans[pos];
  return ans[pos] = min(abs(a[pos] - a[pos + 1]) + fun(a, pos + 1, n),
                        abs(a[pos] - a[pos + 2]) + fun(a, pos + 2, n));
}
int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  memset(ans, -1, sizeof(ans));
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  ll n;
  cin >> n;
  ll a[n];
  rep(i, 0, n) cin >> a[i];
  cout << fun(a, 0, n);
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
int n;
vector<int> v(10010, inf);
int main() {
  cin >> n;
  vector<int> h(n, 0);
  rep(i, n) cin >> h[i];
  v[0] = 0;
  rep(i, n - 1) {
    if (i + 2 < n) {
      v[i + 2] = min(v[i + 2], v[i] + abs(h[i + 2] - h[i]));
      v[i + 1] = min(v[i + 1], v[i] + abs(h[i + 1] - h[i]));
    } else if (i + 1 < n) {
      v[i + 1] = min(v[i + 1], v[i] + abs(h[i + 1] - h[i]));
    }
  }
  cout << v[n - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
int n;
vector<int> v(100100, inf);
int main() {
  cin >> n;
  vector<int> h(n, 0);
  rep(i, n) cin >> h[i];
  v[0] = 0;
  rep(i, n - 1) {
    if (i + 2 < n) {
      v[i + 2] = min(v[i + 2], v[i] + abs(h[i + 2] - h[i]));
      v[i + 1] = min(v[i + 1], v[i] + abs(h[i + 1] - h[i]));
    } else if (i + 1 < n) {
      v[i + 1] = min(v[i + 1], v[i] + abs(h[i + 1] - h[i]));
    }
  }
  cout << v[n - 1] << endl;
  return 0;
}
Problem p03160 (C++)

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

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
constexpr long long INF = 1LL << 60;

long long h[10010];
long long dp[100010];

int main() {
  int N;
  std::cin >> N;
  for (int i = 0; i < N; i++) {
    std::cin >> h[i];
  }

  for (auto &e : dp) {
    e = INF;
  }

  // i==0
  dp[0] = 0;

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

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

  return 0;
}

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

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
constexpr long long INF = 1LL << 60;

long long h[100010];
long long dp[100010];

int main() {
  int N;
  std::cin >> N;
  for (int i = 0; i < N; i++) {
    std::cin >> h[i];
  }

  for (auto &e : dp) {
    e = INF;
  }

  // i==0
  dp[0] = 0;

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

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

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long int ll;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vector<int> h(n);
  for (auto &x : h)
    cin >> x;
  vector<int> dp(n, INT_MAX);
  dp[0] = 0;
  for (int i = 0; i < n; i++)
    for (auto j : {i + 1, i + 2})
      if (j < n)
        dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
  cout << dp[n - 1];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long int ll;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> h(n);
  for (auto &x : h)
    cin >> x;
  vector<int> dp(n, INT_MAX);
  dp[0] = 0;
  for (int i = 0; i < n; i++)
    for (auto j : {i + 1, i + 2})
      if (j < n)
        dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
  cout << dp[n - 1];
  return 0;
}
Problem p03160 (C++)

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

int main() {

  int n;
  cin >> n;
  vector<int> h(n);
  vector<int> dp(1e9);
  rep(i, n) { cin >> h.at(i); }

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

  cout << dp.at(n - 1) << endl;
}


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

int main() {

  int n;
  cin >> n;
  vector<int> h(n);
  vector<int> dp(n, 1e9);
  rep(i, n) { cin >> h.at(i); }

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

  cout << dp.at(n - 1) << endl;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define pb push_back
#define F first
#define S second
#define vi vector<int>
#define pi pair<int, int>
#define PI 3.14159265
#define all(x) x.begin(), x.end()
#define er(n) cout << "error " << n << endl;
#define pbds                                                                   \
  tree<int, null_type, less_equal<int>, rb_tree_tag,                           \
       tree_order_statistics_node_update>
#define mod 1000000007

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

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

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define pb push_back
#define F first
#define S second
#define vi vector<int>
#define pi pair<int, int>
#define PI 3.14159265
#define all(x) x.begin(), x.end()
#define er(n) cout << "error " << n << endl;
#define pbds                                                                   \
  tree<int, null_type, less_equal<int>, rb_tree_tag,                           \
       tree_order_statistics_node_update>
#define mod 1000000007

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

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

Buggy submission (status: Time Limit Exceeded):
//       In the name of         ****                      God ****

/*

Copyright (C)          JadedBeast

                                   Morocco

created : 7/3/2019

*/

#include <bits/stdc++.h>

using namespace std;

int dp(int i, int *values, int *result, int n) {
  if (i >= n)
    return 0;
  if (i < n - 2)
    return result[i] = min(abs(values[i] - values[i + 1]) +
                               dp(i + 1, values, result, n),
                           min(abs(values[i] - values[i + 2]) +
                                   dp(i + 2, values, result, n),
                               result[i]));
  if (i < n - 1)
    return result[i] = min(abs(values[i] - values[i + 1]) +
                               dp(i + 1, values, result, n),
                           result[i]);
  return 0;
}

int main() {
  int n;
  cin >> n;
  int values[n], result[n];
  fill(result, result + n, INT_MAX);
  for (int i = 0; i < n; i++) {
    cin >> values[i];
  }

  cout << dp(0, values, result, n) << endl;

  return 0;
}

Fixed submission:
//       In the name of         ****                      God ****

/*

Copyright (C)          JadedBeast

                                   Morocco

created : 7/3/2019

*/

#include <bits/stdc++.h>

using namespace std;

int dp(int i, int *values, int *result, int n) {
  if (i >= n)
    return 0;
  if (result[i] < INT_MAX)
    return result[i];
  if (i < n - 2)
    return result[i] = min(abs(values[i] - values[i + 1]) +
                               dp(i + 1, values, result, n),
                           min(abs(values[i] - values[i + 2]) +
                                   dp(i + 2, values, result, n),
                               result[i]));
  if (i < n - 1)
    return result[i] = min(abs(values[i] - values[i + 1]) +
                               dp(i + 1, values, result, n),
                           result[i]);
  return 0;
}

int main() {
  int n;
  cin >> n;
  int values[n], result[n];
  fill(result, result + n, INT_MAX);
  for (int i = 0; i < n; i++) {
    cin >> values[i];
  }

  cout << dp(0, values, result, n) << endl;

  return 0;
}
Problem p03160 (C++)

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

using namespace std;
typedef long long int ll;
ll N1097 = 1000000007LL;

int h[10000];
int dp[10000];
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    scanf("%d", &h[i]);
  }
  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;
}


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

using namespace std;
typedef long long int ll;
ll N1097 = 1000000007LL;

int h[1000000];
int dp[1000000];
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    scanf("%d", &h[i]);
  }
  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;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
typedef long long ll;

ll mincost(ll n, ll h[], ll *cost) {
  if (cost[n] != 0)
    return cost[n];
  else {
    if (n <= 3)
      return cost[n] = abs(h[n] - h[1]);
    return cost[n] = min(abs(h[n] - h[n - 2]) + mincost(n - 2, h, cost),
                         abs(h[n] - h[n - 1]) + mincost(n - 1, h, cost));
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  ll i, h[n + 1];
  ll *cost = new ll[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> h[i];
    cost[i] = 0;
  }
  ll ans = mincost(n, h, cost);
  cout << ans;
  return 0;
}


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

ll mincost(ll n, ll h[], ll *cost) {
  if (cost[n] != 0)
    return cost[n];
  else {
    cost[2] = abs(h[2] - h[1]);
    cost[3] = abs(h[3] - h[1]);
    for (ll i = 4; i <= n; i++) {
      cost[i] = min(abs(h[i] - h[i - 1]) + cost[i - 1],
                    abs(h[i] - h[i - 2]) + cost[i - 2]);
    }
    return cost[n];
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  ll i, h[n + 1];
  ll *cost = new ll[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> h[i];
    cost[i] = 0;
  }
  ll ans = mincost(n, h, cost);
  cout << ans;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define endl "\n"
#define ull unsigned long long
#define pb push_back
#define all(v) ((v).begin()), ((v).end())
#define vi vector<ll>
#define INF 1000000000
#define M ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;
int main() {
  M ll n, dp[100] = {};
  vi v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    ll in;
    cin >> in;
    v.pb(in);
  }
  dp[0] = 0;
  for (int i = 1; i <= n; i++)
    dp[i] = INF;
  for (int i = 0; i < n; i++) {
    if (i + 1 < n)
      dp[i + 1] = min(dp[i + 1], dp[i] + abs(v[i] - v[i + 1]));

    if (i + 2 < n)
      dp[i + 2] = min(dp[i + 2], dp[i] + abs(v[i] - v[i + 2]));
  }
  cout << dp[n - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define endl "\n"
#define ull unsigned long long
#define pb push_back
#define all(v) ((v).begin()), ((v).end())
#define vi vector<ll>
#define INF 1000000000
#define M ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;
int main() {
  M ll n, dp[1000001] = {};
  vi v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    ll in;
    cin >> in;
    v.pb(in);
  }
  dp[0] = 0;
  for (int i = 1; i <= n; i++)
    dp[i] = INF;
  for (int i = 0; i < n; i++) {
    if (i + 1 < n)
      dp[i + 1] = min(dp[i + 1], dp[i] + abs(v[i] - v[i + 1]));

    if (i + 2 < n)
      dp[i + 2] = min(dp[i + 2], dp[i] + abs(v[i] - v[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 pair<int, int> P;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define PI 3.14159265358979323846
#define sz(x) ((int)(x).size())
#define chmin(x, y) x = min(x, y)

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
ll dp[15000];
ll h[15000];
int main() {
  int n;
  cin >> n;
  rep(i, n) { cin >> h[i]; }
  dp[1] = abs(h[1] - h[0]);
  rep(i, n) { dp[i] = LINF; }
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    chmin(dp[i], dp[i - 1] + abs(h[i] - h[i - 1]));
    if (i > 1)
      chmin(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << dp[n - 1] << endl;
}


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

typedef pair<int, int> P;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define PI 3.14159265358979323846
#define sz(x) ((int)(x).size())
#define chmin(x, y) x = min(x, y)

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
ll dp[150000];
ll h[150000];
int main() {
  int n;
  cin >> n;
  rep(i, n) { cin >> h[i]; }
  dp[1] = abs(h[1] - h[0]);
  rep(i, n) { dp[i] = LINF; }
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    chmin(dp[i], dp[i - 1] + abs(h[i] - h[i - 1]));
    if (i > 1)
      chmin(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << dp[n - 1] << endl;
}
Problem p03160 (C++)

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


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

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

using namespace std;

int main() {

  int n;
  vector<int> vec;

  cin >> n;

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

    int x;
    cin >> x;
    vec.push_back(x);
  }

  vector<int> dp;

  dp.push_back(0);
  dp.push_back(abs(vec[0] - vec[1]));

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

    int y = min(abs(vec[i - 1] - vec[i]) + dp[i - 1],
                abs(vec[i - 2] - vec[i]) + dp[i - 2]);
    dp.push_back(y);
  }

  return dp[n - 1];
}

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

using namespace std;

int main() {

  int n;
  vector<int> vec;

  cin >> n;

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

    int x;
    cin >> x;
    vec.push_back(x);
  }

  vector<int> dp;

  dp.push_back(0);
  dp.push_back(abs(vec[0] - vec[1]));

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

    int y = min(abs(vec[i - 1] - vec[i]) + dp[i - 1],
                abs(vec[i - 2] - vec[i]) + dp[i - 2]);
    dp.push_back(y);
  }

  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 pb push_back
#define pie 3.141592653589

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

  ll i, n, n2, m, t, j, l, r, pos, mpos, f = 0, k, p, n1, z, q, x, y, x1, y1,
                                         x2, y2, x3, y3, x4, y4;
  cin >> n;
  ll a[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  vector<ll> dp(n, 0);
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (i = 2; i < n; i++)
    dp[i] =
        min(dp[i - 1] + abs(a[i] - a[i - 1]), dp[i - 2] + abs(a[i] - a[i - 2]));

  cout << dp[n - 1];

  return 0;
}

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

using namespace std;

#define ll long long
#define mod 1000000007
#define pb push_back
#define pie 3.141592653589

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

  ll i, n, n2, m, t, j, l, r, pos, mpos, f = 0, k, p, n1, z, q, x, y, x1, y1,
                                         x2, y2, x3, y3, x4, y4;
  cin >> n;
  ll a[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  vector<ll> dp(n, 0);
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (i = 2; i < n; i++)
    dp[i] =
        min(dp[i - 1] + abs(a[i] - a[i - 1]), dp[i - 2] + abs(a[i] - a[i - 2]));

  cout << dp[n - 1];

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int n;
  int h[10000];
  int a[10000];
  a[0] = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];

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

    a[i + 1] = abs(h[i] - h[i + 1]) + a[i];

    if (i > 0) {
      if (a[i + 1] > abs(h[i - 1] - h[i + 1]) + a[i - 1])
        a[i + 1] = abs(h[i - 1] - h[i + 1]) + a[i - 1];
    }
  }

  cout << a[n - 1];
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n;
  int h[1000000];
  int a[1000000];
  a[0] = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];

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

    a[i + 1] = abs(h[i] - h[i + 1]) + a[i];

    if (i > 0) {
      if (a[i + 1] > abs(h[i - 1] - h[i + 1]) + a[i - 1])
        a[i + 1] = abs(h[i - 1] - h[i + 1]) + a[i - 1];
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
using namespace std;
const ll inf = 10000000000;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k, i, j;
  cin >> n;
  ll arr[n + 5];
  for (i = 0; i < n; i++)
    cin >> arr[i];
  k = 2;
  vector<ll> dp(n + 5, inf);

  // for(i=0;i<n;i++)
  //     cout<<dp[i]<<" ";
  ll zero = 0;
  dp[0] = 0;
  for (i = 1; i < n; i++) {
    for (j = i - 1; j >= max(zero, i - k); j--) {
      dp[i] = min(dp[i], dp[j] + abs(arr[i] - arr[j]));
    }
  }
  cout << dp[n - 1];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
using namespace std;
const ll inf = 10000000000;
int main() {
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt", "r", stdin);
  //     freopen("output.txt", "w", stdout);
  // #endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, k, i, j;
  cin >> n;
  ll arr[n + 5];
  for (i = 0; i < n; i++)
    cin >> arr[i];
  k = 2;
  vector<ll> dp(n + 5, inf);

  // for(i=0;i<n;i++)
  //     cout<<dp[i]<<" ";
  ll zero = 0;
  dp[0] = 0;
  for (i = 1; i < n; i++) {
    for (j = i - 1; j >= max(zero, i - k); j--) {
      dp[i] = min(dp[i], dp[j] + abs(arr[i] - arr[j]));
    }
  }
  cout << dp[n - 1];
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define all(a) a.begin(), a.end()
#define print(x) cout << x << "\n";
const ll M = ll(1e9 + 7);
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  for (int i = 0; i < v.size(); ++i) {
    os << v[i];
    if (i != v.size() - 1)
      os << ", ";
  }
  os << "]\n";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "[";
  for (auto it : v) {
    os << it;
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "]\n";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  for (auto it : v)
    os << it.first << " : " << it.second << "\n";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << "(";
  os << v.first << ", " << v.second << ")";
  return os;
}
/***********************************************************************/
int 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

  ll n;
  cin >> n;
  vector<ll> a(n);
  for (auto &x : a)
    cin >> x;
  vector<ll> dp(n);
  dp[1] = abs(a[1] - a[0]);
  for (ll 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];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define all(a) a.begin(), a.end()
#define print(x) cout << x << "\n";
const ll M = ll(1e9 + 7);
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  for (int i = 0; i < v.size(); ++i) {
    os << v[i];
    if (i != v.size() - 1)
      os << ", ";
  }
  os << "]\n";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "[";
  for (auto it : v) {
    os << it;
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "]\n";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  for (auto it : v)
    os << it.first << " : " << it.second << "\n";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << "(";
  os << v.first << ", " << v.second << ")";
  return os;
}
/***********************************************************************/
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n;
  cin >> n;
  vector<ll> a(n);
  for (auto &x : a)
    cin >> x;
  vector<ll> dp(n);
  dp[1] = abs(a[1] - a[0]);
  for (ll 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];
}
Problem p03160 (C++)

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

#define INF (1 << 31) - 1 // INT_MAX/2
#define MOD 1000000007
#define PI acos(-1)

using ll = long long;
using ull = unsigned long long;
using P = std::pair<int, int>;

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 argc, char *argv[]) {
  int N;
  std::cin >> N;

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

  // dp table
  std::vector<ll> dp(N, INF);

  // init dp table
  // ok

  // init condition
  dp[0] = 0;

  // loop
  for (int i = 0; i < N; i++) {
    chmin(dp[i + 1], dp[i] + std::abs(h[i + 1] - h[i]));
    chmin(dp[i + 2], dp[i] + std::abs(h[i + 2] - h[i]));
  }

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

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

#define INF (1 << 31) - 1 // INT_MAX/2
#define MOD 1000000007
#define PI acos(-1)

using ll = long long;
using ull = unsigned long long;
using P = std::pair<int, int>;

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 argc, char *argv[]) {
  int N;
  std::cin >> N;

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

  // dp table
  std::vector<ll> dp(N + 2, INF);

  // init dp table
  // ok

  // init condition
  dp[0] = 0;

  // loop
  for (int i = 0; i < N; i++) {
    chmin(dp[i + 1], dp[i] + std::abs(h[i + 1] - h[i]));
    chmin(dp[i + 2], dp[i] + std::abs(h[i + 2] - h[i]));
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int solve(int i, int a[], vector<int> &dp, int n) {
  if (i > n - 2)
    return 0;
  if (i == n - 2) {
    return dp[i] = abs(a[n - 1] - a[n - 2]);
  }
  return dp[i] = min((abs(a[i] - a[i + 1]) + solve(i + 1, a, dp, n)),
                     (abs(a[i] - a[i + 2]) + solve(i + 2, a, dp, n)));
}
int main() {
  int n;
  cin >> n;
  int a[n];
  vector<int> dp(n, -1);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int m = solve(0, a, dp, n);
  cout << m;
  return 0;
}


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

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

int main(void) {
  int N;
  int h[10001];
  int dp[N + 1];
  int tmp, tnp;

  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++) {
    tmp = dp[i - 2] + abs(h[i] - h[i - 2]);
    tnp = dp[i - 1] + abs(h[i] - h[i - 1]);
    dp[i] = min(tmp, tnp);
  }

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

  return 0;
}

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

int main(void) {
  int N;
  int h[100001];
  int dp[N];
  int tmp, tnp;

  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++) {
    tmp = dp[i - 2] + abs(h[i] - h[i - 2]);
    tnp = dp[i - 1] + abs(h[i] - h[i - 1]);
    dp[i] = min(tmp, tnp);
  }

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

  return 0;
}
Problem p03160 (C++)

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int dp1[n];
  for (int i = 0; i < n; i++) {
    if (i == 0)
      dp1[i] = 0;
    else if (i == 1)
      dp1[i] = dp1[i - 1] + abs(a[i] - a[i - 1]);
    else
      dp1[i] = min(dp1[i - 1] + abs(a[i] - a[i - 1]),
                   dp1[i - 2] + abs(a[i] - a[i - 2]));
    // cout<< dp1[i]<<"\n";
  }
  cout << dp1[n - 1] << "\n";
  return 0;
}

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

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;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int dp1[n];
  for (int i = 0; i < n; i++) {
    if (i == 0)
      dp1[i] = 0;
    else if (i == 1)
      dp1[i] = dp1[i - 1] + abs(a[i] - a[i - 1]);
    else
      dp1[i] = min(dp1[i - 1] + abs(a[i] - a[i - 1]),
                   dp1[i - 2] + abs(a[i] - a[i - 2]));
    // cout<< dp1[i]<<"\n";
  }
  cout << dp1[n - 1] << "\n";
  return 0;
}
Problem p03160 (C++)

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

int fun(vector<int> &v, int i, vector<int> dp) {
  if (dp[i] != 1e6) {
    return dp[i];
  }
  if (i == v.size()) {

    return 1e5;
  }
  int ans = 0;
  if (i + 2 < v.size()) {

    ans = ans + min(abs(v[i] - v[i + 1]) + fun(v, i + 1, dp),
                    abs(v[i] - v[i + 2]) + fun(v, i + 2, dp));
  } else if (i + 1 < v.size()) {
    ans = ans + abs(v[i] - v[i + 1]) + fun(v, i + 1, dp);
  }
  dp[i] = ans;
  return ans;
}

int main() {
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  vector<int> dp(v.size(), 1e6);
  cout << fun(v, 0, dp);
}

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

int fun(vector<int> &v, int i, vector<int> &dp) {
  if (dp[i] != 1e6) {
    return dp[i];
  }
  if (i == v.size()) {

    return 1e5;
  }
  int ans = 0;
  if (i + 2 < v.size()) {

    ans = ans + min(abs(v[i] - v[i + 1]) + fun(v, i + 1, dp),
                    abs(v[i] - v[i + 2]) + fun(v, i + 2, dp));
  } else if (i + 1 < v.size()) {
    ans = ans + abs(v[i] - v[i + 1]) + fun(v, i + 1, dp);
  }
  dp[i] = ans;
  return ans;
}

int main() {
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  vector<int> dp(v.size(), 1e6);
  cout << fun(v, 0, dp);
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int n, h[10000], dp[10000], k = 3;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  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];
}

Fixed submission:
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int n, h[100000], dp[100000], k = 3;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  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];
}