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

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

int solve(vector<int> &ht, int n, int k) {
  vector<int> dp(n, 0);
  dp[0] = 0;
  dp[1] = abs(ht[1] - ht[0]);
  int mn = INT_MAX;

  for (int i = 2; i < n; i++) {
    for (int j = i - 1; j >= i - k && j >= 0; j--) {
      mn = min(mn, (dp[j] + abs(ht[i] - ht[j])));
    }
    dp[i] = mn;
    mn = INT_MAX;
  }
  return dp[n - 1];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif

  vector<int> ht;
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    ht.push_back(a);
  }

  cout << solve(ht, n, k) << endl;
}


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

int solve(vector<int> &ht, int n, int k) {
  vector<int> dp(n, 0);
  dp[0] = 0;
  dp[1] = abs(ht[1] - ht[0]);
  int mn = INT_MAX;

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

  vector<int> ht;
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    ht.push_back(a);
  }

  cout << solve(ht, n, k) << endl;
}
Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll cost[100002], n, k;
ll dp[100002];
ll jump(ll pos) {
  if (dp[pos] != -1)
    return dp[pos];
  if (pos == n)
    return 0;
  if (pos > n)
    return INT_MAX;
  ll ans = 0, prev = INT_MAX;
  for (ll i = 1; i <= k; i++) {
    ans = (abs(cost[pos] - cost[pos + i]) + jump(pos + i));
    prev = min(prev, ans);
  }
  return prev;
}

int main() {
  cin >> n >> k;
  memset(dp, -1, sizeof(dp));
  for (int i = 1; i <= n; i++)
    cin >> cost[i];
  ll ans = jump(1);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll cost[100002], n, k;
ll dp[100002];
ll jump(ll pos) {
  if (dp[pos] != -1)
    return dp[pos];
  if (pos == n)
    return 0;
  if (pos > n)
    return INT_MAX;
  ll ans = 0, prev = INT_MAX;
  for (ll i = 1; i <= k; i++) {
    ans = (abs(cost[pos] - cost[pos + i]) + jump(pos + i));
    prev = min(prev, ans);
  }
  return dp[pos] = prev;
}

int main() {
  cin >> n >> k;
  memset(dp, -1, sizeof(dp));
  for (int i = 1; i <= n; i++)
    cin >> cost[i];
  ll ans = jump(1);
  cout << ans << endl;
  return 0;
}
Problem p03161 (C++)

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

int n, k, h[100001], f[100001];
int abs(int a) { return (a > 0) ? a : (-a); }
int way(int a) {
  int mins = 2147483647, minp = 0;
  for (int i = ((a - k > 0) ? (a - k) : 1); i < a; i++) {
    if (f[i] + abs(h[a] - h[i]) < mins) {
      mins = f[i] + abs(h[a] - h[i]);
      minp = i;
    }
  }
  return minp;
}
int main() {
  freopen("FROG2.inp", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  f[1] = 0, f[2] = abs(h[2] - h[1]);
  for (int i = 3; i <= n; i++) {
    f[i] = f[way(i)] + abs(h[i] - h[way(i)]);
  }
  cout << f[n];
  return 0;
}


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

int n, k, h[100001], f[100001];
int abs(int a) { return (a > 0) ? a : (-a); }
int way(int a) {
  int mins = 2147483647, minp = 0;
  for (int i = ((a - k > 0) ? (a - k) : 1); i < a; i++) {
    if (f[i] + abs(h[a] - h[i]) < mins) {
      mins = f[i] + abs(h[a] - h[i]);
      minp = i;
    }
  }
  return minp;
}
int main() {
  // freopen("FROG2.inp", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  f[1] = 0, f[2] = abs(h[2] - h[1]);
  for (int i = 3; i <= n; i++) {
    f[i] = f[way(i)] + abs(h[i] - h[way(i)]);
  }
  cout << f[n];
  return 0;
}
Problem p03161 (C++)

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

const long long INF = 1LL << 60; // これはなに？

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  for (int i = 0; i < N; ++i)
    cin >> h[i];
  vector<long long> dp(N);
  for (int i = 0; i < N; ++i)
    dp[i] = 100000000010; // DPテーブル

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

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

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

const long long INF = 1LL << 60; // これはなに？

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  for (int i = 0; i < N; ++i)
    cin >> h[i];
  vector<long long> dp(100000);
  for (int i = 0; i < 100000; ++i)
    dp[i] = 100000000010; // DPテーブル

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

  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;
  vector<int> dp(n, 1e9), arr(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  dp[0] = 0;
  if (n > 1)
    dp[1] = abs(arr[1] - arr[0]);
  for (int i = 2; i < n; i++)
    for (int j = 1; j <= k; j++)
      dp[i] = min(dp[i], dp[i - j] + abs(arr[i] - arr[i - j]));
  cout << dp[n - 1];
}


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

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

using namespace std;

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define euc_dis(x, y) sqrt(x *x + y * y)

#define MOD 1e9 + 7
#define sp ' '

using ll = long long;

ll mod(ll a, ll b) { return (a % b + b) % b; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void Judge(bool b) { b ? Yes() : No(); }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void JUDGE(bool b) { b ? YES() : NO(); }
ll powMod(ll b, ll e, ll m) {
  ll r = 1;
  while (e > 0) {
    if (e & 1)
      r = (r % m) * (b % m) % m;
    b = (b % m) * (b % m) % m;
    e >>= 1;
  }
  return r;
}
double distance(ll x1, ll y1, ll x2, ll y2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

template <typename T> void ppp(T n) { cout << n << endl; }

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int dp[100001];
  fill_n(dp, 100001, 1e9);

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

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

  ppp(dp[n - 1]);

  return 0;
}


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

using namespace std;

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define euc_dis(x, y) sqrt(x *x + y * y)

#define MOD 1e9 + 7
#define sp ' '

using ll = long long;

ll mod(ll a, ll b) { return (a % b + b) % b; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void Judge(bool b) { b ? Yes() : No(); }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void JUDGE(bool b) { b ? YES() : NO(); }
ll powMod(ll b, ll e, ll m) {
  ll r = 1;
  while (e > 0) {
    if (e & 1)
      r = (r % m) * (b % m) % m;
    b = (b % m) * (b % m) % m;
    e >>= 1;
  }
  return r;
}
double distance(ll x1, ll y1, ll x2, ll y2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

template <typename T> void ppp(T n) { cout << n << endl; }

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int dp[100001];
  fill_n(dp, 100001, 1e9);

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

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

  ppp(dp[n - 1]);

  return 0;
}
Problem p03161 (C++)

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

  cin >> n >> k;
  for (int i = k; i <= (n + k - 1); i++) {
    cin >> a[i];
  }
  for (int i = 0; i <= (n + k - 1); i++) {
    if (i == k)
      dp[i] = 0;
    else {
      dp[i] = OO;
    }
  }

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

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


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

  cin >> n >> k;
  for (int i = k; i <= (n + k - 1); i++) {
    cin >> a[i];
  }
  for (int i = 0; i <= (n + k - 1); i++) {
    if (i == k)
      dp[i] = 0;
    else {
      dp[i] = OO;
    }
  }

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

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

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

using namespace std;

int main(void) {
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  vector<int> dp(n);
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  if (n == 2) {
    cout << dp[1] << endl;
    return 0;
  }
  for (int i = 2; i < n; i++) {
    int tmp = dp[i - 1] + abs(h[i] - h[i - 1]);
    for (int j = 2; j <= min(n, k); j++) {
      tmp = min(tmp, dp[i - j] + abs(h[i] - h[i - j]));
    }
    dp[i] = tmp;
  }
  cout << dp.back() << endl;
  return 0;
}


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

using namespace std;

int main(void) {
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  vector<int> dp(n);
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  if (n == 2) {
    cout << dp[1] << endl;
    return 0;
  }
  for (int i = 2; i < n; i++) {
    int tmp = dp[i - 1] + abs(h[i] - h[i - 1]);
    for (int j = 2; j <= min(i, k); j++) {
      tmp = min(tmp, dp[i - j] + abs(h[i] - h[i - j]));
    }
    dp[i] = tmp;
  }
  cout << dp.back() << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
ll n, m, k = 0, l = 0, x, y;

int main() {
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  vector<ll> a(n + 2 * k, 0), dp(n + 2 * k, 0);

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

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

    for (int j = i + 1; j < n; j++)
      dp[j] = min(dp[j], dp[i] + abs(a[i] - a[j]));

    dp[i + k] = dp[i] + abs(a[i] - a[i + k]);
  }

  cout << dp[n - 1];
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
ll n, m, k = 0, l = 0, x, y;

int main() {
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  vector<ll> a(n + 2 * k, 0), dp(n + 2 * k, 0);

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

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

    for (int j = i + 1; j < i + k; j++)
      dp[j] = min(dp[j], dp[i] + abs(a[i] - a[j]));

    dp[i + k] = dp[i] + abs(a[i] - a[i + k]);
  }

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

Buggy submission (status: Time Limit Exceeded):
// #define _GLIBCXX_DEBUG//TLEの原因になるので注意！！！！！！！！！！！
#include <bits/stdc++.h>
#include <cmath>
typedef long long ll;
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.xise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// #define int long long

// cerrはコメントアウトしなくても通る//ただしTLEを引き起こすことがある

ll N, M, H, W, K, Q, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
// const ll INF = 1LL << 60;
// typedef pair<ll,ll> P;
typedef pair<int, int> P;

int h[100005];
int dp[100005];

int main() {
  int n, k;
  cin >> n >> k;
  rep(i, n + 1) { dp[i] = INF; }
  dp[1] = 0;
  rep2(i, n) {
    cin >> h[i];
    for (int j = i - k; j < n; j++) {
      if (0 < j && j < i) {
        dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]));
      }
    }
  }
  cout << dp[n] << endl;
  return 0;
}


Fixed submission:
// #define _GLIBCXX_DEBUG//TLEの原因になるので注意！！！！！！！！！！！
#include <bits/stdc++.h>
#include <cmath>
typedef long long ll;
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.xise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// #define int long long

// cerrはコメントアウトしなくても通る//ただしTLEを引き起こすことがある

ll N, M, H, W, K, Q, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
// const ll INF = 1LL << 60;
// typedef pair<ll,ll> P;
typedef pair<int, int> P;

int h[100005];
int dp[100005];

int main() {
  int n, k;
  cin >> n >> k;
  rep(i, n + 1) { dp[i] = INF; }
  dp[1] = 0;
  rep2(i, n) {
    cin >> h[i];
    for (int j = i - k; j < n && j < i; j++) {
      if (0 < j && j < i) {
        dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]));
      }
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p03161 (C++)

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

#define rep(i, a, b) for (auto(i) = a; i < b; i++)
#define list(i, N) for (auto(i) = 0; i < N; i++)

typedef long long ll;

#define int ll
#define INF 1e9 + 5
#undef int
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#define int ll
  int N;
  cin >> N;
  int K;
  cin >> K; // K = 2;
  int H[N];
  list(i, N) cin >> H[i];
  int dp[N];
  list(i, N) dp[i] = INF;
  dp[0] = 0;
  list(i, N) {
    for (int j = i + 1; j <= i + K; j++)
      dp[j] = min(dp[i] + abs(H[i] - H[j]), dp[j]);
  }
  cout << dp[N - 1];
  return 0;
}


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

#define rep(i, a, b) for (auto(i) = a; i < b; i++)
#define list(i, N) for (auto(i) = 0; i < N; i++)

typedef long long ll;

#define int ll
#define INF 1e9 + 5
#undef int
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#define int ll
  int N;
  cin >> N;
  int K;
  cin >> K; // K = 2;
  int H[N];
  list(i, N) cin >> H[i];
  int dp[N];
  list(i, N) dp[i] = INF;
  dp[0] = 0;
  list(i, N) {
    for (int j = i + 1; j <= i + K && j < N; j++)
      dp[j] = min(dp[i] + abs(H[i] - H[j]), dp[j]);
  }
  cout << dp[N - 1];
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <unordered_map>
#define M 1000000007
#define T 998244353
#define PI 3.142
#define ll long long

using namespace std;

int dpj[100001][101];
int dp[100001];

int jump(int a, int k, int h[], int n) {
  if (a == n) {
    return 0;
  }
  if (dp[a] != -1) {
    return dp[a];
  }
  int min = INT32_MAX, i, x;
  for (i = a + 1; i <= a + k && i <= n; i++) {
    if (dpj[i][k] != -1) {
      x = dpj[i][k];
    } else {
      x = jump(i, k, h, n);
      dpj[i][k] = x;
    }
    x += abs(h[i] - h[a]);
    if (min > x) {
      min = x;
    }
  }
  dp[a] = min;
  return min;
}

void ini() {
  for (int i = 0; i < 100001; i++) {
    dp[i] = -1;
    for (int j = 0; j < 101; j++) {
      dpj[i][j] = -1;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

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

  int n, k, i;
  cin >> n >> k;
  int h[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> h[i];
  }
  ini();
  cout << jump(1, k, h, n) << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <unordered_map>
#define M 1000000007
#define T 998244353
#define PI 3.142
#define ll long long

using namespace std;

int dpj[100001][101];
int dp[100001];

int jump(int a, int k, int h[], int n) {
  if (a == n) {
    return 0;
  }
  if (dp[a] != -1) {
    return dp[a];
  }
  int min = INT32_MAX, i, x;
  for (i = a + 1; i <= a + k && i <= n; i++) {
    if (dpj[i][k] != -1) {
      x = dpj[i][k];
    } else {
      x = jump(i, k, h, n);
      dpj[i][k] = x;
    }
    x += abs(h[i] - h[a]);
    if (min > x) {
      min = x;
    }
  }
  dp[a] = min;
  return min;
}

void ini() {
  for (int i = 0; i < 100001; i++) {
    dp[i] = -1;
    for (int j = 0; j < 101; j++) {
      dpj[i][j] = -1;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k, i;
  cin >> n >> k;
  int h[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> h[i];
  }
  ini();
  cout << jump(1, k, h, n) << "\n";
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int main(int, char **) {
  int n, k;
  cin >> n >> k;
  vector<int> h(n), a(n);
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  a[0] = 0;
  for (int i = 1; i < k; ++i) {
    a[i] = abs(h[i] - h[i - 1]) + a[i - 1];
    for (int j = 2; j <= i; ++j) {
      auto tmp = abs(h[i] - h[i - j]) + a[i - j];
      if (a[i] > tmp) {
        a[i] = tmp;
      }
    }
  }
  for (int i = k; i < n; ++i) {
    a[i] = abs(h[i] - h[i - 1]) + a[i - 1];
    for (int j = 2; j <= k; ++j) {
      auto tmp = abs(h[i] - h[i - j]) + a[i - j];
      if (a[i] > tmp) {
        a[i] = tmp;
      }
    }
  }
  cout << a[n - 1] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int main(int, char **) {
  int n, k;
  cin >> n >> k;
  vector<long> h(n), a(n);
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  a[0] = 0;
  for (int i = 1; i < k; ++i) {
    a[i] = abs(h[i] - h[i - 1]) + a[i - 1];
    for (int j = 2; j <= i; ++j) {
      auto tmp = abs(h[i] - h[i - j]) + a[i - j];
      if (a[i] > tmp) {
        a[i] = tmp;
      }
    }
  }
  for (int i = k; i < n; ++i) {
    a[i] = abs(h[i] - h[i - 1]) + a[i - 1];
    for (int j = 2; j <= k; ++j) {
      auto tmp = abs(h[i] - h[i - j]) + a[i - j];
      if (a[i] > tmp) {
        a[i] = tmp;
      }
    }
  }
  cout << a[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

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

using namespace std;

int main() {
  long n, k;
  cin >> n >> k;
  long a[n], cost[n];
  for (int i = 0; i < n; ++i)
    cost[i] = INT_MAX;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = 1; i <= k; ++i)
    cost[i] = abs(a[i] - a[0]);
  for (int i = 1; i < n; ++i)
    for (int j = i + 1; j <= i + k and j < n; ++j)
      cost[j] = min(cost[j], cost[i] + abs(a[j] - a[i]));
  cout << cost[n - 1];
  return 0;
}

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

using namespace std;

int main() {
  long n, k;
  cin >> n >> k;
  long a[n], cost[n];
  for (int i = 0; i < n; ++i)
    cost[i] = INT_MAX;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  cost[0] = 0;
  for (int i = 0; i < n; ++i)
    for (int j = i + 1; j <= i + k and j < n; ++j)
      cost[j] = min(cost[j], cost[i] + abs(a[j] - a[i]));
  cout << cost[n - 1];
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip> // << fixed << precision(10)
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, s, t) for (int i = s; i < t; i++)
#define REP(i, n) FOR(i, 0, n)
#define PRINT(s) cout << s << "\n"
#define SORT(A) sort(A.begin(), A.end())
#define EACHPRINT(A, T)                                                        \
  for_each(begin(A), end(A), [&](T i) { cout << i << " "; });                  \
  cout << "\n"

typedef long lint;
typedef pair<lint, lint> P;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  lint N;
  cin >> N;
  lint K;
  cin >> K;

  vector<lint> h(N);
  REP(i, N) {
    lint a;
    cin >> a;
    h[i] = a;
  }

  vector<lint> dp(N);

  for (lint i = N - K - 1; i < N; i++) {
    dp[i] = abs(h[i] - h[N - 1]);
  }
  for (lint i = N - K - 2; i >= 0; i--) {
    lint res = 10000000000;
    for (lint k = 1; k <= K; k++) {
      res = min(abs(h[i] - h[i + k]) + dp[i + k], res);
    }
    dp[i] = res;
  }

  PRINT(dp[0]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip> // << fixed << precision(10)
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, s, t) for (int i = s; i < t; i++)
#define REP(i, n) FOR(i, 0, n)
#define PRINT(s) cout << s << "\n"
#define SORT(A) sort(A.begin(), A.end())
#define EACHPRINT(A, T)                                                        \
  for_each(begin(A), end(A), [&](T i) { cout << i << " "; });                  \
  cout << "\n"

typedef long lint;
typedef pair<lint, lint> P;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  lint N;
  cin >> N;
  lint K;
  cin >> K;

  vector<lint> h(N);
  REP(i, N) {
    lint a;
    cin >> a;
    h[i] = a;
  }

  vector<lint> dp(N);

  for (int i = max(N - K - 1, (lint)0); i < N; i++) {
    dp[i] = abs(h[i] - h[N - 1]);
  }
  for (lint i = N - K - 2; i >= 0; i--) {
    lint res = 10000000000;
    for (lint k = 1; k <= K; k++) {
      res = min(abs(h[i] - h[i + k]) + dp[i + k], res);
    }
    dp[i] = res;
  }

  PRINT(dp[0]);
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define INF 1000000000
#define MOD 1000000007

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> H(N);
  for (int &h : H)
    cin >> h;

  vector<int> dp(N, 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;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define INF 1000000000
#define MOD 1000000007

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> H(N);
  for (int &h : H)
    cin >> h;

  vector<int> dp(N, 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 <= i; j++) {
      dp[i] = min(dp[i], dp[i - j] + abs(H[i] - H[i - j]));
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define inf 1e9

using namespace std;

ll min(ll a, ll b) { return (a <= b) ? a : b; }

ll find(vector<ll> &h, ll n, ll k) {
  vector<ll> dp(n + 1);
  dp[1] = 0;

  for (ll i = 2; i <= n; i++) {
    dp[i] = inf;

    for (ll j = i - 1; j >= 1 && (i - j) <= k; j--)
      dp[i] = min(dp[i], abs(h[i] - h[j]) + dp[j]);
  }
  return dp[n];
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("Input.txt", "r", stdin);
  freopen("Output.txt", "w", stdout);
#endif

  fio

      ll n,
      k;
  cin >> n >> k;

  vector<ll> h(n + 1);

  for (ll i = 1; i <= n; i++)
    cin >> h[i];

  cout << find(h, n, k) << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define inf 1e9

using namespace std;

ll min(ll a, ll b) { return (a <= b) ? a : b; }

ll find(vector<ll> &h, ll n, ll k) {
  vector<ll> dp(n + 1);
  dp[1] = 0;

  for (ll i = 2; i <= n; i++) {
    dp[i] = inf;

    for (ll j = i - 1; j >= 1 && (i - j) <= k; j--)
      dp[i] = min(dp[i], abs(h[i] - h[j]) + dp[j]);
  }
  return dp[n];
}

int main() {

  fio

      ll n,
      k;
  cin >> n >> k;

  vector<ll> h(n + 1);

  for (ll i = 1; i <= n; i++)
    cin >> h[i];

  cout << find(h, n, k) << "\n";
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long int ll;
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int h[n];
  int dp[n];
  REP(i, n) {
    scanf("%d", &h[i]);
    dp[i] = INF;
  }
  dp[0] = 0;
  REP(i, n) {
    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 <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long int ll;
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int h[n];
  int dp[n + k];
  REP(i, n) {
    scanf("%d", &h[i]);
    dp[i] = INF;
  }
  dp[0] = 0;
  REP(i, n) {
    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;

#define ll long long

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

#ifndef ONLINE_JUDGE
  freopen("frog_in.txt", "r", stdin);
#endif

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

  k = min(n, k);

  int arr[n], dp[n];

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

    if (i < k)
      dp[i] = abs(arr[0] - arr[i]);
  }

  for (int i = k; i < n; ++i) {
    int mn = INT_MAX;

    for (int j = 0; j < k; ++j)
      mn = min(mn, abs(arr[i] - arr[i - j - 1]) + dp[i - j - 1]);

    dp[i] = mn;
  }

  cout << dp[n - 1];

  return 0;
}


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

#define ll long long

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

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

  k = min(n, k);

  int arr[n], dp[n];

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

    if (i < k)
      dp[i] = abs(arr[0] - arr[i]);
  }

  for (int i = k; i < n; ++i) {
    int mn = INT_MAX;

    for (int j = 0; j < k; ++j)
      mn = min(mn, abs(arr[i] - arr[i - j - 1]) + dp[i - j - 1]);

    dp[i] = mn;
  }

  cout << dp[n - 1];

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
/*
    LEARNING DP:    FROG 2
        + Frog need to reach stone N
        + Can jump to Stone i + 1 or i + 2 when the Frog is on Stone i
        + Cost |hi - hj|
        => Imagine if the frog were at stone i + 1 or i + 2
        What is the minium cost to reach on Stone i.

        + DIFFERENT FROM FROG 1:
            + i + 1, i + 2, i + k
            => Image if the frog were at stone i + 1 or i + 2 ... i + k
            Or what if we've already known the minium cost at i - 1, i - 2 .. i
   - k. we now can calculate the solution for stone i PRE DP: +FIRST STATE: i =
   0 => cost = 0 TIME COMPLEXITY:
        + O(n * k)
*/
#include <bits/stdc++.h>
using namespace std;

#define DB(_x) cout << #_x << " is " << (_x) << "\n";
#define FOR(_a, _b) for (_a = 0; _a < _b; ++_a)
#define IOFILE

using LL = long long;
using LD = long double;
using VI = vector<int>;
using PI = pair<int, int>;

void Excalibur() {
  int n, k;
  while (cin >> n >> k) {
    VI a(n);
    for (auto &x : a)
      cin >> x;

    VI dp(n, 1e9);
    int i, j;
    dp[0] = 0;
    FOR(i, n) {
      FOR(j, k + 1) {
        if (i - j >= 0)
          dp[i] = min(dp[i], dp[i - j] + abs(a[i] - a[i - j]));
      }
    }

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

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

#ifdef IOFILE
  freopen("INPUT.in", "r", stdin);
  freopen("OUTPUT.out", "w", stdout);
#endif

  Excalibur();
  return 0;
}


Fixed submission:
/*
    LEARNING DP:    FROG 2
        + Frog need to reach stone N
        + Can jump to Stone i + 1 or i + 2 when the Frog is on Stone i
        + Cost |hi - hj|
        => Imagine if the frog were at stone i + 1 or i + 2
        What is the minium cost to reach on Stone i.

        + DIFFERENT FROM FROG 1:
            + i + 1, i + 2, i + k
            => Image if the frog were at stone i + 1 or i + 2 ... i + k
            Or what if we've already known the minium cost at i - 1, i - 2 .. i
   - k. we now can calculate the solution for stone i PRE DP: +FIRST STATE: i =
   0 => cost = 0 TIME COMPLEXITY:
        + O(n * k)
*/
#include <bits/stdc++.h>
using namespace std;

#define DB(_x) cout << #_x << " is " << (_x) << "\n";
#define FOR(_a, _b) for (_a = 0; _a < _b; ++_a)
// #define IOFILE

using LL = long long;
using LD = long double;
using VI = vector<int>;
using PI = pair<int, int>;

void Excalibur() {
  int n, k;
  while (cin >> n >> k) {
    VI a(n);
    for (auto &x : a)
      cin >> x;

    VI dp(n, 1e9);
    int i, j;
    dp[0] = 0;
    FOR(i, n) {
      FOR(j, k + 1) {
        if (i - j >= 0)
          dp[i] = min(dp[i], dp[i - j] + abs(a[i] - a[i - j]));
      }
    }

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

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

#ifdef IOFILE
  freopen("INPUT.in", "r", stdin);
  freopen("OUTPUT.out", "w", stdout);
#endif

  Excalibur();
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
long long int INF = 1e9 + 5;
int main() {
  int n;
  cin >> n;
  int k;
  cin >> k;
  vector<long long int> arr(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  vector<long long int> dp(n + 1, INF);
  dp[1] = 0;
  for (int i = 2; i <= n; i++) {
    for (int j = 1; i - j >= 1; j++) {
      if (j <= k)
        dp[i] = min(dp[i - j] + abs(arr[i] - arr[i - j]), dp[i]);
    }
    // cout<<dp[i]<<" ";
  }
  cout << dp[n];
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
long long int INF = 1e9 + 5;
int main() {
  int n;
  cin >> n;
  int k;
  cin >> k;
  vector<long long int> arr(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  vector<long long int> dp(n + 1, INF);
  dp[1] = 0;
  for (int i = 2; i <= n; i++) {
    for (int j = 1; i - j >= 1; j++) {
      if (j <= k)
        dp[i] = min(dp[i - j] + abs(arr[i] - arr[i - j]), dp[i]);
      else {
        break;
      }
    }
    // cout<<dp[i]<<" ";
  }
  cout << dp[n];
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep2(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
ll MOD = 1000000007;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> h(n);
  rep(i, n) cin >> h[i];
  vector<ll> dp(n);
  rep(i, n) dp[i] = INF;
  dp[0] = 0;
  rep(i, n) {
    rep2(j, 1, k + 1) { chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); }
  }
  cout << dp[n - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep2(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
ll MOD = 1000000007;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> h(n);
  rep(i, n) cin >> h[i];
  vector<ll> dp(n + k);
  rep(i, n) dp[i] = INF;
  dp[0] = 0;
  rep(i, n) {
    rep2(j, 1, k + 1) { chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); }
  }
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, a, b) for (ll i = a; i < b; i++)
int main() {
  ll n, k5;
  cin >> n >> k5;
  ll a[n], inf = 1000000009;
  f(i, 0, n) { cin >> a[i]; }
  ll dp[n];
  f(k1, 0, n) { dp[k1] = inf; }
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  f(j, 2, k5 + 1) {
    f(k, 0, j) { dp[j] = min(dp[j], dp[k] + abs(a[j] - a[k])); }
  }
  f(j2, k5 + 1, n) {
    f(k3, j2 - k5, j2) { dp[j2] = min(dp[j2], dp[k3] + abs(a[k3] - a[j2])); }
  }
  cout << dp[n - 1];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, a, b) for (ll i = a; i < b; i++)
int main() {
  ll n, k5;
  cin >> n >> k5;
  ll a[n], inf = 1000000009;
  f(i, 0, n) { cin >> a[i]; }
  ll dp[n];
  f(k1, 0, n) { dp[k1] = inf; }
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  if (n > k5 + 1) {
    f(j, 2, k5 + 1) {
      f(k, 0, j) { dp[j] = min(dp[j], dp[k] + abs(a[j] - a[k])); }
    }
  }
  f(j2, k5 + 1, n) {
    f(k3, j2 - k5, j2) { dp[j2] = min(dp[j2], dp[k3] + abs(a[k3] - a[j2])); }
  }
  cout << dp[n - 1];
}
Problem p03161 (C++)

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

const ll INF = 1LL << 60;
vector<ll> dp(100010, INF);
vector<ll> h(100010);
int K;

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 rec(int i) {
  if (dp[i] != INF)
    return dp[i];
  if (i == 0)
    return 0;
  ll res = INF;
  for (int j = 1; j <= K; j++) {
    if (i > j - 1)
      chmin(res, rec(i - j) + abs(h[i - j] - h[i]));
  }
  return res;
}

int main() {
  int N;
  cin >> N;
  cin >> K;
  rep(i, N) cin >> h.at(i);

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

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

const ll INF = 1LL << 60;
vector<ll> dp(100010, INF);
vector<ll> h(100010);
int K;

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 rec(int i) {
  if (dp[i] != INF)
    return dp[i];
  if (i == 0)
    return 0;
  ll res = INF;
  for (int j = 1; j <= K; j++) {
    if (i > j - 1)
      chmin(res, rec(i - j) + abs(h[i - j] - h[i]));
  }
  return res;
}

int main() {
  int N;
  cin >> N;
  cin >> K;
  rep(i, N) cin >> h.at(i);

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

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

Buggy submission (status: Time Limit Exceeded):
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author ExplodingFreeze
 */

#include <fstream>
#include <iostream>

#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#define int long long
#define f(i, a, b) for (int i = (a); i < (b); i++)
#define MOD 1000000007
using pii = std::pair<int, int>;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); <-- PUT
// IN main()/solve()!

using namespace std;

int n, k, a[100005], ans[100105] = {0};
class BFrog2 {
public:
  void solve(std::istream &in, std::ostream &out) {
    in >> n >> k;
    f(i, 0, n) {
      in >> a[i];
      ans[i] = (int)1e18;
    }
    ans[0] = 0;
    f(i, 0, n) f(j, 1, min(i + k + 1, n - i + 1)) ans[i + j] =
        min(ans[i + j], ans[i] + abs(a[i + j] - a[i]));
    out << ans[n - 1] << "\n";
  }
};

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  BFrog2 solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Fixed submission:
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author ExplodingFreeze
 */

#include <fstream>
#include <iostream>

#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#define int long long
#define f(i, a, b) for (int i = (a); i < (b); i++)
#define MOD 1000000007
using pii = std::pair<int, int>;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); <-- PUT
// IN main()/solve()!

using namespace std;

int n, k, a[100005], ans[100105] = {0};
class BFrog2 {
public:
  void solve(std::istream &in, std::ostream &out) {
    in >> n >> k;
    f(i, 0, n) {
      in >> a[i];
      ans[i] = (int)1e18;
    }
    ans[0] = 0;
    f(i, 0, n) f(j, 1, min(k + 1, n - i + 1)) ans[i + j] =
        min(ans[i + j], ans[i] + abs(a[i + j] - a[i]));
    out << ans[n - 1] << "\n";
  }
};

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  BFrog2 solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}
Problem p03161 (C++)

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

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


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

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<ll> dp(n, INT_MAX);
  dp[0] = 0;
  k = min(k, n);
  for (int i = 1; i < k; i++) {
    dp[i] = abs(v[i] - v[0]);
  }
  for (int i = k; i < n; i++) {
    for (int j = 1; j <= k; j++) {
      dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j]));
    }
  }
  cout << dp[n - 1] << endl;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = n - 1; i > m; i--)
#define pb(n) push_back(n)
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(20) << S << endl;
#define Vecpr vector<pair<lli, lli>>
#define mod 1000000007;
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int main() {
  lli A, B, N, M;
  cin >> N >> M;
  vector<int> DP(N, INF), Ct(N, 0);
  rep(i, 0, N) cin >> Ct[i];
  DP[0] = 0;
  rep(i, 0, N) rep(j, 1, M + 1)
      chmin(DP[i + j], DP[i] + abs(Ct[i] - Ct[i + j]));
  Out(DP[N - 1]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = n - 1; i > m; i--)
#define pb(n) push_back(n)
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(20) << S << endl;
#define Vecpr vector<pair<lli, lli>>
#define mod 1000000007;
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int main() {
  lli A, B, N, M;
  cin >> N >> M;
  vector<int> DP(N, INF), Ct(N, 0);
  rep(i, 0, N) cin >> Ct[i];
  DP[0] = 0;
  rep(i, 0, N) rep(j, 1, M + 1) {
    if (i + j > N - 1)
      continue;
    chmin(DP[i + j], DP[i] + abs(Ct[i] - Ct[i + j]));
  }
  Out(DP[N - 1]);
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main(void) {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> dp(10010, 1e9);
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= k; j++) {
      chmin(dp[i + j], dp[i] + abs(a[i] - a[i + j]));
    }
  }
  cout << dp[n - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main(void) {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> dp(100010, 1e9);
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= k; j++) {
      chmin(dp[i + j], dp[i] + abs(a[i] - a[i + j]));
    }
  }
  cout << dp[n - 1] << endl;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(void) {
  int n, k;
  cin >> n >> k;
  int h[n + 1], dp[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  for (int i = 2; i <= n; i++)
    dp[i] = INT_MAX;
  dp[0] = dp[1] = 0;
  for (int i = 1; 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]));
  cout << endl << dp[n];
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(void) {
  int n, k;
  cin >> n >> k;
  int h[n + 1], dp[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  for (int i = 2; i <= n; i++)
    dp[i] = INT_MAX;
  dp[0] = dp[1] = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= k; j++)
      if (i + j <= n + 1)
        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
  cout << dp[n] << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  int i, j;
  int dp[n];
  for (i = 0; i < n; i++) {
    dp[i] = 0;
    cin >> arr[i];
  }
  for (i = 1; i < n; i++) {
    dp[i] = INT_MAX;
    for (j = i - 1; j >= 0; j--) {
      int a1 = INT_MAX;
      if (i - j <= k) {
        a1 = abs(arr[i] - arr[j]) + dp[j];
      }
      dp[i] = min(dp[i], a1);
    }
  }
  cout << dp[n - 1];

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  int i, j;
  int dp[n];
  for (i = 0; i < n; i++) {
    dp[i] = 0;
    cin >> arr[i];
  }
  for (i = 1; i < n; i++) {
    dp[i] = INT_MAX;
    for (j = i - 1; j >= i - k && j >= 0; j--) {

      int a1 = abs(arr[i] - arr[j]) + dp[j];
      dp[i] = min(dp[i], a1);
    }
  }
  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;
  if (N == 1) {
    cout << 0;
    return 0;
  }
  int h[N];

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

  int val[N];
  for (int i = 0; i < N; i++) {
    val[i] = INT_MAX;
  }
  val[0] = 0;

  for (int i = 1; i < N; i++) {
    for (int j = i - 1; j >= 0 && i - j <= K; j++) {
      val[i] = min(val[i], abs(h[i] - h[j]) + val[j]);
    }
  }

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

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

using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  if (N == 1) {
    cout << 0;
    return 0;
  }
  int h[N];

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

  int val[N];
  for (int i = 0; i < N; i++) {
    val[i] = INT_MAX;
  }
  val[0] = 0;

  for (int i = 1; i < N; i++) {
    for (int j = i - 1; j >= 0 && i - j <= K; j--) {
      val[i] = min(val[i], abs(h[i] - h[j]) + val[j]);
    }
  }

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