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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define range(i, l, h) for (int i = l; i < h; i++)
#define endl '\n'
#define I INT_MAX
#define L INT_MIN
#define pb push_back
#define ll long long
#define ull unsigned long long
#define vi vector<int>
using namespace std;
using std::ios;
constexpr int MOD = 1e9 + 7;
constexpr int mod = 998244353;

ll cost[100005];

int main() {
  fastio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  memset(cost, 0, sizeof cost);
  int n, k;
  cin >> n >> k;
  vi a(n + 1);
  range(i, 1, n + 1) cin >> a[i];

  cost[1] = 0;
  range(i, 1, k + 1) {
    cost[i] = abs(a[i] - a[1]);
    range(j, 1, i) {
      cost[i] = min(cost[i], cost[i - j] + abs(a[i - j] - a[i]));
    }
    // cost[t]
  }
  range(i, k + 1, n + 1) {
    cost[i] = I;
    range(j, 1, k + 1) {
      if (i - j > 0)
        cost[i] = min(cost[i], cost[i - j] + abs(a[i - j] - a[i]));
    }
  }

  cout << cost[n];
}

Fixed submission:
#include <bits/stdc++.h>
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define range(i, l, h) for (int i = l; i < h; i++)
#define endl '\n'
#define I INT_MAX
#define L INT_MIN
#define pb push_back
#define ll long long
#define ull unsigned long long
#define vi vector<int>
using namespace std;
using std::ios;
constexpr int MOD = 1e9 + 7;
constexpr int mod = 998244353;

ll cost[100005];

int main() {
  fastio;
  memset(cost, 0, sizeof cost);
  int n, k;
  cin >> n >> k;
  vi a(n + 1);
  range(i, 1, n + 1) cin >> a[i];

  cost[1] = 0;
  range(i, 1, k + 1) {
    cost[i] = abs(a[i] - a[1]);
    range(j, 1, i) {
      cost[i] = min(cost[i], cost[i - j] + abs(a[i - j] - a[i]));
    }
    // cost[t]
  }
  range(i, k + 1, n + 1) {
    cost[i] = I;
    range(j, 1, k + 1) {
      if (i - j > 0)
        cost[i] = min(cost[i], cost[i - j] + abs(a[i - j] - a[i]));
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define un unordered_map
#define us unordered_set
#define ll long long
#define l long
using namespace std;
const int mod = 1e9 + 7;
const int inf = 1e9 + 3;
int main() {

  l n, k;
  cin >> n >> k;
  vector<l> v(n);
  for (l i = 0; i < n; i++) {
    cin >> v[i];
  }
  l dp[n];
  dp[0] = 0;

  for (l i = 1; i < n; i++) {
    l mini = inf;
    for (int j = 1; j <= k; j++) {

      mini = min((dp[i - j] + abs(v[i] - v[i - j])), mini);
    }
    dp[i] = mini;
  }

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


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define un unordered_map
#define us unordered_set
#define ll long long
#define l long
using namespace std;
const int mod = 1e9 + 7;
const int inf = 1e9 + 3;
int main() {

  l n, k;
  cin >> n >> k;
  vector<l> v(n);
  for (l i = 0; i < n; i++) {
    cin >> v[i];
  }
  l dp[n];
  dp[0] = 0;

  for (l i = 1; i < n; i++) {
    l mini = inf;
    for (int j = 1; j <= k; j++) {
      if (i - j < 0)
        break;

      mini = min((dp[i - j] + abs(v[i] - v[i - j])), mini);
    }
    dp[i] = mini;
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define mod 998244353
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
  // online submission
#endif
  IOS int n, k;
  cin >> n >> k;
  vector<ll> cost;
  vector<ll> ans(n, 0);
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    cost.pb(x);
  }
  ans[1] = abs(cost[1] - cost[0]);
  for (int i = 2; i < n; i++) {
    ll mini = INT_MAX;
    for (int j = 1; j <= k; j++) {
      mini = min(mini, ans[i - j] + abs(cost[i] - cost[i - j]));
    }
    ans[i] = mini;
  }
  cout << ans[n - 1];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define mod 998244353
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
  // online submission
#endif
  IOS int n, k;
  cin >> n >> k;
  vector<ll> cost;
  vector<ll> ans(n, 0);
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    cost.pb(x);
  }
  ans[1] = abs(cost[1] - cost[0]);
  for (int i = 2; i < n; i++) {
    ll mini = INT_MAX;
    for (int j = 1; j <= k && i - j >= 0; j++) {
      mini = min(mini, ans[i - j] + abs(cost[i] - cost[i - j]));
    }
    ans[i] = mini;
  }
  cout << ans[n - 1];
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <limits.h>
using namespace std;
int dp[100004];
int k;
int mincost(int n, int height[]) {
  if (n == 1 || n == 0)
    return 0;
  if (dp[n] != -1)
    return dp[n];
  int cost1 = INT_MAX;
  for (int i = 1; i <= k; i++) {
    if (n - i - 1 >= 0 && n - i >= 0)
      cost1 = min(cost1, abs(height[n - 1] - height[n - i - 1]) +
                             mincost(n - i, height));
  }
  return dp[n] = cost1;
}
int main() {
  int n;
  cin >> n;
  cin >> k;
  int height[1000];
  for (int i = 0; i < n; i++)
    cin >> height[i];
  for (int i = 0; i < 100004; i++)
    dp[i] = -1;
  cout << mincost(n, height);
}

Fixed submission:
#include <iostream>
#include <limits.h>
using namespace std;
int dp[100004];
int k;
int mincost(int n, int height[]) {
  if (n == 1 || n == 0)
    return 0;
  if (dp[n] != -1)
    return dp[n];
  int cost1 = INT_MAX;
  for (int i = 1; i <= k; i++) {
    if (n - i - 1 >= 0 && n - i >= 0)
      cost1 = min(cost1, abs(height[n - 1] - height[n - i - 1]) +
                             mincost(n - i, height));
  }
  return dp[n] = cost1;
}
int main() {
  int n;
  cin >> n;
  cin >> k;
  int height[100004];
  for (int i = 0; i < n; i++)
    cin >> height[i];
  for (int i = 0; i < 100004; i++)
    dp[i] = -1;
  cout << mincost(n, height);
}
Problem p03161 (C++)

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

#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ms memset
// #define MAXN 10005
// #define MAXM 100005
#define pi 3.14159265359

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef priority_queue<int> pqi;
typedef priority_queue<pii, vpii, greater<pii>> dij;

int N, K;
int H[100005];
int memo[100005];

int solve(int x) {

  if (x == N - 1)
    return 0;

  if (x >= N)
    return INF;

  int &state = memo[x];
  if (state != -1)
    return state;

  int minimo = INF;
  for (int i = 1; i <= K; i++) {
    minimo = min(abs(H[x] - H[x + i]) + solve(x + i), minimo);
  }

  return minimo;
}

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

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

  ms(memo, -1, sizeof memo);

  cout << solve(0) << "\n";

  return 0;
}

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

#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ms memset
// #define MAXN 10005
// #define MAXM 100005
#define pi 3.14159265359

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef priority_queue<int> pqi;
typedef priority_queue<pii, vpii, greater<pii>> dij;

int N, K;
int H[100005];
int memo[100005];

int solve(int x) {

  if (x == N - 1)
    return 0;

  if (x >= N)
    return INF;

  int &state = memo[x];
  if (state != -1)
    return state;

  int minimo = INF;
  for (int i = 1; i <= K; i++) {
    minimo = min(abs(H[x] - H[x + i]) + solve(x + i), minimo);
  }

  return state = minimo;
}

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

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

  ms(memo, -1, sizeof memo);

  cout << solve(0) << "\n";

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define INF 1e9 + 5

using namespace std;

int get_first_negative(vector<int> &vt) {
  int low = 0, high = vt.size() - 1;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (vt[mid] < 0 && vt[max(mid - 1, 0)] >= 0) {
      return mid;
    } else if (vt[mid] >= 0) {
      low = mid + 1;
    } else if (vt[max(mid - 1, 0)] < 0) {
      high = mid - 1;
    }
  }
  return -1;
}

int get_first_zero(vector<int> &vt) {
  int low = 0, high = vt.size() - 1;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (vt[mid] == 0 && vt[max(mid - 1, 0)] == 1) {
      return mid;
    } else if (vt[mid] == 1) {
      low = mid + 1;
    } else if (vt[mid] == 0) {
      high = mid - 1;
    }
  }
  return -1;
}

int min_jumps(vector<int> &stones, int n, int k) {
  vector<int> dp(n, 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(stones[i] - stones[min(i + j, n - 1)]));
    }
  }
  return dp[n - 1];
}

int main() {
  FASTIO
  int n, k;
  cin >> n;
  cin >> k;
  vector<int> vt(n);
  for (int &elem : vt) {
    cin >> elem;
  }
  cout << min_jumps(vt, vt.size(), k);
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define INF 1e9 + 5

using namespace std;

int get_first_negative(vector<int> &vt) {
  int low = 0, high = vt.size() - 1;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (vt[mid] < 0 && vt[max(mid - 1, 0)] >= 0) {
      return mid;
    } else if (vt[mid] >= 0) {
      low = mid + 1;
    } else if (vt[max(mid - 1, 0)] < 0) {
      high = mid - 1;
    }
  }
  return -1;
}

int get_first_zero(vector<int> &vt) {
  int low = 0, high = vt.size() - 1;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (vt[mid] == 0 && vt[max(mid - 1, 0)] == 1) {
      return mid;
    } else if (vt[mid] == 1) {
      low = mid + 1;
    } else if (vt[mid] == 0) {
      high = mid - 1;
    }
  }
  return -1;
}

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

int main() {
  FASTIO
  int n, k;
  cin >> n;
  cin >> k;
  vector<int> vt(n);
  for (int &elem : vt) {
    cin >> elem;
  }
  cout << min_jumps(vt, vt.size(), k);
  return 0;
}
Problem p03161 (C++)

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

using namespace std;

typedef long long ll;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long LINF = 1e18;

int main() {
  int N, K;
  cin >> N >> K;
  ll h[100000];
  for (int i = 0; i < N; ++i) {
    cin >> h[i];
  }

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

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

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

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

using namespace std;

typedef long long ll;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long LINF = 1e18;

int main() {
  int N, K;
  cin >> N >> K;
  ll h[100000];
  for (int i = 0; i < N; ++i) {
    cin >> h[i];
  }

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

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

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

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

const long long INF = 1LL << 60;

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

  long long 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) {
      chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
    }
  }

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

  return 0;
}


Fixed submission:
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
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;
}

const long long INF = 1LL << 60;

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

  long long 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) {
      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 <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

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

  int n;
  cin >> n;
  vector<int> ans(n);

  int k;
  cin >> k;

  vector<int> r(n);
  int e;
  cin >> e;
  r[0] = e;
  ans[0] = 0;
  for (int i = 1; i < k; i++) {
    cin >> e;
    int add = INT_MAX;
    for (int j = 0; j < i; j++) {
      if (ans[j] + abs(e - r[j]) < add) {
        add = ans[j] + abs(e - r[j]);
      }
    }
    r[i] = e;
    ans[i] = add;
  }

  for (int i = k; i < n; i++) {
    cin >> e;
    r[i] = e;
    int add = INT_MAX;
    for (int j = i - k; j < i; j++) {
      if (ans[j] + abs(e - r[j]) < add) {
        add = ans[j] + abs(e - r[j]);
      }
    }
    ans[i] = add;
  }
  cout << ans[n - 1] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

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

  int n;
  cin >> n;
  vector<int> ans(n);

  int k;
  cin >> k;

  vector<int> r(n);
  int e;
  cin >> e;
  r[0] = e;
  ans[0] = 0;
  for (int i = 1; i < k && i < n; i++) {
    cin >> e;
    int add = INT_MAX;
    for (int j = 0; j < i; j++) {
      if (ans[j] + abs(e - r[j]) < add) {
        add = ans[j] + abs(e - r[j]);
      }
    }
    r[i] = e;
    ans[i] = add;
  }

  for (int i = k; i < n; i++) {
    cin >> e;
    r[i] = e;
    int add = INT_MAX;
    for (int j = i - k; j < i; j++) {
      if (ans[j] + abs(e - r[j]) < add) {
        add = ans[j] + abs(e - r[j]);
      }
    }
    ans[i] = add;
  }
  cout << ans[n - 1] << endl;

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define REPR(i, n) for (ll i = n; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
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;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const int INF = 1e9;
const ll LLINF = 1e16;

ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<int> h(n);
  REP(i, n) cin >> h[i];
  vector<ll> cost(n, 0);
  FOR(i, 1, n) {
    cost[i] = LLINF;
    FOR(j, i - k, i) { chmin(cost[i], abs(h[i] - h[j]) + cost[j]); }
  }

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define REPR(i, n) for (ll i = n; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
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;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const int INF = 1e9;
const ll LLINF = 1e16;

ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<int> h(n);
  REP(i, n) cin >> h[i];
  vector<ll> cost(n, 0);
  FOR(i, 1, n) {
    cost[i] = LLINF;
    FOR(j, max(0ll, i - k), i) { chmin(cost[i], abs(h[i] - h[j]) + cost[j]); }
  }

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

  return 0;
}
Problem p03161 (C++)

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

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) FOR(i, 0, n - 1)
typedef long long ll;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;

void solve(long long N, ll K, vector<long long> h) {
  vector<ll> dp(N + K + 100, INF);

  // initial point
  dp[0] = 0;

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

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  cin >> N;
  long long K;
  cin >> K;
  vector<long long> h(N);
  for (ll i = 0; i < N; i++) {
    cin >> h[i];
  }
  solve(N, K, move(h));
}


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

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) FOR(i, 0, n - 1)
typedef long long ll;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;

void solve(long long N, ll K, vector<long long> h) {
  vector<ll> dp(N + K + 100, INF);

  // initial point
  dp[0] = 0;

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

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  cin >> N;
  long long K;
  cin >> K;
  vector<long long> h(N);
  for (ll i = 0; i < N; i++) {
    cin >> h[i];
  }
  solve(N, K, move(h));
}
Problem p03161 (C++)

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

#define fo(i, n) for (int i = 0; i < n; i++)

int main() {

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

  long arr[n], temp[n], mi;
  fo(i, n) cin >> arr[i];

  fo(i, n) {
    var = i - 1;
    mi = INT_MAX;
    while (var >= 0 && var >= i - k) {
      mi = min(abs(arr[i] - arr[var]) + temp[var], mi);
      var--;
    }
    temp[i] = mi == INT_MAX ? 0 : mi;
  }

  cout << temp[n - 1];
  return 1;
}

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

#define fo(i, n) for (int i = 0; i < n; i++)

int main() {

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

  long arr[n], temp[n], mi;
  fo(i, n) cin >> arr[i];

  fo(i, n) {
    var = i - 1;
    mi = INT_MAX;
    while (var >= 0 && var >= i - k) {
      mi = min(abs(arr[i] - arr[var]) + temp[var], mi);
      var--;
    }
    temp[i] = mi == INT_MAX ? 0 : mi;
  }

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

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

int main() {
  int n, k;
  cin >> n >> k;
  int h[n];
  rep(i, n) cin >> h[i];
  int dp[n];
  rep(i, n) dp[i] = 1e9;
  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:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
typedef long long ll;

int main() {
  int n, k;
  cin >> n >> k;
  int h[110000];
  rep(i, n) cin >> h[i];
  int dp[n];
  rep(i, n) dp[i] = 1e9;
  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;
int main() {
  int n, i, k, c = 0, j;
  cin >> n >> k;
  int a[n];
  long long dp[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (i = 2; i <= k; i++) {
    c = 0;
    for (j = i; j >= 1; j--) {
      if (j == i) {
        c = dp[j - 1] + abs(a[i] - a[j - 1]);
      } else {
        if (dp[j - 1] + abs(a[i] - a[j - 1]) < c) {
          c = dp[j - 1] + abs(a[i] - a[j - 1]);
        }
      }
    }
    dp[i] = c;
  }
  for (i = k + 1; i < n; i++) {
    c = 0;
    for (j = i; j > i - k; j--) {
      if (j == i) {
        c = dp[j - 1] + abs(a[i] - a[j - 1]);
      } else {
        if (dp[j - 1] + abs(a[i] - a[j - 1]) < c) {
          c = dp[j - 1] + abs(a[i] - a[j - 1]);
        }
      }
    }
    dp[i] = c;
  }
  cout << dp[n - 1];
  return 0;
}


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

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long
int32_t main() {
  IOS
      // #ifndef ONLINE_JUDGE
      // freopen("input.txt", "r", stdin);
      // freopen("output.txt", "w", stdout);
      // #endif
      int n,
      k;
  cin >> n >> k;
  int a[n], dp[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  dp[0] = 0;
  for (int i = 1; i < n; i++)
    dp[i] = INT_MAX;
  for (int i = 1; i < n; i++)
    for (int j = 1; j <= min(k, i); j++)
      dp[i] = min(dp[i], dp[i - j] + abs(a[i] - a[i - j]));
  cout << dp[n - 1];
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
const int INF = 1e9 + 7;
using namespace std;
ll dp[100005];
ll solve(int n, int k, vector<int> &h) {
  dp[1] = 0;

  for (int i = 2; i <= n; i++) {
    ll ans = INF;
    for (int j = 1; j <= k; j++) {
      ll tmp = INF;

      if (i - j >= 1) {
        tmp = abs(h[i] - h[i - j]) + dp[i - j];
        ans = min(tmp, ans);
        dp[i] = ans;
      } else
        break;
    }
  }
  // op1 = abs(h[st]-h[st+1]) +solve(st+1,n,h);
  // if(st+2<=n)
  // 	op2 = abs(h[st]-h[st+2])+solve(st+2,n,h);
  // cout<<op1<<" "<<op2<<endl;
  return dp[n];
}
int main() {

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<int> h;
  h.push_back(0);
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    h.push_back(tmp);
  }
  if (n == 1) {
    cout << 0;
    return 0;
  }
  memset(dp, 0, n + 1);
  cout << solve(n, k, h);
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
const int INF = 1e9 + 7;
using namespace std;
ll dp[100005];
ll solve(int n, int k, vector<int> &h) {
  dp[1] = 0;

  for (int i = 2; i <= n; i++) {
    ll ans = INF;
    for (int j = 1; j <= k; j++) {
      ll tmp = INF;

      if (i - j >= 1) {
        tmp = abs(h[i] - h[i - j]) + dp[i - j];
        ans = min(tmp, ans);
        dp[i] = ans;
      } else
        break;
    }
  }
  // op1 = abs(h[st]-h[st+1]) +solve(st+1,n,h);
  // if(st+2<=n)
  // 	op2 = abs(h[st]-h[st+2])+solve(st+2,n,h);
  // cout<<op1<<" "<<op2<<endl;
  return dp[n];
}
int main() {

  // #ifndef ONLINE_JUDGE
  //     // for getting input from input.txt
  //     freopen("input.txt", "r", stdin);
  //     // for writing output to output.txt
  //     freopen("output.txt", "w", stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<int> h;
  h.push_back(0);
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    h.push_back(tmp);
  }
  if (n == 1) {
    cout << 0;
    return 0;
  }
  memset(dp, 0, n + 1);
  cout << solve(n, k, h);
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define input_vector(v, n)                                                     \
  for (long long i = 0; i < n; i++) {                                          \
    cin >> v[i];                                                               \
  }
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define pb push_back
using namespace std;
typedef long long ll;

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


Fixed submission:
#include <bits/stdc++.h>
#define input_vector(v, n)                                                     \
  for (long long i = 0; i < n; i++) {                                          \
    cin >> v[i];                                                               \
  }
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define pb push_back
using namespace std;
typedef long long ll;

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define div1 1000000007
const long long INF = 1LL << 60;
using namespace std;
// a>b -> a==b;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
// a<b -> a==b;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main(void) {
  long long N, K, i, j, h[100001], dp[100001];

  cin >> N >> K;

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

  for (i = 1; i <= N; i++) {
    dp[i] = INF;
  }

  dp[1] = 0;

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

  cout << dp[N] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define div1 1000000007
const long long INF = 1LL << 60;
using namespace std;
// a>b -> a==b;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
// a<b -> a==b;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main(void) {
  long long N, K, i, j, h[100001], dp[100001];

  cin >> N >> K;

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

  for (i = 1; i <= N; i++) {
    dp[i] = INF;
  }

  dp[1] = 0;

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

  cout << dp[N] << endl;

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
const int inf = 0x3f3f3f3f;
using namespace std;

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

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

  vector<int> dp(n + 1, inf);
  dp[2] = abs(arr[2] - arr[1]);
  for (int i = 3; i <= n; i++) {
    for (int k = 1; k <= temp; k++) {
      dp[i] = min(dp[i - k] + abs(arr[i] - arr[i - k]), dp[i]);
    }
  }
  cout << dp[n];
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
const int inf = 0x3f3f3f3f;
using namespace std;

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

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

  vector<int> dp(n + 1, inf);
  dp[1] = 0;
  for (int i = 2; i <= n; i++) {
    for (int k = i - 1; k >= max(1, i - temp); k--) {
      dp[i] = min(dp[k] + abs(arr[i] - arr[k]), dp[i]);
    }
  }
  cout << dp[n];
  return 0;
}
Problem p03161 (C++)

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

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

  std::vector<int> heights;

  for (int height; std::cin >> height;) {
    heights.push_back(height);
  }

  std::vector<int> costs_min(1, 0);

  for (int i = 1; i < K; ++i) {
    costs_min.push_back(std::abs(heights.at(i) - heights.at(0)));
  } // K番目以下は1番目とK番目を結ぶ飛び方が少なくとも最適

  for (int i = K; i < N; ++i) {
    std::vector<int> temps;

    for (int j = 1; j < K + 1; ++j) {
      // jは段差の数 i-j  -> iの飛び方を考える．
      temps.push_back(costs_min.at(i - j) +
                      std::abs(heights.at(i) - heights.at(i - j)));
    }
    std::sort(temps.begin(), temps.end());
    costs_min.push_back(temps.at(0));
  }

  std::cout << costs_min.at(N - 1) << std::endl;
}

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

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

  std::vector<int> heights;

  for (int height; std::cin >> height;) {
    heights.push_back(height);
  }

  std::vector<int> costs_min(1, 0);

  for (int i = 1; i < std::min(K, N); ++i) {
    costs_min.push_back(std::abs(heights.at(i) - heights.at(0)));
  } // K番目以下は1番目とK番目を結ぶ飛び方が少なくとも最適

  for (int i = K; i < N; ++i) {
    std::vector<int> temps;

    for (int j = 1; j < K + 1; ++j) {
      // jは段差の数 i-j  -> iの飛び方を考える．
      temps.push_back(costs_min.at(i - j) +
                      std::abs(heights.at(i) - heights.at(i - j)));
    }
    std::sort(temps.begin(), temps.end());
    costs_min.push_back(temps.at(0));
  }

  std::cout << costs_min.at(N - 1) << std::endl;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#pragma once
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define M_PI acos(-1)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1000000007;
const int MAX_V = 201;
int now = 0;

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
struct edge {
  int t;
  ll cost;
  edge(int _t, ll _cost) : t(_t), cost(_cost) {}
};

void Solve() {
  int n, k;
  cin >> n >> k;
  ll h[100010];
  for (size_t i = 0; i < n; i++) {
    cin >> h[i];
  }
  ll dp[100010];
  for (size_t i = 0; i < n + 1000; i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  for (size_t i = 0; i < n; i++) {
    for (size_t j = 1; j <= k; j++) {
      if (i + j >= n)
        break;
      dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
    }
  }
  cout << dp[n - 1] << endl;
}
int main() {
  Solve();
  return 0;
}


Fixed submission:
#pragma once
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define M_PI acos(-1)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1000000007;
const int MAX_V = 201;
int now = 0;

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
struct edge {
  int t;
  ll cost;
  edge(int _t, ll _cost) : t(_t), cost(_cost) {}
};

void Solve() {
  int n, k;
  cin >> n >> k;
  ll h[100010];
  for (size_t i = 0; i < n; i++) {
    cin >> h[i];
  }
  ll dp[100010];
  for (size_t i = 0; i <= n; i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  for (size_t i = 0; i < n; i++) {
    for (size_t j = 1; j <= k; j++) {
      if (i + j >= n)
        break;
      dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
    }
  }
  cout << dp[n - 1] << endl;
}
int main() {
  Solve();
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#include <limits>
#include <vector>

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

  std::vector<int> h(n);
  for (auto &&hi : h)
    std::cin >> hi;

  std::vector<int> cost(n, std::numeric_limits<int>::max());
  cost[0] = 0;
  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < k; ++k) {
      const auto ii = i - j - 1;
      if (ii < 0)
        break;
      const auto c = std::abs(h[i] - h[ii]) + cost[ii];
      if (cost[i] > c)
        cost[i] = c;
    }
  }
  std::cout << cost[n - 1] << std::endl;
}

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

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

  std::vector<int> h(n);
  for (auto &&hi : h)
    std::cin >> hi;

  std::vector<int> cost(n, std::numeric_limits<int>::max());
  cost[0] = 0;
  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < k; ++j) {
      const auto ii = i - j - 1;
      if (ii < 0)
        break;
      const auto c = std::abs(h[i] - h[ii]) + cost[ii];
      if (cost[i] > c)
        cost[i] = c;
    }
  }
  std::cout << cost[n - 1] << std::endl;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll n, i, k, j;
  cin >> n >> k;
  ll a[n], dp[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  dp[0] = 0;
  // dp[1]=abs(a[1]-a[0]);
  for (i = 1; i < k; i++)
  // dp[i]=min(dp[i-1]+abs(a[i]-a[i-1]),dp[i-2]+abs(a[i]-a[i-2]));
  {
    ll m = dp[i - 1] + abs(a[i] - a[i - 1]);
    for (j = 2; j <= i; j++) {
      ll x = dp[i - j] + abs(a[i] - a[i - j]);
      if (x < m)
        m = x;
    }
    dp[i] = m;
  }
  for (i = k; i < n; i++) {
    ll m = dp[i - 1] + abs(a[i] - a[i - 1]);
    for (j = 2; j <= k; j++) {
      ll x = dp[i - j] + abs(a[i] - a[i - j]);
      if (x < m)
        m = x;
    }
    dp[i] = m;
  }
  // dp[i]=min(dp[i-1]+abs(a[i]-a[i-1]),dp[i-2]+abs(a[i]-a[i-2]));
  cout << dp[n - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll n, i, k, j;
  cin >> n >> k;
  ll a[n], dp[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  dp[0] = 0;
  // dp[1]=abs(a[1]-a[0]);
  for (i = 1; i < k; i++) {
    if (i >= n)
      break;
    ll m = dp[i - 1] + abs(a[i] - a[i - 1]);
    for (j = 2; j <= i; j++) {
      ll x = dp[i - j] + abs(a[i] - a[i - j]);
      if (x < m)
        m = x;
    }
    dp[i] = m;
  }
  for (i = k; i < n; i++) {
    ll m = dp[i - 1] + abs(a[i] - a[i - 1]);
    for (j = 2; j <= k; j++) {
      ll x = dp[i - j] + abs(a[i] - a[i - j]);
      if (x < m)
        m = x;
    }
    dp[i] = m;
  }
  // dp[i]=min(dp[i-1]+abs(a[i]-a[i-1]),dp[i-2]+abs(a[i]-a[i-2]));
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, a[100009], dp[100009], k;
ll f(int i) {
  if (i > n)
    return 1e9;
  else if (i == n)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  ll ans = f(i + 1) + abs(a[i] - a[i + 1]);
  for (int j = 2; j <= k; j++) {
    ans = min(ans, f(i + j) + abs(a[i] - a[i + j]));
  }
  return dp[i] = ans;
}
int main() {
  memset(dp, -1, sizeof dp);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  cout << f(1) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, a[200009], dp[200009], k;
ll f(int i) {
  if (i > n)
    return 1e9;
  else if (i == n)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  ll ans = f(i + 1) + abs(a[i] - a[i + 1]);
  for (int j = 2; j <= k; j++) {
    ans = min(ans, f(i + j) + abs(a[i] - a[i + j]));
  }
  return dp[i] = ans;
}
int main() {
  memset(dp, -1, sizeof dp);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  cout << f(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> h(N, 0);
  for (int i = 0; i < N; i++)
    cin >> h[i];

  vector<int> cost(N, 0);
  cost[1] = abs(h[1] - h[0]);

  for (int i = 2; i < N; i++) {
    int res = INT_MAX;
    for (int k = 1; k <= K; k++) {
      res = min(res, cost[i - k] + abs(h[i] - h[i - k]));
    }
    cost[i] = res;
  }

  cout << cost[N - 1];

  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> cost(N, 0);
  cost[1] = abs(h[1] - h[0]);

  for (int i = 2; i < N; i++) {
    int res = INT_MAX;
    for (int k = 1; k <= K and i - k >= 0; k++) {
      res = min(res, cost[i - k] + abs(h[i] - h[i - k]));
    }
    cost[i] = res;
  }

  cout << cost[N - 1];

  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, n) for (int i = a; i < n; i++)
#define pb(v) push_back(v)

int main() {
  // code

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  int a[n];

  f(i, 0, n) cin >> a[i];
  int dp[n + 3];
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (int i = 2; i < k; i++) {
    dp[i] = abs(a[i] - a[0]);
  }
  for (int i = k; i < n; i++) {
    vector<int> v;
    for (int j = i - k; j < i;
         j++) { // cout<<dp[j]<<"d"<<dp[j]+abs(a[i]-a[j])<<"\n"<<endl;
      v.pb(dp[j] + abs(a[i] - a[j]));
    }

    dp[i] = *min_element(v.begin(), v.end());
    // cout<<dp[i]<<"sa";
  }
  cout << dp[n - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, a, n) for (int i = a; i < n; i++)
#define pb(v) push_back(v)

int main() {
  // code

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  int a[n];

  f(i, 0, n) cin >> a[i];
  int dp[n + k + 1];
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (int i = 2; i < k; i++) {
    dp[i] = abs(a[i] - a[0]);
  }
  for (int i = k; i < n; i++) {
    vector<int> v;
    for (int j = i - k; j < i;
         j++) { // cout<<dp[j]<<"d"<<dp[j]+abs(a[i]-a[j])<<"\n"<<endl;
      v.pb(dp[j] + abs(a[i] - a[j]));
    }

    dp[i] = *min_element(v.begin(), v.end());
    // cout<<dp[i]<<"sa";
  }
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

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

using namespace std;

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

  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  vector<long long> dp(n, INT_MAX);
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < n; i++) {
    for (int j = i - 1; j <= k; j++) {
      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;

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

  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  vector<long long> dp(n, INT_MAX);
  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)
        break;
      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;

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

  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
  }
  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;
}