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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vi>
#define forr(i, k, n) for (int i = k; i < n; i++)
#define iforr(i, k, n) for (int i = n - 1; i >= k; i--)
#define rep(i, n) forr(i, 0, n)
#define irep(i, n) iforr(i, 0, n)
#define aut(i, arr) for (auto &i : arr)
#define all(arr) arr.begin(), arr.end()
#define pb push_back
#define pii pair<int, int>
#define vpii vector<pii>
#define f first
#define db double
#define s second
#define ll long long
#define ull unsigned long long
#define E '\n'
#define lmax 9223372036854775807;
#define MOD 1000000007
using namespace std;
vi v;
vvi ed;
void dfs(int i) {
  v[i] = 1;
  aut(j, ed[i]) if (!v[j]) dfs(j);
}
void Fast_IO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("code.in", "r", stdin);
  freopen("code.out", "w", stdout);
#endif
}

int main() {
  Fast_IO();
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    int n;
    cin >> n;
    vi arr(n);
    vi dp(n, 0);
    rep(i, n) cin >> arr[i];
    dp[1] = abs(arr[1] - arr[0]);
    forr(i, 2, n) dp[i] = min(dp[i - 2] + abs(arr[i] - arr[i - 2]),
                              dp[i - 1] + abs(arr[i] - arr[i - 1]));
    cout << dp[n - 1];
    cout << E;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define vi vector<int>
#define vvi vector<vi>
#define forr(i, k, n) for (int i = k; i < n; i++)
#define iforr(i, k, n) for (int i = n - 1; i >= k; i--)
#define rep(i, n) forr(i, 0, n)
#define irep(i, n) iforr(i, 0, n)
#define aut(i, arr) for (auto &i : arr)
#define all(arr) arr.begin(), arr.end()
#define pb push_back
#define pii pair<int, int>
#define vpii vector<pii>
#define f first
#define db double
#define s second
#define ll long long
#define ull unsigned long long
#define E '\n'
#define lmax 9223372036854775807;
#define MOD 1000000007
using namespace std;
vi v;
vvi ed;
void dfs(int i) {
  v[i] = 1;
  aut(j, ed[i]) if (!v[j]) dfs(j);
}
void Fast_IO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("code.in", "r", stdin);
  freopen("code.out", "w", stdout);
#endif
}

int main() {
  // Fast_IO();
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    int n;
    cin >> n;
    vi arr(n);
    vi dp(n, 0);
    rep(i, n) cin >> arr[i];
    dp[1] = abs(arr[1] - arr[0]);
    forr(i, 2, n) dp[i] = min(dp[i - 2] + abs(arr[i] - arr[i - 2]),
                              dp[i - 1] + abs(arr[i] - arr[i - 1]));
    cout << dp[n - 1];
    cout << E;
  }
  return 0;
}
Problem p03160 (C++)

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

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

int main() {
  int N;
  cin >> N;
  copy_n(istream_iterator<int>(cin), N, h);
  memset(dp, N, 99999);
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < N; ++i)
    dp[i] =
        min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
  cout << dp[N - 1];
}


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

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

int main() {
  int N;
  cin >> N;
  copy_n(istream_iterator<int>(cin), N, h);
  memset(dp, N, 99999);
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < N; ++i)
    dp[i] =
        min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
  cout << dp[N - 1];
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
#define ve vector
#define maxe(v) *max_element(v.begin(), v.end())
#define mine(v) *min_element(v.begin(), v.end())
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define FAST                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mp make_pair
#define M 1000000007
#define PRECISE cout.precision(18);
#define all(c) c.begin(), c.end()
#define sec second
#define fst first
#define tr(container, it)                                                      \
  for (typeof(container.begin()) it = container.begin();                       \
       it != container.end(); it++)
int main() {

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  FAST ll n;
  cin >> n;
  ll h[n];
  for (ll i = 0; i < n; i++) {
    cin >> h[i];
  }
  ll dp[n];
  memset(dp, 0, sizeof(dp));
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (ll i = 2; i <= n - 1; 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];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
#define ve vector
#define maxe(v) *max_element(v.begin(), v.end())
#define mine(v) *min_element(v.begin(), v.end())
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define FAST                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mp make_pair
#define M 1000000007
#define PRECISE cout.precision(18);
#define all(c) c.begin(), c.end()
#define sec second
#define fst first
#define tr(container, it)                                                      \
  for (typeof(container.begin()) it = container.begin();                       \
       it != container.end(); it++)
int main() {
  FAST ll n;
  cin >> n;
  ll h[n];
  for (ll i = 0; i < n; i++) {
    cin >> h[i];
  }
  ll dp[n];
  memset(dp, 0, sizeof(dp));
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (ll i = 2; i <= n - 1; 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];
}
Problem p03160 (C++)

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second

using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int ans = 0;
  vi dp(n, 0);
  dp[0] = 0;
  if (n >= 1)
    dp[1] = abs(a[1] - a[0]);
  if (n >= 2) {
    for (int i = 2; i < n; ++i) {
      dp[i] = min(dp[i - 1] + abs(a[i] - a[i - 1]),
                  dp[i - 2] + abs(a[i] - a[i - 2]));
    }
  }
  // for (int i = 0; i < n; ++i)
  // {
  // 	cout << dp[i] << " ";
  // }
  cout << dp[n - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second

using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int ans = 0;
  vi dp(n, 0);
  dp[0] = 0;
  if (n >= 1)
    dp[1] = abs(a[1] - a[0]);
  if (n >= 2) {
    for (int i = 2; i < n; ++i) {
      dp[i] = min(dp[i - 1] + abs(a[i] - a[i - 1]),
                  dp[i - 2] + abs(a[i] - a[i - 2]));
    }
  }
  // for (int i = 0; i < n; ++i)
  // {
  // 	cout << dp[i] << " ";
  // }
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03160 (C++)

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

#define ll long long
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define bac(i, a, b) for (ll i = b - 1; i <= a; i--)

#define pb push_back
#define vi vector<int>
#define vll vector<ll>

#define mod 1000000007
vi v(100000, -1);
int result(vi ve, int indx, int size) {
  if (v[indx] != -1)
    return v[indx];
  if (indx == size - 1)
    return 0;
  if (indx > size - 1)
    return INT_MAX;
  return v[indx] =
             min(result(ve, indx + 1, size) + abs(ve[indx] - ve[indx + 1]),
                 indx + 2 <= size
                     ? result(ve, indx + 2, size) + abs(ve[indx] - ve[indx + 2])
                     : INT_MAX);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int a;
  cin >> a;
  vi vec(a + 1);
  for (int i = 0; i < a; i++)
    cin >> vec[i];
  int b = vec[a - 2];
  vec[a] = b;
  int res = result(vec, 0, a);
  cout << res << "\n";
  v.clear();
  return 0;
}


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

#define ll long long
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define bac(i, a, b) for (ll i = b - 1; i <= a; i--)

#define pb push_back
#define vi vector<int>
#define vll vector<ll>

#define mod 1000000007
vi v(100000, -1);
int result(vi &ve, int indx, int size) {
  if (v[indx] != -1)
    return v[indx];
  if (indx == size - 1)
    return 0;
  if (indx > size - 1)
    return INT_MAX;
  return v[indx] =
             min(result(ve, indx + 1, size) + abs(ve[indx] - ve[indx + 1]),
                 indx + 2 <= size
                     ? result(ve, indx + 2, size) + abs(ve[indx] - ve[indx + 2])
                     : INT_MAX);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int a;
  cin >> a;
  vi vec(a + 1);
  for (int i = 0; i < a; i++)
    cin >> vec[i];
  int b = vec[a - 2];
  vec[a] = b;
  int res = result(vec, 0, a);
  cout << res << "\n";
  v.clear();
  return 0;
}
Problem p03160 (C++)

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

#define int long long int
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mii map<int, int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define MOD 1000000007
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define FORD(i, a, n) for (int i = n - 1; i >= 0; i--)
using namespace std;

int32_t 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, a[100001];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int dp[n + 1]; // dp[i] reps cost to reach a[i]
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (int i = 2; i < n; i++) {
    dp[i] = min((dp[i - 1] + abs(a[i] - a[i - 1])),
                (dp[i - 2] + abs(a[i] - a[i - 2])));
  }

  cout << dp[n - 1];

  return 0;
}


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

#define int long long int
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mii map<int, int>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define MOD 1000000007
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define FORD(i, a, n) for (int i = n - 1; i >= 0; i--)
using namespace std;

int32_t main() {

  int n, a[100001];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int dp[n + 1]; // dp[i] reps cost to reach a[i]
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (int i = 2; i < n; i++) {
    dp[i] = min((dp[i - 1] + abs(a[i] - a[i - 1])),
                (dp[i - 2] + abs(a[i] - a[i - 2])));
  }

  cout << dp[n - 1];

  return 0;
}
Problem p03160 (C++)

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

#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define PB push_back
#define F first
#define S second
#define MP make_pair
#define ALL(obj) (obj).begin(), (obj).end()
#define debug(x) cerr << #x << ": " << x << "\n"

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 LINF = 1LL << 60;

int main() {

  ll dp[10010];
  REP(i, 10010) dp[i] = LINF;
  dp[0] = 0;

  ll n;
  cin >> n;
  vector<ll> h(n);
  REP(i, n) cin >> h[i];

  REPS(i, n) {
    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;
  return 0;
}


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

#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define PB push_back
#define F first
#define S second
#define MP make_pair
#define ALL(obj) (obj).begin(), (obj).end()
#define debug(x) cerr << #x << ": " << x << "\n"

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 LINF = 1LL << 60;

int main() {

  ll dp[110000];
  REP(i, 110000) dp[i] = LINF;
  dp[0] = 0;

  ll n;
  cin >> n;
  vector<ll> h(n);
  REP(i, n) cin >> h[i];

  REPS(i, n) {
    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;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define M 998244353
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define F first
#define S second
#define Zero(dp) memset(dp, 0, sizeof(dp))
#define pb emplace_back
using namespace std;
int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    int h[n];
    for (int i = 0; i < n; i++)
      cin >> h[i];
    int dp[n];
    dp[0] = 0;
    dp[1] = abs(h[1] - h[0]);
    for (int i = 2; i < n; i++) {
      dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]),
                  dp[i - 2] + abs(h[i] - h[i - 2]));
    }
    cout << dp[n - 1];
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define M 998244353
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define F first
#define S second
#define Zero(dp) memset(dp, 0, sizeof(dp))
#define pb emplace_back
using namespace std;
int32_t main() {
  /*
  #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
  #endif
  */
  ios int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    int h[n];
    for (int i = 0; i < n; i++)
      cin >> h[i];
    int dp[n];
    dp[0] = 0;
    dp[1] = abs(h[1] - h[0]);
    for (int i = 2; i < n; i++) {
      dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]),
                  dp[i - 2] + abs(h[i] - h[i - 2]));
    }
    cout << dp[n - 1];
  }
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ll long long
#define pb push_back
#define F first
#define S second
#define ld long double
#define vi vector<ll>
const int INF = 1e9;
const int N = 100005;
vi h(N, 0);
vi dp(N, 0);
void solve() {
  ll n;
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> h[i];

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

int main() {

  FASTIO;

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif

  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ll long long
#define pb push_back
#define F first
#define S second
#define ld long double
#define vi vector<ll>
const int INF = 1e9;
const int N = 100005;
vi h(N, 0);
vi dp(N, 0);
void solve() {
  ll n;
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> h[i];

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

int main() {

  FASTIO;
  /*
  #ifndef ONLINE_JUDGE
          // for getting input from input.txt
          freopen("input.txt", "r", stdin);
          // for writing output to output.txt
          freopen("output.txt", "w", stdout);
  #endif
  */
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
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 MOD 1000000007
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define mi map<int, int>
#define umi unordered_map<int, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define inf 1e18
#define case                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> pbds;
void pre() {
  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
}
int32_t main() {
  pre();
  // case
  {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++)
      cin >> a[i];
    if (n == 2) {
      cout << abs(a[0] - a[1]);
      return 0;
    }
    int dp[n];
    dp[0] = 0;
    dp[1] = abs(a[0] - a[1]);
    for (int i = 2; i < n; i++) {
      int s1 = dp[i - 1] + abs(a[i - 1] - a[i]);
      int s2 = dp[i - 2] + abs(a[i - 2] - a[i]);
      dp[i] = min(s1, s2);
    }
    cout << dp[n - 1];
  }
  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 MOD 1000000007
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define mi map<int, int>
#define umi unordered_map<int, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define inf 1e18
#define case                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> pbds;
void pre() {
  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
}
int32_t main() { // pre();
  // case
  {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++)
      cin >> a[i];
    if (n == 2) {
      cout << abs(a[0] - a[1]);
      return 0;
    }
    int dp[n];
    dp[0] = 0;
    dp[1] = abs(a[0] - a[1]);
    for (int i = 2; i < n; i++) {
      int s1 = dp[i - 1] + abs(a[i - 1] - a[i]);
      int s2 = dp[i - 2] + abs(a[i - 2] - a[i]);
      dp[i] = min(s1, s2);
    }
    cout << dp[n - 1];
  }
  return 0;
}
Problem p03160 (C++)

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

using ll = long long;

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

  vector<int> dp(100, 0);
  dp[0] = 0;
  dp[1] = dp[0] + 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;

using ll = long long;

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

  vector<int> dp(100'000, 0);
  dp[0] = 0;
  dp[1] = dp[0] + 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: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define rep(a, b, c) for (int a = (int)b; a < (int)c; a++)
#define repk(a, b, c, k) for (int a = (int)b; a < (int)c; a += (int)k)
#define comeback                                                               \
  std::ios_base::sync_with_stdio(false);                                       \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define mod 1000000007
#define int long long int
#define pii pair<int, int>
#define ful(a) a.begin(), a.end()
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
using namespace std;
int em(int x, int n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return em((x % mod * x % mod) % mod, n / 2);
  else
    return (x % mod * em((x % mod * x % mod) % mod, (n - 1) / 2)) % mod;
}
signed main() {
  comeback
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  int i, j;
  int t;
  cin >> t;
  int a[t + 1];
  int dp[t + 1];
  dp[1] = 0;
  for (i = 1; i <= t; i++)
    cin >> a[i];
  for (i = 2; i <= t; i++) {
    if (i - 2 >= 1) {
      dp[i] = min(dp[i - 1] + abs(a[i] - a[i - 1]),
                  dp[i - 2] + abs(a[i] - a[i - 2]));
    } else
      dp[i] = dp[i - 1] + abs(a[i] - a[i - 1]);
  }
  cout << dp[t];
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define rep(a, b, c) for (int a = (int)b; a < (int)c; a++)
#define repk(a, b, c, k) for (int a = (int)b; a < (int)c; a += (int)k)
#define comeback                                                               \
  std::ios_base::sync_with_stdio(false);                                       \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define mod 1000000007
#define int long long int
#define pii pair<int, int>
#define ful(a) a.begin(), a.end()
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
using namespace std;
int em(int x, int n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return em((x % mod * x % mod) % mod, n / 2);
  else
    return (x % mod * em((x % mod * x % mod) % mod, (n - 1) / 2)) % mod;
}
signed main() {
  comeback
      /*#ifndef ONLINE_JUDGE
              freopen("input.txt","r",stdin);
              freopen("output1.txt","w",stdout);
      #endif*/
      int i,
      j;
  int t;
  cin >> t;
  int a[t + 1];
  int dp[t + 1];
  dp[1] = 0;
  for (i = 1; i <= t; i++)
    cin >> a[i];
  for (i = 2; i <= t; i++) {
    if (i - 2 >= 1) {
      dp[i] = min(dp[i - 1] + abs(a[i] - a[i - 1]),
                  dp[i - 2] + abs(a[i] - a[i - 2]));
    } else
      dp[i] = dp[i - 1] + abs(a[i] - a[i - 1]);
  }
  cout << dp[t];
}
Problem p03160 (C++)

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


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

Buggy submission (status: Runtime Error):
// AnkitCode99 here....
#include <bits/stdc++.h>
#define endl "\n"
typedef long long int ll;
#define MOD 1000000007
#define mp make_pair
#define pll pair<ll, ll>
#define pb push_back
// max xor btw range of two numbers..
#define max_XOR(a, b) (1 << int(log2(a ^ b) + 1)) - 1
#define vl vector<ll>
#define inf ll(1e18)
#define all(v) v.begin(), v.end()
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define rep(i, a, b) for (long long int i = a; i < b; i++)
#define nrep(i, a, b) for (long long int i = a; i >= b; i--)
#define ceil(a, b) a / b + ((a % b == 0) ? 0 : 1)
using namespace std;

const ll sz = 100005;
const ll szz = 1000006;

ll n;
ll ar[sz], cache[sz];

// TOP - DOWN Approach

ll dp(ll idx) {
  if (idx > n)
    return ll(1e9);

  if (idx == n)
    return 0;

  if (cache[idx] != -1)
    return cache[idx];

  cache[idx] = min(abs(ar[idx] - ar[idx + 1]) + dp(idx + 1),
                   abs(ar[idx] - ar[idx + 2]) + dp(idx + 2));
  return cache[idx];
}

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

  cin >> n;
  memset(cache, -1, sizeof cache);
  rep(i, 1, n + 1) { cin >> ar[i]; }

  cout << dp(1) << endl;

  // rep(i,1,n+1)
  //     cout<<cache[i]<<" ";
} // Goodbye...

Fixed submission:
// AnkitCode99 here....
#include <bits/stdc++.h>
#define endl "\n"
typedef long long int ll;
#define MOD 1000000007
#define mp make_pair
#define pll pair<ll, ll>
#define pb push_back
// max xor btw range of two numbers..
#define max_XOR(a, b) (1 << int(log2(a ^ b) + 1)) - 1
#define vl vector<ll>
#define inf ll(1e18)
#define all(v) v.begin(), v.end()
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define rep(i, a, b) for (long long int i = a; i < b; i++)
#define nrep(i, a, b) for (long long int i = a; i >= b; i--)
#define ceil(a, b) a / b + ((a % b == 0) ? 0 : 1)
using namespace std;

const ll sz = 100005;
const ll szz = 1000006;

ll n;
ll ar[sz], cache[sz];

// TOP - DOWN Approach

ll dp(ll idx) {
  if (idx > n)
    return ll(1e9);

  if (idx == n)
    return 0;

  if (cache[idx] != -1)
    return cache[idx];

  cache[idx] = min(abs(ar[idx] - ar[idx + 1]) + dp(idx + 1),
                   abs(ar[idx] - ar[idx + 2]) + dp(idx + 2));
  return cache[idx];
}

int main() {
  fast;

  cin >> n;
  memset(cache, -1, sizeof cache);
  rep(i, 1, n + 1) { cin >> ar[i]; }

  cout << dp(1) << endl;

  // rep(i,1,n+1)
  //     cout<<cache[i]<<" ";
} // Goodbye...
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
const int MAX = 100005;
const long long mod = 1.0e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ll n, i, j;
  cin >> n;
  ll arr[n];
  for (i = 0; i < n; i++) {
    cin >> arr[i];
  }
  ll dp[n + 1];
  memset(dp, 0, sizeof(dp));
  dp[1] = abs(arr[1] - arr[0]);
  for (i = 2; i < n; i++) {
    dp[i] = min(dp[i - 1] + abs(arr[i - 1] - arr[i]),
                dp[i - 2] + abs(arr[i - 2] - arr[i]));
  }
  cout << dp[n - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
const int MAX = 100005;
const long long mod = 1.0e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, i, j;
  cin >> n;
  ll arr[n];
  for (i = 0; i < n; i++) {
    cin >> arr[i];
  }
  ll dp[n + 1];
  memset(dp, 0, sizeof(dp));
  dp[1] = abs(arr[1] - arr[0]);
  for (i = 2; i < n; i++) {
    dp[i] = min(dp[i - 1] + abs(arr[i - 1] - arr[i]),
                dp[i - 2] + abs(arr[i - 2] - arr[i]));
  }
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
// ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll x, ll y) {return x / gcd(x, y) * y;}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll mod = 1e9 + 7;
const ll inf = 1LL << 60;
const long double pi = 3.14159265358979323846;

// ****************************************CODE***************************************//

ll n, k, h[100100];
ll dp[100100];

ll rec(ll i) {
  if (dp[i] != inf)
    return dp[i];

  if (i == 0)
    return 0;

  ll res = inf;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= k; j++) {
      if (i - j >= 0)
        res = min(res, rec(i - j) + abs(h[i] - h[i - 1]));
    }
  }

  return dp[i] = res;
}

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

  for (int i = 0; i < 100100; i++)
    dp[i] = inf;

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


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
// ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll x, ll y) {return x / gcd(x, y) * y;}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll mod = 1e9 + 7;
const ll inf = 1LL << 60;
const long double pi = 3.14159265358979323846;

// ****************************************CODE***************************************//

ll n, k, h[100100];
ll dp[100100];

ll rec(ll i) {
  if (dp[i] != inf)
    return dp[i];

  if (i == 0)
    return 0;

  ll res = inf;
  for (int j = 1; j <= k; j++) {
    if (i - j >= 0)
      res = min(res, rec(i - j) + abs(h[i] - h[i - j]));
  }

  return dp[i] = res;
}

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

  for (int i = 0; i < 100100; i++)
    dp[i] = inf;

  cout << rec(n - 1) << endl;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define MAX 100005

using namespace std;

const int INF = 1e9 + 5;
ll arr[MAX];
vector<ll> dp(MAX, INF);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  dp[1] = 0;
  dp[2] = abs(arr[2] - arr[1]);
  for (int i = 3; i <= n; i++)
    for (int j = 1; j <= k; j++)
      if (j > 0)
        dp[i] = min(dp[i - j] + abs(arr[i] - arr[i - j]), dp[i]);
  cout << dp[n] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define MAX 100005

using namespace std;

const int INF = 1e9 + 5;
ll arr[MAX];
vector<ll> dp(MAX, INF);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  dp[1] = 0;
  dp[2] = abs(arr[2] - arr[1]);
  for (int i = 3; i <= n; i++)
    for (int j = 1; j <= k; j++) {
      if (i - j <= 0)
        break;
      dp[i] = min(dp[i - j] + abs(arr[i] - arr[i - j]), dp[i]);
    }
  cout << dp[n] << endl;
  return 0;
}
Problem p03161 (C++)

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

int main() {
  int n, k;
  int _min;
  cin >> n >> k;
  vector<int> h(n);
  vector<int> dp(n);

  for (int i = 0; i < n; i++) {
    cin >> h.at(i);
  };
  if (n <= k + 1) {
    cout << abs(h.at(n - 1) - h.at(0)) << endl;
    exit(1);
  };
  for (int i = 0; i <= k; i++) {
    dp.at(i) = abs(h.at(i) - h.at(0));
  };
  for (int g = k + 1; g < n; g++) {
    _min = 999999999;
    for (int l = g - k; l < g; l++) {
      _min = min(_min, dp.at(l) + abs(h.at(g) - h.at(l)));
    }
    dp.at(g) = _min;
  };

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

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

int main() {
  int n, k;
  int _min;
  cin >> n >> k;
  vector<int> h(n);
  vector<int> dp(n);

  for (int i = 0; i < n; i++) {
    cin >> h.at(i);
  };
  if (n <= k + 1) {
    cout << abs(h.at(n - 1) - h.at(0)) << endl;
  } else {
    for (int i = 0; i <= k; i++) {
      dp.at(i) = abs(h.at(i) - h.at(0));
    };
    for (int g = k + 1; g < n; g++) {
      _min = 999999999;
      for (int l = g - k; l < g; l++) {
        _min = min(_min, dp.at(l) + abs(h.at(g) - h.at(l)));
      }
      dp.at(g) = _min;
    };
    cout << dp.at(n - 1) << endl;
  }
  return 0;
}
Problem p03161 (C++)

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

#define int long long
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define COUT(x) cout << x << endl;
#define PB(x) push_back(x)
#define MP make_pair
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

signed main() {
  int n, k;
  cin >> n >> k;
  // 0-index
  vector<int> h(10010), dp(10010, INF);
  rep(i, n) cin >> h[i];
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= k; j++) {
      chmin(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
    }
  }
  cout << dp[n - 1] << endl;
}

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

#define int long long
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define COUT(x) cout << x << endl;
#define PB(x) push_back(x)
#define MP make_pair
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

signed main() {
  int n, k;
  cin >> n >> k;
  // 0-index
  vector<int> h(100100), dp(100100, INF);
  rep(i, n) cin >> h[i];
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= k; j++) {
      chmin(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
    }
  }
  cout << dp[n - 1] << endl;
}
Problem p03161 (C++)

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

#define sz(c) c.size()
#define fr first
#define sc second
#define pb push_back
#define sz(c) c.size()
#define ppb pop_back
#define all(a) (a).begin(), (a).end()
#define rep(i, a, n) for (int i = a; i < n; i++)
#define U 1000000007
#define N 100005
#define ins insert
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int h[n], ans[n];

  rep(i, 0, n) {
    cin >> h[i];
    ans[i] = INT_MAX;
  }
  ans[0] = 0;
  rep(i, 0, n) {
    rep(j, 1, k + 1) ans[i + j] =
        min(ans[i + j], ans[i] + abs(h[i + j] - h[i]));
  }
  cout << ans[n - 1];
}

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

#define sz(c) c.size()
#define fr first
#define sc second
#define pb push_back
#define sz(c) c.size()
#define ppb pop_back
#define all(a) (a).begin(), (a).end()
#define rep(i, a, n) for (int i = a; i < n; i++)
#define U 1000000007
#define N 100005
#define ins insert
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int h[n], ans[n];

  rep(i, 0, n) {
    cin >> h[i];
    ans[i] = INT_MAX;
  }
  ans[0] = 0;
  rep(i, 0, n) {
    rep(j, 1, k + 1) {
      if (i + j < n)
        ans[i + j] = min(ans[i + j], ans[i] + abs(h[i + j] - h[i]));
    }
  }
  cout << ans[n - 1];
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define MOD 1000000007
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, n) for (int i = 0; i < n; i++)
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;
}
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  ll h[100010];
  REP(i, N) cin >> h[i];

  ll INF = 1LL << 60;
  ll dp[100010];
  REP(i, 100010) dp[i] = INF;
  dp[0] = 0;

  REP(i, N) {
    FOR(j, 1, K + 1) { chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); }
  }
  cout << dp[N - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define MOD 1000000007
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, n) for (int i = 0; i < n; i++)
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;
}
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  ll h[100010];
  REP(i, N) cin >> h[i];

  ll INF = 1LL << 60;
  ll dp[101000];
  REP(i, 100010) dp[i] = INF;
  dp[0] = 0;

  REP(i, N) {
    FOR(j, 1, K + 1) { chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); }
  }
  cout << dp[N - 1] << endl;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
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;
}

using namespace std;

const int INF = 1e9;

int main() {
  int N, K;
  cin >> N >> K;
  int h[100010];
  for (int i = 0; i < N; i++)
    cin >> h[i];
  int dp[100010];
  for (int i = 0; i < N; i++)
    dp[i] = INF;
  dp[0] = 0;

  for (int i = 0; i < N; i++) {
    for (int j = 1; j <= K; j++) {
      chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
    }
  }
  cout << dp[N - 1] << endl;
}

Fixed submission:
#include <iostream>
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;
}

using namespace std;

const int INF = 1e9;

int main() {
  int N, K;
  cin >> N >> K;
  int h[100010];
  for (int i = 0; i < N; i++)
    cin >> h[i];
  int dp[110000];
  for (int i = 0; i < N; i++)
    dp[i] = INF;
  dp[0] = 0;

  for (int i = 0; i < N; i++) {
    for (int j = 1; j <= K; j++) {
      chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
    }
  }
  cout << dp[N - 1] << endl;
}
Problem p03161 (C++)

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

using namespace std;

#define F first
#define S second

int N, K;
int H[100005];
int dp[100001];

int main() {
  cin >> N >> K;
  H[0] = INT32_MAX; // to avoid RTE
  for (int i = 1; i <= N; i++)
    cin >> H[i];
  for (int i = 0; i <= N; i++)
    dp[i] = INT32_MAX;
  int ans = 0;

  dp[N] = 0; // the base case
  K = min(N - 1, K);

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

  cout << dp[1] << endl;

  return 0;
}

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

using namespace std;

#define F first
#define S second

int N, K;
int H[100005];
int dp[100001];

int main() {
  cin >> N >> K;
  H[0] = INT32_MAX; // to avoid RTE
  for (int i = 1; i <= N; i++)
    cin >> H[i];
  for (int i = 0; i <= N; i++)
    dp[i] = INT32_MAX;
  int ans = 0;

  dp[N] = 0; // the base case
  K = min(N - 1, K);

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

  cout << dp[1] << endl;

  return 0;
}
Problem p03161 (C++)

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

int main() {
  // your code goes here
  ll n, k;
  cin >> n;
  cin >> k;
  ll arr[100001], dp[100001], i, ans;
  for (i = 0; i < n; i++) {
    cin >> arr[i];
  }
  dp[0] = 0;
  for (i = 1; i < 100001; i++) {
    dp[i] = 1e9;
  }
  for (i = 0; i < n; i++) {
    for (int j = 1; j <= k; j++) {
      dp[i + j] = min(dp[i + j], abs(arr[i] - arr[i + j]) + dp[i]);
    }
  }
  cout << dp[n - 1] << "\n";
  return 0;
}

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

int main() {
  // your code goes here
  ll n, k;
  cin >> n;
  cin >> k;
  ll arr[100001], dp[10000001], i, ans;
  for (i = 0; i < n; i++) {
    cin >> arr[i];
  }
  dp[0] = 0;
  for (i = 1; i < 100001; i++) {
    dp[i] = 1e9;
  }
  for (i = 0; i < n; i++) {
    for (int j = 1; j <= k; j++) {
      dp[i + j] = min(dp[i + j], abs(arr[i] - arr[i + j]) + dp[i]);
    }
  }
  cout << dp[n - 1] << "\n";
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, k;
  cin >> n >> k;
  int array[n];
  for (int x = 0; x < n; x++) {
    cin >> array[x];
  }
  int dp[n];
  dp[0] = 0;
  for (int x = 0; x <= k; x++) {
    dp[x] = abs(array[x] - array[0]);
    for (int i = 0; i < x; i++) {
      dp[x] = min(dp[x], dp[i] + abs(array[x] - array[i]));
    }
  }
  for (int x = k + 1; x < n; x++) {
    dp[x] = dp[x - k] + abs(array[x] - array[x - k]);
    for (int i = x - k; i < x; i++) {
      dp[x] = min(dp[x], dp[i] + abs(array[x] - array[i]));
    }
  }
  cout << dp[n - 1];

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, k;
  cin >> n >> k;
  int array[n];
  for (int x = 0; x < n; x++) {
    cin >> array[x];
  }
  int dp[n];
  dp[0] = 0;
  for (int x = 0; x <= k && x < n; x++) {
    dp[x] = abs(array[x] - array[0]);
    for (int i = 0; i < x; i++) {
      dp[x] = min(dp[x], dp[i] + abs(array[x] - array[i]));
    }
  }
  for (int x = k + 1; x < n; x++) {
    dp[x] = dp[x - k] + abs(array[x] - array[x - k]);
    for (int i = x - k; i < x; i++) {
      dp[x] = min(dp[x], dp[i] + abs(array[x] - array[i]));
    }
  }
  cout << dp[n - 1];

  return 0;
}
Problem p03161 (C++)

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

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

  return 0;
}


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

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

  return 0;
}