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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define repone(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define each(i, mp) for (auto i : mp)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll half_inf = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);

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 n, k;
vector<ll> h;

void solve() {
  ll dp[half_inf];
  rep(i, half_inf) 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;
}

int main() {
  cin >> n >> k;
  h.resize(n);
  rep(i, n) cin >> h[i];
  solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define repone(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define each(i, mp) for (auto i : mp)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll half_inf = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);

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 n, k;
vector<ll> h;

void solve() {
  ll dp[half_inf + k];
  rep(i, half_inf + k) 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;
}

int main() {
  cin >> n >> k;
  h.resize(n);
  rep(i, n) cin >> h[i];
  solve();
  return 0;
}
Problem p03161 (C++)

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

int main() {
  int N, K;
  int h[10102];
  int64_t dp[10102];
  cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    cin >> h[i];
    dp[i] = 100000 * 10001;
  }
  dp[0] = 0;
  for (int i = 0; i < N - 1; ++i) {
    for (int j = 1; j < K + 1; ++j) {
      dp[i + j] = min(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;

int main() {
  int N, K;
  int h[100102];
  int64_t dp[100102];
  cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    cin >> h[i];
    dp[i] = 100000 * 10001;
  }
  dp[0] = 0;
  for (int i = 0; i < N - 1; ++i) {
    for (int j = 1; j < K + 1; ++j) {
      dp[i + j] = min(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>
using namespace std;
using ll = long long;
#define rep(n, i) for (ll i = 0; i < n; ++i)
const ll INF = 1e18;

void solve(void) {
  ll n, k;
  cin >> n >> k;
  vector<ll> h(n, 0);
  rep(n, i) cin >> h[i];

  vector<ll> dp(n + 1, INF);
  dp[0] = 0;
  for (ll i = 1; i < n; ++i) {
    for (ll j = 1; j <= k; ++j) {
      dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
    }
  }

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

int main(void) {
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(n, i) for (ll i = 0; i < n; ++i)
const ll INF = 1e18;

void solve(void) {
  ll n, k;
  cin >> n >> k;
  vector<ll> h(n, 0);
  rep(n, i) cin >> h[i];

  vector<ll> dp(n + 1, INF);
  dp[0] = 0;
  for (ll i = 1; i < n; ++i) {
    for (ll j = 1; j <= k; ++j) {
      if (i >= j)
        dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
    }
  }

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

int main(void) {
  solve();
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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;
}
const ll IMF = 1LL << 60;
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> h(N);

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

  ll dp[N];
  for (int i = 0; i < N; i++) {
    dp[i] = IMF;
  }
  dp[0] = 0;

  for (int i = 0; i < N; i++) {
    for (int k = 1; k <= K; k++) {
      if (i + k >= N)
        chmin(dp[i + k], dp[i] + abs(h[i + k] - h[i]));
    }
  }

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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;
}
const ll IMF = 1LL << 60;
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> h(N);

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

  ll dp[N];
  for (int i = 0; i < N; i++) {
    dp[i] = IMF;
  }
  dp[0] = 0;

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

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

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

using namespace std;

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

  int *dp = new int[n]; // dp[i]はi番目の足場までの最小コスト
  dp[0] = 0;

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

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

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

using namespace std;

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

  int *dp = new int[n]; // dp[i]はi番目の足場までの最小コスト
  dp[0] = 0;

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

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

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

typedef long long ll;
const ll INF = 1LL << 60;

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

  int A[100000];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  vector<ll> dp(n, INF);
  dp[0] = 0;

  for (int i = 1; i < n; i++) {
    for (int j = 1; j <= k; j++) {
      dp[i] = min(dp[i], dp[i - j] + abs(A[i] - A[i - j]));
    }
  }
  cout << dp.back() << endl;
}

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

typedef long long ll;
const ll INF = 1LL << 60;

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

  int A[100000];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  vector<ll> dp(n, INF);
  dp[0] = 0;

  for (int i = 1; i < n; i++) {
    for (int j = 1; j <= k; j++) {
      if (i - j >= 0) {
        dp[i] = min(dp[i], dp[i - j] + abs(A[i] - A[i - j]));
      }
    }
  }
  cout << dp.back() << endl;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
/*
  The Island Was Silent before.
  .....
  And One day again it became Silent.
*/
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define ll long long
#define modd(a, b) ((a + 2 * b) % b)
#define debug(a) cout << #a << ": " << (a) << "\n"
#define ioso ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define rtt cerr << "Time: " << clock() * 1.0 / CLOCKS_PER_SEC << endl;
#define ffe                                                                    \
  freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);

int main() {
  ioso
#ifndef ONLINE_JUDGE
      ffe
#endif
          ll n,
      k;
  cin >> n >> k;
  int co[100010];
  for (int i = 1; i <= n; i++) {
    cin >> co[i];
  }
  int dp[100010];
  memset(dp, -1, sizeof(dp));
  dp[1] = 0;
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      if (dp[i + j] == -1) {
        dp[i + j] = dp[i] + (abs(co[i + j] - co[i]));
      } else {
        dp[i + j] = min(dp[i + j], dp[i] + abs(co[i + j] - co[i]));
      }
    }
  }

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

Fixed submission:
/*
  The Island Was Silent before.
  .....
  And One day again it became Silent.
*/
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define ll long long
#define modd(a, b) ((a + 2 * b) % b)
#define debug(a) cout << #a << ": " << (a) << "\n"
#define ioso ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define rtt cerr << "Time: " << clock() * 1.0 / CLOCKS_PER_SEC << endl;
#define ffe                                                                    \
  freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);

int main() {
  ioso
#ifndef ONLINE_JUDGE
      ffe
#endif
          ll n,
      k;
  cin >> n >> k;
  int co[100010];
  for (int i = 1; i <= n; i++) {
    cin >> co[i];
  }
  int dp[100010];
  memset(dp, -1, sizeof(dp));
  dp[1] = 0;
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      if (i + j <= n) {
        if (dp[i + j] == -1) {
          dp[i + j] = dp[i] + (abs(co[i + j] - co[i]));
        } else {
          dp[i + j] = min(dp[i + j], dp[i] + abs(co[i + j] - co[i]));
        }
      }
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define itn int
#define REP(i, n) for (int i = 0; i < n; i++)
#define IREP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPEACH(i, k) for (auto &i : k)
#define all(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
typedef long long ll;

const ll INF = 1LL << 60;

signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<ll> dp(n, INF);
  REP(i, n) {
    cin >> a[i];
    if (i != 0) {
      if (i <= 1) {
        for (int j = 0; j != i; j++) {
          dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j]));
        }
      } else {
        for (int j = i - 1; j >= i - k; j--) {
          dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j]));
        }
      }
    } else {
      dp[0] = 0;
    }
  }
  cout << dp[n - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define itn int
#define REP(i, n) for (int i = 0; i < n; i++)
#define IREP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPEACH(i, k) for (auto &i : k)
#define all(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
typedef long long ll;

const ll INF = 1LL << 60;

signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<ll> dp(n, INF);
  REP(i, n) {
    cin >> a[i];
    if (i != 0) {
      if (i <= k) {
        for (int j = 0; j != i; j++) {
          dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j]));
        }
      } else {
        for (int j = i - 1; j >= i - k; j--) {
          dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j]));
        }
      }
    } else {
      dp[0] = 0;
    }
  }
  cout << dp[n - 1] << endl;
}
Problem p03161 (C++)

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

int main() {
  int N, k;
  cin >> N >> k;
  int h[N];   // INPUT ARRAY
  int ans[N]; // OUTPUT ARRAY
  for (int i = 0; i < N; i++) {
    cin >> h[i];
    ans[i] = INT_MAX;
  }
  ans[0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N, j - i <= k; j++)
      ans[j] = min(ans[j], ans[i] + abs(h[i] - h[j]));
  }
  cout << ans[N - 1];

  return 0;
}


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

int main() {
  int N, k;
  cin >> N >> k;
  int h[N];   // INPUT ARRAY
  int ans[N]; // OUTPUT ARRAY
  for (int i = 0; i < N; i++) {
    cin >> h[i];
    ans[i] = INT_MAX;
  }
  ans[0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N && j - i <= k; j++)
      ans[j] = min(ans[j], ans[i] + abs(h[i] - h[j]));
  }
  cout << ans[N - 1];

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int nature(ll a) { // 絶対値を返す
  if (a >= 0) {
    return (a);
  } else {
    return (-1 * a);
  }
}
int main() {
  ll N, K, h[N], dp[N], ans;
  cin >> N >> K;               // 足場の個数N//何個先まで飛べるかK
  for (ll i = 0; i < N; i++) { // 各足場の高さ
    cin >> h[i];
  }
  dp[0] = 0;
  for (ll i = 1; i < N; i++) {
    ans = dp[i - 1] + nature(h[i] - h[i - 1]);
    for (ll j = 1; j <= min(K, i); j++) {
      ans = min(dp[i - j] + nature(h[i] - h[i - j]),
                ans); // 各足場までの最小コスト
    }
    dp[i] = ans;
  }
  cout << dp[N - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int nature(ll a) { // 絶対値を返す
  if (a >= 0) {
    return (a);
  } else {
    return (-1 * a);
  }
}
int main() {
  ll N, K, h[100010], dp[100010], ans;
  cin >> N >> K;               // 足場の個数N//何個先まで飛べるかK
  for (ll i = 0; i < N; i++) { // 各足場の高さ
    cin >> h[i];
  }
  dp[0] = 0;
  for (ll i = 1; i < N; i++) {
    ans = dp[i - 1] + nature(h[i] - h[i - 1]);
    for (ll j = 1; j <= min(K, i); j++) {
      ans = min(dp[i - j] + nature(h[i] - h[i - j]),
                ans); // 各足場までの最小コスト
    }
    dp[i] = ans;
  }
  cout << dp[N - 1] << endl;
}
Problem p03161 (C++)

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

using namespace std;

const int MaxN = 100005;
const int oo = 1e9;

int n, k;
int h[MaxN], dp[MaxN];

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

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

using namespace std;

const int MaxN = 100005;
const int oo = 1e9;

int n, k;
int h[MaxN], dp[MaxN];

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

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

int x, k;
int main() {
  cin >> x >> k;
  int n;
  for (int i = 0; i < x; i++) {
    cin >> n;
    peso.push_back(n);
    dp[i] = 1e9;
  }

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

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

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

int x, k;
int main() {
  cin >> x >> k;
  int n;
  for (int i = 0; i < x; i++) {
    cin >> n;
    peso.push_back(n);
    dp[i] = 1e9;
  }

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

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

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

using namespace std;

int main() {
  int n, k;

  cin >> n;
  cin >> k;

  vector<int> v(n);
  vector<int> dp(n, INT_MAX);

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

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

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

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

using namespace std;

int main() {
  int n, k;

  cin >> n;
  cin >> k;

  vector<int> v(n);
  vector<int> dp(n, INT_MAX);

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
ll int gcd(ll int a, ll int b) { return b ? gcd(b, a % b) : a; }

int main() {
  int n, k;
  cin >> n >> k;
  std::vector<int> v(n);
  int dp[n];
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  dp[0] = 0;
  dp[1] = abs(v[1] - v[0]);
  for (int i = 2; i < n; i++) {
    int val = INT_MAX;
    int j = 1;
    while (j <= k && i - j < n) {
      val = min(dp[i - j] + abs(v[i] - v[i - j]), val);
      // cout<<"hi "<<val<<"\n";
      j++;
    }
    dp[i] = val;
    // cout<<i<<" "<<dp[i]<<"--"<<dp[i-1]<<"--"<<dp[i-2]<<"\n";
  }
  cout << dp[n - 1] << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
ll int gcd(ll int a, ll int b) { return b ? gcd(b, a % b) : a; }

int main() {
  int n, k;
  cin >> n >> k;
  std::vector<int> v(n);
  int dp[n];
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  dp[0] = 0;
  dp[1] = abs(v[1] - v[0]);
  for (int i = 2; i < n; i++) {
    int val = INT_MAX;
    int j = 1;
    while (j <= k && i - j >= 0) {
      val = min(dp[i - j] + abs(v[i] - v[i - j]), val);
      // cout<<"hi "<<val<<"\n";
      j++;
    }
    dp[i] = val;
    // cout<<i<<" "<<dp[i]<<"--"<<dp[i-1]<<"--"<<dp[i-2]<<"\n";
  }
  cout << dp[n - 1] << "\n";
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
// A-Frog2.cpp
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
  // freopen("input.txt","r",stdin);
  ll n, k;
  cin >> n >> k;
  ll *arr = new ll[n];
  for (ll i = 0; i < n; i++)
    cin >> arr[i];

  ll *dp = new ll[n + 1]{};
  dp[0] = 0;
  dp[1] = abs(arr[1] - arr[0]);
  for (ll i = 2; i < n; i++) {
    ll best = INT_MAX;
    for (ll j = 1; j <= k; j++) {
      best = min(best, dp[i - j] + abs(arr[i - j] - arr[i]));
    }
    dp[i] = best;
  }
  // for(ll i=0;i<n;i++)cout<<dp[i]<<" ";
  cout << dp[n - 1] << endl;
  return 0;
}

Fixed submission:
// A-Frog2.cpp
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
  // freopen("input.txt","r",stdin);
  ll n, k;
  cin >> n >> k;
  ll *arr = new ll[n];
  for (ll i = 0; i < n; i++)
    cin >> arr[i];

  ll *dp = new ll[n + 1]{};
  dp[0] = 0;
  dp[1] = abs(arr[1] - arr[0]);
  for (ll i = 2; i < n; i++) {
    ll best = INT_MAX;
    for (ll j = 1; j <= k; j++) {
      if (j <= i)
        best = min(best, dp[i - j] + abs(arr[i - j] - arr[i]));
    }
    dp[i] = best;
  }
  // for(ll i=0;i<n;i++)cout<<dp[i]<<" ";
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

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

#define fi first
#define se second
#define be begin
#define en end
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ALL(a) (a).be(), (a).en()

using LL = long long;

template <typename T> using V = vector<T>;
using Vi = V<int>;
using Vll = V<LL>;
using Vs = V<string>;

const LL INFLL = 999999999999999LL;

template <typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}

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

LL dp[100005];
int main() {
  int n, k;
  cin >> n >> k;

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

  FILL(dp, INFLL);
  dp[0] = 0;

  for (int i = 1; i < n; i++) {
    int w = ((i - k >= 0) ? k : k - 1);
    for (int j = 1; j <= w; j++)
      chmin(dp[i], dp[i - j] + abs(h[i - j] - h[i]));
  }

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

  return 0;
}

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

#define fi first
#define se second
#define be begin
#define en end
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ALL(a) (a).be(), (a).en()

using LL = long long;

template <typename T> using V = vector<T>;
using Vi = V<int>;
using Vll = V<LL>;
using Vs = V<string>;

const LL INFLL = 999999999999999LL;

template <typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}

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

LL dp[100005];
int main() {
  int n, k;
  cin >> n >> k;

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

  FILL(dp, INFLL);
  dp[0] = 0;

  for (int i = 1; i < n; i++) {
    int w = ((i - k >= 0) ? k : i);
    for (int j = 1; j <= w; j++)
      chmin(dp[i], dp[i - j] + abs(h[i - j] - h[i]));
  }

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

  return 0;
}
Problem p03161 (C++)

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

using namespace std;
int mod(int a) {
  if (a > 0) {
    return (a);
  }
  return (-1 * a);
}
int min(int a, int b) {
  if (a > b) {
    // cout<<b<<"\t" ;
    return (b);
  }
  // cout<<a<<"\t" ;
  return (a);
}
int main() {
  int a, b, c, i, j, n, k;
  cin >> n >> k;
  vector<int> v, cost;
  for (i = 0; i < n; i = i + 1) {
    cin >> a;
    v.push_back(a);
  }
  int arr[n];
  arr[1] = mod(v[1] - v[0]);
  arr[0] = 0;
  for (i = 2; i < n; i = i + 1) {
    b = arr[i - 1] + mod(v[i - 1] - v[i]);
    for (j = 2; j <= k; j = j + 1) {
      c = arr[i - j] + mod(v[i - j] - v[i]);
      b = min(b, c);
    }
    arr[i] = b;
  }
  cout << arr[n - 1];
}

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

using namespace std;
int mod(int a) {
  if (a > 0) {
    return (a);
  }
  return (-1 * a);
}
int min(int a, int b) {
  if (a > b) {
    // cout<<b<<"\t" ;
    return (b);
  }
  // cout<<a<<"\t" ;
  return (a);
}
int main() {
  int a, b, c, i, j, n, k;
  cin >> n >> k;
  vector<int> v, cost;
  for (i = 0; i < n; i = i + 1) {
    cin >> a;
    v.push_back(a);
  }
  int arr[n];
  arr[1] = mod(v[1] - v[0]);
  arr[0] = 0;
  for (i = 2; i < n; i = i + 1) {
    b = arr[i - 1] + mod(v[i - 1] - v[i]);
    for (j = 2; j <= k; j = j + 1) {
      if (i - j < 0) {
        break;
      }
      c = arr[i - j] + mod(v[i - j] - v[i]);
      b = min(b, c);
    }
    arr[i] = b;
  }
  cout << arr[n - 1];
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fu(a, b) for (int i = a; i < b; i++)
#define fv(a, b) for (int j = a; j < b; j++)
using namespace std;

int mini(int x, int y) { return x > y ? y : x; }

int main() {
  int dp[100001];
  fu(0, 100001) dp[i] = INT_MAX;
  dp[0] = dp[1] = 0;
  int n, k;
  cin >> n >> k;
  int a[n + 1];
  fu(1, n + 1) { cin >> a[i]; }
  dp[2] = abs(a[1] - a[2]);
  fu(1, n + 1) {
    fv(1, k + 1) {
      dp[i + j] = mini(dp[i + j], dp[i] + abs(a[i] - a[i + j]));
      if (i + j == n)
        break;
    }
  }

  cout << dp[n];
}

Fixed submission:
#include <bits/stdc++.h>
#define fu(a, b) for (int i = a; i < b; i++)
#define fv(a, b) for (int j = a; j < b; j++)
using namespace std;

int mini(int x, int y) { return x > y ? y : x; }

int main() {
  int dp[100001];
  fu(0, 100001) dp[i] = INT_MAX;
  dp[0] = dp[1] = 0;
  int n, k;
  cin >> n >> k;
  int a[n + 1];
  fu(1, n + 1) { cin >> a[i]; }
  dp[2] = abs(a[1] - a[2]);
  fu(1, n + 1) {
    fv(i + 1, i + k + 1) {
      if (j < n + 1)
        dp[j] = mini(dp[j], dp[i] + abs(a[i] - a[j]));
    }
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define repone(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define each(i, mp) for (auto i : mp)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll half_inf = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);

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 n, k;
vector<ll> h;

void solve() {
  ll dp[n];
  rep(i, n) dp[i] = inf;
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < n; ++i) {
    for (int j = 1; j <= k; ++j) {
      chmin(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
    }
  }
  cout << dp[n - 1] << endl;
}

int main() {
  cin >> n >> k;
  h.resize(n);
  rep(i, n) cin >> h[i];
  solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define repone(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define each(i, mp) for (auto i : mp)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll half_inf = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);

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 n, k;
vector<ll> h;

void solve() {
  ll dp[n];
  rep(i, n) dp[i] = inf;
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < n; ++i) {
    for (int j = 1; j <= k; ++j) {
      if (i - j >= 0)
        chmin(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
    }
  }
  cout << dp[n - 1] << endl;
}

int main() {
  cin >> n >> k;
  h.resize(n);
  rep(i, n) cin >> h[i];
  solve();
  return 0;
}
Problem p03161 (C++)

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

using namespace std;

#define lp(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define MP make_pair
#define SZ(v) ((int)((v).size()))
typedef long long ll;

const ll MOD = 1e9 + 7;
const int OO = (int)1e9;
const int N = (int)1e5 + 5;
const int K = 105;
void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int n;
int k;
int dp[N];
int a[N];
int solve(int idx) {
  if (idx > n)
    return OO;
  if (idx == n - 1)
    return 0;
  int &ret = dp[idx];
  if (~ret)
    return ret;
  ret = OO;
  for (int i = 1; i <= k; i++) {
    ret = min(ret, solve(idx + i) + abs(a[idx + i] - a[idx]));
  }
  return ret;
}
int dp2[N][K];
int solve_rec(int idx, int k_so_far) {
  if (idx == n - 1)
    return abs(a[idx] - a[idx - k_so_far]);
  if (k_so_far == k)
    return solve_rec(idx, 0) + abs(a[idx] - a[idx - k_so_far]);
  int &ret = dp2[idx][k_so_far];
  if (~ret)
    return ret;
  ret = solve_rec(idx + 1, k_so_far + 1);
  if (k_so_far > 0)
    ret = min(ret, solve_rec(idx, 0) + abs(a[idx] - a[idx - k_so_far]));
  return ret;
}
int main() {
#ifdef LOCAL_PROJECT
  freopen("in.txt", "r", stdin);
#endif
  scanf("%d", &n);
  scanf("%d", &k);
  memset(dp, -1, sizeof(dp));
  memset(dp, -1, sizeof(dp2));
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  // printf("%d\n",solve(0));
  printf("%d\n", solve_rec(0, 0));
  return 0;
}

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

using namespace std;

#define lp(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define MP make_pair
#define SZ(v) ((int)((v).size()))
typedef long long ll;

const ll MOD = 1e9 + 7;
const int OO = (int)1e9;
const int N = (int)1e5 + 5;
const int K = 105;
void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int n;
int k;
int dp[N];
int a[N];
int solve(int idx) {
  if (idx > n)
    return OO;
  if (idx == n - 1)
    return 0;
  int &ret = dp[idx];
  if (~ret)
    return ret;
  ret = OO;
  for (int i = 1; i <= k; i++) {
    ret = min(ret, solve(idx + i) + abs(a[idx + i] - a[idx]));
  }
  return ret;
}
int dp2[N][K];
int solve_rec(int idx, int k_so_far) {
  if (idx == n - 1)
    return abs(a[idx] - a[idx - k_so_far]);
  if (k_so_far == k)
    return solve_rec(idx, 0) + abs(a[idx] - a[idx - k_so_far]);
  int &ret = dp2[idx][k_so_far];
  if (~ret)
    return ret;
  ret = solve_rec(idx + 1, k_so_far + 1);
  if (k_so_far > 0)
    ret = min(ret, solve_rec(idx, 0) + abs(a[idx] - a[idx - k_so_far]));
  return ret;
}
int main() {
#ifdef LOCAL_PROJECT
  freopen("in.txt", "r", stdin);
#endif
  scanf("%d", &n);
  scanf("%d", &k);
  memset(dp, -1, sizeof(dp));
  memset(dp2, -1, sizeof(dp2));
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  // printf("%d\n",solve(0));
  printf("%d\n", solve_rec(0, 0));
  return 0;
}
Problem p03161 (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 File                                                                   \
  freopen("in.in", "r", stdin);                                                \
  freopen("out.out", "w", stdout)
#define ll long long
ll a[(int)1e5 + 10] = {0};
ll dp[(int)1e5 + 10];
ll n, k;
ll solve(int i) {
  if (i + 1 == n)
    return 0;
  if (i > n)
    return 1e9;
  ll &re = dp[i];
  if (re != -1)
    return re;
  for (int j = 1; j <= k; j++)
    re = min(abs(a[i] - a[i + j]) + solve(i + j), re == -1 ? (int)1e9 : re);
  return re;
}
int main() {
  fastIO;
  // File;
  int _(1);
  // cin >> _;
  while (_--) {
    memset(dp, -1, sizeof dp);
    cin >> n >> k;
    memset(a, 0, sizeof a);
    for (int i = 0; i < n; i++)
      cin >> a[i];
    cout << solve(0) << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define File                                                                   \
  freopen("in.in", "r", stdin);                                                \
  freopen("out.out", "w", stdout)
#define ll long long
ll a[(int)1e5 + 200] = {0};
ll dp[(int)1e5 + 200];
ll n, k;
ll solve(int i) {
  if (i + 1 == n)
    return 0;
  if (i > n)
    return 1e9;
  ll &re = dp[i];
  if (re != -1)
    return re;
  for (int j = 1; j <= k; j++)
    re = min(abs(a[i] - a[i + j]) + solve(i + j), re == -1 ? (int)1e9 : re);
  return re;
}
int main() {
  fastIO;
  // File;
  int _(1);
  // cin >> _;
  while (_--) {
    memset(dp, -1, sizeof dp);
    cin >> n >> k;
    memset(a, 0, sizeof a);
    for (int i = 0; i < n; i++)
      cin >> a[i];
    cout << solve(0) << endl;
  }
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long int
#define frwd(i, a, b, j) for (int i = a; i < b; i += j)
#define rev(i, a, b, j) for (int i = a; i >= b; i -= j)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define tr(container, it)                                                      \
  for (__typeof(container.begin()) it = container.begin();                     \
       it != container.end(); it++)
#define endl '\n'
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define pi pair<int, int>
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define ub upper_bound
#define lb lower_bound
#define INF LLONG_MAX
#define mod 1000000007
using namespace std;
void solve() {
  int n, k;
  cin >> n >> k;
  int a[n];
  frwd(i, 0, n, 1) cin >> a[i];
  vector<int> dp(n + 1, INF);
  dp[0] = 0;
  dp[1] = 0;
  dp[2] = abs(a[1] - a[0]);
  frwd(i, 3, n + 1, 1) {
    frwd(j, 1, k + 1, 1) {
      dp[i] = min(dp[i], dp[i - j] + abs(a[i - 1] - a[i - 1 - j]));
    }
  }
  // frwd(i,0,n+1,1) cout<<dp[i]<<endl;
  cout << dp[n];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  /*freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);*/
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long int
#define frwd(i, a, b, j) for (int i = a; i < b; i += j)
#define rev(i, a, b, j) for (int i = a; i >= b; i -= j)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define tr(container, it)                                                      \
  for (__typeof(container.begin()) it = container.begin();                     \
       it != container.end(); it++)
#define endl '\n'
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define pi pair<int, int>
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define ub upper_bound
#define lb lower_bound
#define INF LLONG_MAX
#define mod 1000000007
using namespace std;
void solve() {
  int n, k;
  cin >> n >> k;
  int a[n];
  frwd(i, 0, n, 1) cin >> a[i];
  vector<int> dp(n + 1, INF);
  dp[0] = 0;
  dp[1] = 0;
  dp[2] = abs(a[1] - a[0]);
  frwd(i, 3, n + 1, 1) {
    frwd(j, 1, k + 1, 1) {
      if (j < i)
        dp[i] = min(dp[i], dp[i - j] + abs(a[i - 1] - a[i - 1 - j]));
    }
  }
  // frwd(i,0,n+1,1) cout<<dp[i]<<endl;
  cout << dp[n];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  /*freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);*/
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
    cout << endl;
  }
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define inf 0x7fffffff
#define ll long long
#define REP(i, a, b) for (int i = a; i <= b; i++)
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // Code Starts...
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  vector<ll> dp(n, inf); // it stores the min cost incurred so far...
  dp[0] = 0;             // cost at stone 1 is 0...
  for (int i = 1; i < n; i++) {
    for (int j = 1; j <= k; j++)
      dp[i] = min(dp[j], dp[i - j] + abs(v[i] - v[i - j]));
  }
  cout << dp[n - 1] << endl;
  // Code Ends...
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define inf 0x7fffffff
#define ll long long
#define REP(i, a, b) for (int i = a; i <= b; i++)
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // Code Starts...
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  vector<ll> dp(n, inf); // it stores the min cost incurred so far...
  dp[0] = 0;             // cost at stone 1 is 0...
  for (int i = 1; i < n; i++) {
    for (int j = 1; j <= k; j++)
      if (i >= j)
        dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j]));
  }
  cout << dp[n - 1] << endl;
  // Code Ends...
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
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 int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;
ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
ll lcm(ll x, ll y) {
  ll g = gcd(x, y);
  return x / g * y;
}

using Graph = vector<vector<int>>;
Graph G;

int main() {
  // input
  ll n, k;
  cin >> n >> k;
  vector<ll> h(n);
  rep(i, n) cin >> h[i];

  // initialization
  ll dp[100010] = {};
  rep(i, 100010) dp[i] = INFL;
  dp[0] = 0;

  // compute
  rep(i, n) {
    for (int j = 1; j <= k; j++) {
      dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
    }
  }

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


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
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 int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;
ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
ll lcm(ll x, ll y) {
  ll g = gcd(x, y);
  return x / g * y;
}

using Graph = vector<vector<int>>;
Graph G;

int main() {
  // input
  ll n, k;
  cin >> n >> k;
  vector<ll> h(n);
  rep(i, n) cin >> h[i];

  // initialization
  ll dp[100010] = {};
  rep(i, 100010) dp[i] = INFL;
  dp[0] = 0;

  // compute
  rep(i, n) {
    for (int j = 1; j <= k; j++) {
      if (i + j < n)
        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
    }
  }

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

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

using namespace std;

#define int long long

signed main() {
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);

  int n, k, temp;
  cin >> n >> k;
  vector<int> vec(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> vec[i];
  vector<int> dp(n + 3, 1e12);
  dp[1] = 0;
  dp[2] = abs(vec[2] - vec[1]);
  for (int i = 3; i <= n; i++) {
    for (int j = 1; j <= k; j++)
      dp[i] = min(dp[i - j] + abs(vec[i - j] - vec[i]), dp[i]);
  }

  cout << dp[n] << endl;

  return 0;
}


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

using namespace std;

#define int long long

signed main() {
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);

  int n, k, temp;
  cin >> n >> k;
  vector<int> vec(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> vec[i];
  vector<int> dp(n + 3, 1e12);
  dp[1] = 0;
  dp[2] = abs(vec[2] - vec[1]);
  for (int i = 3; i <= n; i++) {
    for (int j = 1; j <= k && i - j > 0; j++)
      dp[i] = min(dp[i - j] + abs(vec[i - j] - vec[i]), 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;
  cin >> n >> k;
  int a[n];
  long long int dp[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    dp[i] = 1000000000000000;
  }

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

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int a[400000];
  long long int dp[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    dp[i] = 1000000000000000;
  }

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

  for (int i = 2; i < k; i++) {
    for (int j = 0; j < i; j++) {
      dp[i] = min(dp[j] + abs(a[i] - a[j]), dp[i]);
    }
  }
  for (int i = k; i < n; i++) {
    for (int j = i - k; j < i; j++) {
      dp[i] = min(dp[j] + abs(a[i] - a[j]), dp[i]);
    }
  }
  cout << dp[n - 1];
}
Problem p03161 (C++)

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

int main() {
  int n, k;
  cin >> n >> k;
  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++) {
    dp[i] = dp[i - 1] + abs(v[i] - v[i - 1]);
    for (int j = 2; j <= k; j++) {
      dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j]));
    }
  }
  // dp[i]=min(dp[i-2]+abs(v[i]-v[i-2]),dp[i-1]+abs(v[i]-v[i-1]));
  cout << dp[n - 1] << endl;
  return 0;
}

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

int main() {
  int n, k;
  cin >> n >> k;
  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++) {
    dp[i] = dp[i - 1] + abs(v[i] - v[i - 1]);
    for (int j = 2; j <= k && i - j >= 0; j++) {
      dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j]));
    }
  }
  // dp[i]=min(dp[i-2]+abs(v[i]-v[i-2]),dp[i-1]+abs(v[i]-v[i-1]));
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

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

#define FASTIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ll long long int
#define ld long double
#define vll vector<ll>
#define pl pair<ll, ll>
#define vl(p) vector<p>
#define W while
#define For(i, s, x) for (i = s; i < x; i++)
#define Fore(i, s, x) for (i = s; i <= x; i++)
#define FOR(i, x) for (i = 0; i < x; i++)
#define Rof(i, s, x) for (i = s; i >= x; i--)
#define all(v) v.begin(), v.end()
#define it(r, v) for (auto r = v.begin(); r != v.end(); r++)
#define pb push_back
#define in insert
#define sz size()
#define F first
#define S second
#define nl cout << "\n"
#define pr(a) cout << a << " "
#define pr2(a, b) cout << a << " " << b << "\n"
#define pr3(a, b, c) cout << a << " " << b << " " << c << "\n"
#define mod 1000000007

int main() {
  FASTIO;
  ll t = 1, i, j, k, l;
  // cin>>t;
  W(t--) {
    ll n, k;
    cin >> n >> k;
    ll a[n], res[n], temp[k];
    FOR(i, n) cin >> a[i];
    res[0] = 0;
    Fore(i, 1, k) res[i] = abs(a[i] - a[0]);
    For(i, k + 1, n) {
      FOR(j, k) temp[j] = abs(a[i] - a[i - j - 1]) + res[i - j - 1];
      res[i] = *min_element(temp, temp + k);
    }
    pr(res[n - 1]);
    nl;
  }
  return 0;
}


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

#define FASTIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ll long long int
#define ld long double
#define vll vector<ll>
#define pl pair<ll, ll>
#define vl(p) vector<p>
#define W while
#define For(i, s, x) for (i = s; i < x; i++)
#define Fore(i, s, x) for (i = s; i <= x; i++)
#define FOR(i, x) for (i = 0; i < x; i++)
#define Rof(i, s, x) for (i = s; i >= x; i--)
#define all(v) v.begin(), v.end()
#define it(r, v) for (auto r = v.begin(); r != v.end(); r++)
#define pb push_back
#define in insert
#define sz size()
#define F first
#define S second
#define nl cout << "\n"
#define pr(a) cout << a << " "
#define pr2(a, b) cout << a << " " << b << "\n"
#define pr3(a, b, c) cout << a << " " << b << " " << c << "\n"
#define mod 1000000007

int main() {
  FASTIO;
  ll t = 1, i, j, k, l;
  // cin>>t;
  W(t--) {
    ll n, k;
    cin >> n >> k;
    ll a[n], res[n], temp[k];
    FOR(i, n) cin >> a[i];
    if (k >= n - 1) {
      pr(a[n - 1] - a[0]);
      continue;
    }
    Fore(i, 1, k) res[i] = abs(a[i] - a[0]);
    For(i, k + 1, n) {
      FOR(j, k) temp[j] = abs(a[i] - a[i - j - 1]) + res[i - j - 1];
      res[i] = *min_element(temp, temp + k);
    }
    pr(res[n - 1]);
    nl;
  }
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int dp[n];
  dp[0] = 0;
  for (int i = 1; i < k; i++) {
    int m = INT_MAX;
    for (int j = 0; j < i; j++) {
      m = min(dp[j] + abs(a[j] - a[i]), m);
    }
    dp[i] = m;
  }
  for (int i = k; i < n; i++) {
    int m = INT_MAX;
    for (int j = i - 1; i - j <= k && j >= 0; j--) {
      m = min(dp[j] + abs(a[j] - a[i]), m);
    }
    dp[i] = m;
  }
  cout << dp[n - 1];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int dp[n];
  dp[0] = 0;
  for (int i = 1; i < k && i < n; i++) {
    int m = INT_MAX;
    for (int j = 0; j < i; j++) {
      m = min(dp[j] + abs(a[j] - a[i]), m);
    }
    dp[i] = m;
  }
  for (int i = k; i < n; i++) {
    int m = INT_MAX;
    for (int j = i - 1; i - j <= k && j >= 0; j--) {
      m = min(dp[j] + abs(a[j] - a[i]), m);
    }
    dp[i] = m;
  }
  cout << dp[n - 1];
  return 0;
}
Problem p03161 (C++)

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

#define ll long long
#define ff first
#define ss second
#define pb push_back
#define N (ll)(1e7 + 5)
#define mod (ll)(1e9 + 7)

using namespace std;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> dp(n), v(n);
  for (auto &x : v)
    cin >> x;
  dp[1] = abs(v[1] - v[0]);
  for (ll i = 2; i < n; i++) {
    dp[i] = abs(v[i] - v[i - 1]) + dp[i - 1];
    for (ll j = 2; j <= k; j++)
      dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j]));
  }
  cout << dp[n - 1] << endl;
  return 0;
}


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

#define ll long long
#define ff first
#define ss second
#define pb push_back
#define N (ll)(1e7 + 5)
#define mod (ll)(1e9 + 7)

using namespace std;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> dp(n), v(n);
  for (auto &x : v)
    cin >> x;
  dp[1] = abs(v[1] - v[0]);
  for (ll i = 2; i < n; i++) {
    dp[i] = abs(v[i] - v[i - 1]) + dp[i - 1];
    for (ll j = 2; j <= min(k, i); j++)
      dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j]));
  }
  cout << dp[n - 1] << endl;
  return 0;
}