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

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define pb push_back

ll power(ll a, ll b) { // a^b
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a);
      b--;
    }
    a = (a * a);
    b >>= 1;
  }
  return res;
}
vl dp(100005, -1);
vl h(100005);
ll k;
ll jump(ll i) {
  if (dp[i] != -1)
    return dp[i];
  if (i == 1) {
    dp[1] = 0;
    return dp[i];
  }
  ll mi = jump(i - 1) + abs(h[i] - h[i - 1]);
  for (ll j = 2; j <= k; j++) {
    if (i - j < 1)
      break;
    mi = min(mi, jump(i - j) + abs(h[i] - h[i - k]));
  }
  dp[i] = mi;
  return dp[i];
}

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

  cin >> k;
  for (ll i = 1; i <= n; i++)
    cin >> h[i];
  cout << jump(n) << "\n";
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define pb push_back

ll power(ll a, ll b) { // a^b
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a);
      b--;
    }
    a = (a * a);
    b >>= 1;
  }
  return res;
}
vl dp(100005, -1);
vl h(100005);
ll k;
ll jump(ll i) {
  if (dp[i] != -1)
    return dp[i];
  if (i == 1) {
    dp[1] = 0;
    return dp[i];
  }
  ll mi = jump(i - 1) + abs(h[i] - h[i - 1]);
  for (ll j = 2; j <= k; j++) {
    if (i - j < 1)
      break;
    mi = min(mi, jump(i - j) + abs(h[i] - h[i - j]));
  }
  dp[i] = mi;
  return dp[i];
}

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

  cin >> k;
  for (ll i = 1; i <= n; i++)
    cin >> h[i];
  cout << jump(n) << "\n";
}
Problem p03161 (C++)

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

using namespace std;
using ll = long long;

using Field = vector<vector<int>>;
using Graph = vector<vector<int>>;
using VI = vector<int>;
using VC = vector<char>;
using PI = pair<int, int>;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()

const long long INF = 1LL << 60;
const int mod = 1000000007;

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

int N, K;
vector<int> h(10010);
vector<int> dp(10010, INT32_MAX);

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> N >> K;
  REP(i, N) cin >> h.at(i);

  dp.at(0) = 0;

  for (int i = 1; i < N; i++) {
    for (int j = i - K; j < i; j++) {
      if (j < 0)
        continue;
      chmin(dp.at(i), dp.at(j) + abs(h.at(j) - h.at(i)));
    }
  }

  cout << dp.at(N - 1) << endl;

  return 0;
}

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

using namespace std;
using ll = long long;

using Field = vector<vector<int>>;
using Graph = vector<vector<int>>;
using VI = vector<int>;
using VC = vector<char>;
using PI = pair<int, int>;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()

const long long INF = 1LL << 60;
const int mod = 1000000007;

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

int N, K;
vector<int> h(100010);
vector<int> dp(100010, INT32_MAX);

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> N >> K;
  REP(i, N) cin >> h.at(i);

  dp.at(0) = 0;

  for (int i = 1; i < N; i++) {
    for (int j = i - K; j < i; j++) {
      if (j < 0)
        continue;
      chmin(dp.at(i), dp.at(j) + abs(h.at(j) - h.at(i)));
    }
  }

  cout << dp.at(N - 1) << endl;

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
const int INF = 1e9 + 5;
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> c(n);
  for (int &x : c)
    cin >> x;
  vector<int> dp(n, INF);
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j <= i + k; +j) {
      if (j < n)
        dp[j] = min(dp[j], dp[i] + abs(c[i] - c[j]));
    }
  }
  cout << dp[n - 1];
}

Fixed submission:
#include <bits/stdc++.h>
const int INF = 1e9 + 5;
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> c(n);
  for (int &x : c)
    cin >> x;
  vector<int> dp(n, INF);
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j <= i + k; ++j) {
      if (j < n)
        dp[j] = min(dp[j], dp[i] + abs(c[i] - c[j]));
    }
  }
  cout << dp[n - 1];
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
/*
これを入れて実行
g++ code.cpp
./a.out
 */

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

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

const long long INF = 1LL << 60;

bool greaterSecond(const pair<int, int> &f, const pair<int, int> &s) {
  return f.second > s.second;
}

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

  vector<int> h(n);

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

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

  dp[0] = 0;

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

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


Fixed submission:
/*
これを入れて実行
g++ code.cpp
./a.out
 */

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

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

const long long INF = 1LL << 60;

bool greaterSecond(const pair<int, int> &f, const pair<int, int> &s) {
  return f.second > s.second;
}

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

  vector<int> h(n);

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

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

  dp[0] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= k; j++) {
      dp[i + j] = min(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;
typedef unsigned long long ull;
typedef long long ll;
#define tc                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ff(i, a, b) for (i = a; i < b; i++)
#define fb(i, b, a) for (i = b; i > a; i--)
#define ffi(i, a, b, c) for (i = a; i < b; i += c)
#define fbi(i, b, a, c) for (i = b; i > a; i -= c)
#define clin(s) getline(cin, s)
#define MOD 1e9 + 7
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define ub upper_bound
#define lb lower_bound
int main() {
  fio;
  long n, k, i, j;
  cin >> n >> k;
  long arr[n];
  long dp[n];
  ff(i, 0, n) { cin >> arr[i]; }
  dp[0] = 0;
  ff(i, 1, k) { dp[i] = abs(arr[0] - arr[i]); }
  ff(i, k, n) {
    // dp[i]=min(dp[i-1]+abs(arr[i]-arr[i-1]),dp[i-2]+abs(arr[i]-arr[i-2]));
    // cout<<dp[i]<<" ";
    dp[i] = LONG_MAX;
    ff(j, 1, k + 1) {
      dp[i] = min(dp[i], dp[i - j] + abs(arr[i] - arr[i - j]));
    }
  }
  // cout<<"\n";
  cout << dp[n - 1] << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define tc                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ff(i, a, b) for (i = a; i < b; i++)
#define fb(i, b, a) for (i = b; i > a; i--)
#define ffi(i, a, b, c) for (i = a; i < b; i += c)
#define fbi(i, b, a, c) for (i = b; i > a; i -= c)
#define clin(s) getline(cin, s)
#define MOD 1e9 + 7
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define ub upper_bound
#define lb lower_bound
int main() {
  fio;
  long n, k, i, j;
  cin >> n >> k;
  long arr[n];
  long dp[n];
  if (k > n)
    k = n;
  ff(i, 0, n) { cin >> arr[i]; }
  dp[0] = 0;
  ff(i, 1, k) { dp[i] = abs(arr[0] - arr[i]); }
  ff(i, k, n) {
    // dp[i]=min(dp[i-1]+abs(arr[i]-arr[i-1]),dp[i-2]+abs(arr[i]-arr[i-2]));
    // cout<<dp[i]<<" ";
    dp[i] = LONG_MAX;
    ff(j, 1, k + 1) {
      dp[i] = min(dp[i], dp[i - j] + abs(arr[i] - arr[i - j]));
    }
  }
  // cout<<"\n";
  cout << dp[n - 1] << "\n";
  return 0;
}
Problem p03161 (C++)

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

const int MOD = 1000000007;
const int INF = 1000000000;

// long long
using ll = long long;

// 出力系
#define print(x) cout << x << endl
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl

// begin() end()
#define all(x) (x).begin(), (x).end()

// for
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)

// 最大公約数
unsigned gcd(unsigned a, unsigned b) {
  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

// 最小公倍数
unsigned lcm(unsigned a, unsigned b) { return a / gcd(a, b) * b; }

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  REP(i, N) cin >> A[i];

  vector<int> 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], abs(A[i] - A[i - j]) + dp[i - j]);
    }
  }
  print(dp[N - 1]);
}


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

const int MOD = 1000000007;
const int INF = 1000000000;

// long long
using ll = long long;

// 出力系
#define print(x) cout << x << endl
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl

// begin() end()
#define all(x) (x).begin(), (x).end()

// for
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)

// 最大公約数
unsigned gcd(unsigned a, unsigned b) {
  if (a < b)
    return gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

// 最小公倍数
unsigned lcm(unsigned a, unsigned b) { return a / gcd(a, b) * b; }

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  REP(i, N) cin >> A[i];

  vector<int> dp(N, INF);
  dp[0] = 0;

  for (int i = 1; i < N; i++) {
    for (int j = 1; j <= K; j++) {
      if (i - j < 0)
        break;
      dp[i] = min(dp[i], abs(A[i] - A[i - j]) + dp[i - j]);
    }
  }
  print(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> h(N);
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }

  vector<int> dp(N, 1e9 + 7);
  dp[0] = 0;

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

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

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

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

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

  vector<int> dp(N, 1e9 + 7);
  dp[0] = 0;

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

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

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

int dp[100'000 + 5]; /// dp[i] = min cost to get from 0 to i

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

  int n, k;
  cin >> n >> k;

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

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

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

  return 0;
}


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

int dp[100'000 + 5]; /// dp[i] = min cost to get from 0 to i

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

  int n, k;
  cin >> n >> k;

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

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

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

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define FOR(i, a, b) for (ll i = (a); i < (ll)b; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1000000000000000
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;
typedef long long ll;
typedef double db;
typedef string str;
typedef pair<ll, ll> p;
const int MOD = 1000000007;

void print(const std::vector<int> &v) {
  std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
  std::cout << std::endl;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> h(N);
  REP(i, N) cin >> h[i];
  vector<ll> ans(N, INF);
  ans[0] = 0;
  for (int i = 1; i < N; i++) {
    REP(j, i) {
      if (i - j > K)
        continue;
      ll comp = ans[j] + abs(h[i] - h[j]);
      ans[i] = min(ans[i], comp);
    }
  }
  // REP(i,N) cout<<ans[i]<<endl;
  cout << ans[N - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define FOR(i, a, b) for (ll i = (a); i < (ll)b; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1000000000000000
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;
typedef long long ll;
typedef double db;
typedef string str;
typedef pair<ll, ll> p;
const int MOD = 1000000007;

void print(const std::vector<int> &v) {
  std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
  std::cout << std::endl;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> h(N);
  REP(i, N) cin >> h[i];
  vector<ll> ans(N, INF);
  ans[0] = 0;
  for (int i = 1; i < N; i++) {
    for (int j = max(i - K, 0); j < i; j++) {
      ll comp = ans[j] + abs(h[i] - h[j]);
      ans[i] = min(ans[i], comp);
    }
  }
  // REP(i,N) cout<<ans[i]<<endl;
  cout << ans[N - 1] << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/dp/tasks/dp_b

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define INF (1e9)
#define MOD (1e9 + 7)
#define LINF (1e18)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
// int gcd(long a, long b) { return b ? gcd(b, a % b) : a; }
// int lcm(long a, long b) { return a * b / gcd(a, b); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> h(n + k, INF);
  REP(i, n) cin >> h[i];
  // 入力値の足場の高さHの差分の合計を最小化する
  vector<int> dp(n + k, INF);
  dp[0] = 0;
  // 配るDP
  // for (int i = 0; i < n; i++) {
  //     for (int j = 1; j <= k; j++) {
  //         dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
  //     }
  // }

  // 貰うDP
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      dp[i] = min(dp[i], dp[i - j] + abs(h[i - j] - h[i]));
    }
  }
  cout << dp[n - 1] << endl;

  return 0;
}

Fixed submission:
// https://atcoder.jp/contests/dp/tasks/dp_b

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define INF (1e9)
#define MOD (1e9 + 7)
#define LINF (1e18)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
// int gcd(long a, long b) { return b ? gcd(b, a % b) : a; }
// int lcm(long a, long b) { return a * b / gcd(a, b); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> h(n + k, INF);
  REP(i, n) cin >> h[i];
  // 入力値の足場の高さHの差分の合計を最小化する
  vector<int> dp(n + k, INF);
  dp[0] = 0;
  // 配るDP
  // for (int i = 0; i < n; i++) {
  //     for (int j = 1; j <= k; j++) {
  //         dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
  //     }
  // }

  // 貰うDP
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= k && i - j >= 0; j++) {
      dp[i] = min(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 <algorithm>
#include <array>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int num;
  int jump;
  cin >> num >> jump;
  vector<int> v;
  int tmp;
  for (int i = 0; i < num; i++) {
    cin >> tmp;
    v.push_back(tmp);
  }
  for (int i = 0; i <= jump + 10; i++) {
    v.push_back(1000000);
  }
  array<int, 100010> ans;
  for (int i = 0; i < 100010; i++) {
    ans[i] = -1;
  }
  ans[0] = 0;
  for (int i = 0; i < num - 1; i++) {
    for (int j = 1; j <= jump; j++) {
      if (ans[i + j] == -1) {
        ans[i + j] = ans[i] + abs(v[i + j] - v[i]);
      } else {
        ans[i + j] = min(ans[i + j], ans[i] + abs(v[i + j] - v[i]));
      }
    }
  }
  cout << ans[num - 1] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int num;
  int jump;
  cin >> num >> jump;
  vector<int> v;
  int tmp;
  for (int i = 0; i < num; i++) {
    cin >> tmp;
    v.push_back(tmp);
  }
  for (int i = 0; i <= jump + 10; i++) {
    v.push_back(1000000);
  }
  array<int, 110000> ans;
  for (int i = 0; i < 100010; i++) {
    ans[i] = -1;
  }
  ans[0] = 0;
  for (int i = 0; i < num - 1; i++) {
    for (int j = 1; j <= jump; j++) {
      if (ans[i + j] == -1) {
        ans[i + j] = ans[i] + abs(v[i + j] - v[i]);
      } else {
        ans[i + j] = min(ans[i + j], ans[i] + abs(v[i + j] - v[i]));
      }
    }
  }
  cout << ans[num - 1] << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> dp(N), c(N);
  rep(i, 0, N) cin >> c[i];
  dp[0] = 0;
  rep(i, 1, K) dp[i] = abs(c[0] - c[i]);
  rep(i, K, N) {
    dp[i] = dp[i - 1] + abs(c[i - 1] - c[i]);
    rep(j, 1, K) dp[i] = min(dp[i], dp[i - j - 1] + abs(c[i - j - 1] - c[i]));
  }
  cout << dp[N - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> dp(100005), c(N);
  rep(i, 0, N) cin >> c[i];
  dp[0] = 0;
  rep(i, 1, K) dp[i] = abs(c[0] - c[i]);
  rep(i, K, N) {
    dp[i] = dp[i - 1] + abs(c[i - 1] - c[i]);
    rep(j, 1, K) dp[i] = min(dp[i], dp[i - j - 1] + abs(c[i - j - 1] - c[i]));
  }
  cout << dp[N - 1] << endl;
}
Problem p03161 (C++)

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

using namespace std;
const int INF = 1001001001;

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

  vector<int> dp(n + 1, 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) {
      dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
    }
  }
  cout << dp[n - 1] << endl;
}

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

using namespace std;
const int INF = 1001001001;

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

  vector<int> dp(n + 1, 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)
        dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
      else
        break;
    }
  }
  cout << dp[n - 1] << endl;
}
Problem p03161 (C++)

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

using ll = long long;

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> h(N);

  vector<ll> dp(10000, 1000000000);
  for (int i = 0; i < N; i++)
    cin >> h[i];

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


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

using ll = long long;

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> h(N);

  vector<ll> dp(100000, 1000000000);
  for (int i = 0; i < N; i++)
    cin >> h[i];

  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < N; i++) {
    for (int 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;
}
Problem p03161 (C++)

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int b[n];
  memset(b, 0x3f, 4 * n);
  b[0] = 0;
  for (int i = 1; i < k; i++)
    b[i] = abs(a[i] - a[0]);
  for (int i = k; i < n; i++) {
    for (int j = i - k; j < i; j++) {
      b[i] = min(b[i], b[j] + abs(a[i] - a[j]));
    }
  }
  cout << b[n - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007
#define ll long long int
#define ar array
#define mp make_pair
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int b[n];
  memset(b, 0x3f, 4 * n);
  b[0] = 0;
  for (int i = 1; i < min(k, n); i++)
    b[i] = abs(a[i] - a[0]);
  for (int i = k; i < n; i++) {
    for (int j = i - k; j < i; j++) {
      b[i] = min(b[i], b[j] + abs(a[i] - a[j]));
    }
  }
  cout << b[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(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, INF);
void solve() {
  ll n, k;
  cin >> n >> k;
  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++) {
    for (ll j = 1; j <= k && i - j >= 0; j++) {
      dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
    }
  }
  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, INF);
void solve() {
  ll n, k;
  cin >> n >> k;
  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++) {
    for (ll j = 1; j <= k && i - j >= 0; j++) {
      dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
    }
  }
  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 p03161 (C++)

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

using namespace std;
using ll = long long;

ll dp(ll i, ll k);

int n, k;
vector<ll> ans(10010, 0);
vector<ll> field(10010, 0);

int main() {

  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    cin >> field.at(i);
  }
  ans.at(0) = 0;
  ans.at(1) = abs(field.at(1) - field.at(0));
  for (int i = 2; i < n + 1; i++) {
    ans.at(i) = ans.at(i - 1) + abs(field.at(i - 1) - field.at(i));
    for (int o = 2; o < k + 1; o++) {
      if (i - o >= 0) {
        ans.at(i) =
            min(ans.at(i), ans.at(i - o) + abs(field.at(i - o) - field.at(i)));
      }
    }
  }

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


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

using namespace std;
using ll = long long;

ll dp(ll i, ll k);

int n, k;
vector<ll> ans(100010, 0);
vector<ll> field(100010, 0);

int main() {

  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    cin >> field.at(i);
  }
  ans.at(0) = 0;
  ans.at(1) = abs(field.at(1) - field.at(0));
  for (int i = 2; i < n + 1; i++) {
    ans.at(i) = ans.at(i - 1) + abs(field.at(i - 1) - field.at(i));
    for (int o = 2; o < k + 1; o++) {
      if (i - o >= 0) {
        ans.at(i) =
            min(ans.at(i), ans.at(i - o) + abs(field.at(i - o) - field.at(i)));
      }
    }
  }

  cout << ans.at(n - 1) << endl;
  return (0);
}
Problem p03161 (C++)

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

signed main() {
  int N, K;
  cin >> N >> K;
  std::vector<int> v(N);
  for (int i = 0; i < N; i++)
    cin >> v[i];
  std::vector<int> dp(N * 2, 1e9);
  dp[0] = 0;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < K; j++)
      dp[i + j + 1] = min(dp[i + j + 1], dp[i] + abs(v[i] - v[i + j + 1]));
  cout << dp[N - 1] << endl;
}


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

signed main() {
  int N, K;
  cin >> N >> K;
  std::vector<int> v(N * 2, 1e9);
  for (int i = 0; i < N; i++)
    cin >> v[i];
  std::vector<int> dp(N * 2, 1e9);
  dp[0] = 0;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < K; j++)
      dp[i + j + 1] = min(dp[i + j + 1], dp[i] + abs(v[i] - v[i + j + 1]));
  cout << dp[N - 1] << endl;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define endl '\n'
#define init(n)                                                                \
  int n;                                                                       \
  cin >> n;                                                                    \
  int a[n];                                                                    \
  rep(i, n) cin >> a[i];
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long int> vll;
typedef vector<pii> vii;
const int INF = 1e9 + 5;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int n, k;
  cin >> n >> k;
  vector<int> dp(n, INF);
  int h[n];
  rep(i, n) cin >> h[i];
  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(h[i] - h[i - j]));
    }
  }
  cout << dp[n - 1];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define endl '\n'
#define init(n)                                                                \
  int n;                                                                       \
  cin >> n;                                                                    \
  int a[n];                                                                    \
  rep(i, n) cin >> a[i];
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long int> vll;
typedef vector<pii> vii;
const int INF = 1e9 + 5;
int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int n, k;
  cin >> n >> k;
  vector<int> dp(n, INF);
  int h[n];
  rep(i, n) cin >> h[i];
  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(h[i] - h[i - j]));
    }
  }
  cout << dp[n - 1];
  return 0;
}
Problem p03161 (C++)

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

#define pb push_back
#define mp make_pair
#define loop1(x, s, e) for (int x = s; x != e; x += 1 - 2 * (s > e))
#define input_matrix(a, m, n)                                                  \
  for (int i = 0; i < m; i++)                                                  \
    for (int j = 0; j < n; j++)                                                \
      cin >> a[i][j];
#define input_array(a, n)                                                      \
  for (int i = 0; i < n; i++)                                                  \
    cin >> a[i];
#define all(x) x.begin(), x.end()
#define newl "\n"
#define tests(t)                                                               \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define speed                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n), dp(n);
  loop1(i, 0, n) dp[i] = INT_MAX;
  input_array(a, n);
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);

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

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

#define pb push_back
#define mp make_pair
#define loop1(x, s, e) for (int x = s; x != e; x += 1 - 2 * (s > e))
#define input_matrix(a, m, n)                                                  \
  for (int i = 0; i < m; i++)                                                  \
    for (int j = 0; j < n; j++)                                                \
      cin >> a[i][j];
#define input_array(a, n)                                                      \
  for (int i = 0; i < n; i++)                                                  \
    cin >> a[i];
#define all(x) x.begin(), x.end()
#define newl "\n"
#define tests(t)                                                               \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define speed                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n), dp(n);
  loop1(i, 0, n) dp[i] = INT_MAX;
  input_array(a, n);
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using indexed_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(v) ((v).begin()), ((v).end())
#define SZ(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define has(c, i) ((c).find(i) != end(c))
#define has_str(s, c) ((s).find(c) != string::npos)
typedef long long ll;
int main() {
#ifdef LOCAL
  // freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  for (int &i : h)
    cin >> i;
  vector<int> dp(n, 1e9 + 9);
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < k + i; ++j) {
      dp[j] = min(dp[j], dp[i] + abs(h[i] - h[i + 1]));
    }
  }
  cout << dp[n - 1];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using indexed_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(v) ((v).begin()), ((v).end())
#define SZ(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define has(c, i) ((c).find(i) != end(c))
#define has_str(s, c) ((s).find(c) != string::npos)
typedef long long ll;
int main() {
#ifdef LOCAL
  // freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  for (int &i : h)
    cin >> i;
  vector<int> dp(n, 1e9 + 9);
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j <= k + i; ++j) {
      dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
    }
  }
  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, k;
  cin >> n >> k;
  vector<int> h(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  vector<int> dp(n + 1, 1e9 + 5);
  dp[1] = 0;
  for (int i = 1; i < n + 1; i++) {
    for (auto j = i + 1; j < i + k + 1; i++) {
      if (j <= n) {
        dp[j] = min(dp[j], dp[i] + abs(h[j - 1] - h[i - 1]));
      }
    }
  }
  cout << dp[n];
  return 0;
}

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

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

int climb_dp(int *h, int n, int k) {
  int dp[n];

  // Initialise states
  dp[0] = 0;
  for (int j = 1; j <= k; j++)
    dp[j] = abs(h[j] - h[0]);

  // Calculate the most optimal state for i
  // building upon previous optimal states
  for (int i = 2; i < n; i++) {
    int min = dp[i - 1] + abs(h[i] - h[i - 1]);
    for (int j = 2; j <= k; j++)
      if (i - j >= 0 && dp[i - j] + abs(h[i] - h[i - j]) < min)
        min = dp[i - j] + abs(h[i] - h[i - j]);
    dp[i] = min;
  }

  return dp[n - 1];
}

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

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

  cout << climb_dp(h, n, k);
  return 0;
}

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

int climb_dp(int *h, int n, int k) {
  int dp[n];

  // Initialise states
  dp[0] = 0;
  for (int j = 1; j <= k; j++)
    dp[j] = abs(h[j] - h[0]);

  // Calculate the most optimal state for i
  // building upon previous optimal states
  for (int i = 2; i < n; i++) {
    int min = dp[i - 1] + abs(h[i] - h[i - 1]);
    for (int j = 2; j <= k; j++)
      if (i - j >= 0 && dp[i - j] + abs(h[i] - h[i - j]) < min)
        min = dp[i - j] + abs(h[i] - h[i - j]);
    dp[i] = min;
  }

  return dp[n - 1];
}

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

  if (k > n)
    k = n;

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

  cout << climb_dp(h, n, k);
  return 0;
}
Problem p03161 (C++)

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

int main() {
  int n, k;
  cin >> n;
  cin >> k;
  int h[n];
  int dp[n];
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    if (i == 0)
      dp[0] = 0;
    else
      dp[i] = INT_MAX;
  }

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

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

int main() {
  int n, k;
  cin >> n;
  cin >> k;
  int h[n];
  int dp[n];
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    if (i == 0)
      dp[0] = 0;
    else
      dp[i] = INT_MAX;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= k && i + j < n; j++) {
      dp[i + j] = min(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 INF 1e9 + 5

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> hights(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> hights[i];
  }
  vector<int> dp(n + 1, INF);
  dp[1] = 0;

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF 1e9 + 5

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> hights(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cin >> hights[i];
  }
  vector<int> dp(n + 1, INF);
  dp[1] = 0;

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

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

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

  vector<ll> v(n);
  for (ll i = 0; i < n; i++)
    cin >> v[i];

  vector<ll> ans(n);
  ans[0] = 0;

  for (ll i = 1; i < n; i++) {
    ans[i] = ans[i - 1] + abs(v[i] - v[i - 1]);
    for (ll j = 1; j <= k; j++) {
      ans[i] = min(ans[i], ans[i - j] + abs(v[i] - v[i - j]));
    }
  }

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

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

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

  vector<ll> v(n);
  for (ll i = 0; i < n; i++)
    cin >> v[i];

  vector<ll> ans(n);
  ans[0] = 0;

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define lc "\n"
#define fast_io                                                                \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(0)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
#define c(a, n)                                                                \
  for (int i = 0; i < n; i++)                                                  \
    cin >> a[i];
#define ffor(n) for (int i = 0; i < n; i++)

typedef vector<int> vi;
typedef vector<float> vf;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<long long> vll;
typedef map<string, int> msi;
typedef map<int, int> mii;
typedef unordered_map<string, int> umsi;

int32_t main() {
  fast_io;
  int n, k;
  cin >> n >> k;
  vi a(n);
  c(a, n);
  vi dp(n, INT_MAX);
  dp[0] = 0;
  if (n == 1) {
    cout << 0 << lc;
    return 0;
  }
  dp[1] = abs(a[1] - a[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(a[i] - a[i - j]));
  }
  cout << dp[n - 1] << lc;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lc "\n"
#define fast_io                                                                \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(0)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
#define c(a, n)                                                                \
  for (int i = 0; i < n; i++)                                                  \
    cin >> a[i];
#define ffor(n) for (int i = 0; i < n; i++)

typedef vector<int> vi;
typedef vector<float> vf;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<long long> vll;
typedef map<string, int> msi;
typedef map<int, int> mii;
typedef unordered_map<string, int> umsi;

int32_t main() {
  fast_io;
  int n, k;
  cin >> n >> k;
  vi a(n);
  c(a, n);
  vi dp(n, INT_MAX);
  dp[0] = 0;
  if (n == 1) {
    cout << 0 << lc;
    return 0;
  }
  dp[1] = abs(a[1] - a[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(a[i] - a[i - j]));
  }
  cout << dp[n - 1] << lc;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#pragma GCC optimize("O3,Ofast,no-stack-protector,unroll-loops,fast-math")

using namespace std;

using ll = long long;
#define all(x) begin(x), end(x)
const int MAXN = 1e5 + 10;
const int INF = 2 * 1e9;

int dp[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  for (int i{}; i < n; i++)
    cin >> h[i];
  dp[1] = abs(h[1] - h[0]);
  for (int i{2}; i < n; i++) {
    // i'ye gelmek icin olan deger
    int mindeg = INT_MAX;
    for (int j{1}; j <= k; j++) {
      mindeg = min(mindeg, dp[i - j] + abs(h[i] - h[i - j]));
    }
    dp[i] += mindeg;
  }

  cout << dp[n - 1] << "\n";
}


Fixed submission:
#include "bits/stdc++.h"
#pragma GCC optimize("O3,Ofast,no-stack-protector,unroll-loops,fast-math")

using namespace std;

using ll = long long;
#define all(x) begin(x), end(x)
const int MAXN = 1e5 + 10;
const int INF = 2 * 1e9;

int dp[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  for (int i{}; i < n; i++)
    cin >> h[i];
  dp[1] = abs(h[1] - h[0]);
  for (int i{2}; i < n; i++) {
    // i'ye gelmek icin olan deger
    int mindeg = INT_MAX;
    for (int j{1}; j <= k && i - j >= 0; j++) {
      mindeg = min(mindeg, dp[i - j] + abs(h[i] - h[i - j]));
    }
    dp[i] += mindeg;
  }

  cout << dp[n - 1] << "\n";
}