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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const long long INF = 10000000000;
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;
}

signed main() {
  int N, K;
  cin >> N >> K;
  int h[100000 + 10];
  for (int i = 0; i < N; i++)
    cin >> h[i];
  int dp[100000 + 10];
  for (int i = 0; i < 100000 + 10; i++)
    dp[i] = INF; // 初期化の際のiの範囲は配列の要素数！
  dp[0] = 0;     // 初期タッ君
  for (int i = 0; i < N; i++) {
    for (int j = 1; j <= K; j++) {
      chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
    }
  }
  cout << dp[N - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const long long INF = 10000000000;
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;
}

signed main() {
  int N, K;
  cin >> N >> K;
  int h[100000 + 10];
  for (int i = 0; i < N; i++)
    cin >> h[i];
  int dp[100000 + 10];
  for (int i = 0; i < 100000 + 10; i++)
    dp[i] = INF; // 初期化の際のiの範囲は配列の要素数！
  dp[0] = 0;     // 初期タッ君
  for (int i = 0; i < N; i++) {
    for (int j = 1; j <= K; j++) {
      chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
      if (i + j >= N)
        break;
    }
  }
  cout << dp[N - 1] << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(v) v.begin(), v.end()
#define mp make_pair
#define ff first
#define ss second
#define MAXN 1000005
#define endl "\n"
using namespace std;
vector<ll> v;
ll dp[100003];
ll a, n, k;
ll rec(int i) {
  if (i == n - 1)
    return 0;
  ll ans = LLONG_MAX;
  for (int j = 1; j <= k && i + j < n; j++) {
    ans = min(ans, abs(v[i] - v[i + j]) + rec(i + j));
  }
  return dp[i] = ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (ll i = 0; i < n; i++) {
    cin >> a;
    v.pb(a);
  }
  memset(dp, -1, sizeof(dp));
  ll ans = rec(0);
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(v) v.begin(), v.end()
#define mp make_pair
#define ff first
#define ss second
#define MAXN 1000005
#define endl "\n"
using namespace std;
vector<ll> v;
ll dp[100003];
ll a, n, k;
ll rec(int i) {
  if (i == n - 1)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  ll ans = LLONG_MAX;
  for (int j = 1; j <= k && i + j < n; j++) {
    ans = min(ans, abs(v[i] - v[i + j]) + rec(i + j));
  }
  return dp[i] = ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (ll i = 0; i < n; i++) {
    cin >> a;
    v.pb(a);
  }
  memset(dp, -1, sizeof(dp));
  ll ans = rec(0);
  cout << ans << endl;

  return 0;
}
Problem p03161 (C++)

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

using namespace std;

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

  vector<int> input(n, 0);
  vector<int> score(n, 0);

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

  for (int i = 1; i < k; i++) {
    t = INT_MAX;
    for (int j = 1; j <= i; j++) {
      t = min(t, abs(input[i] - input[i - j]) + score[i - j]);
    }
    score[i] = t;
  }

  for (int i = k; i < n; i++) {
    t = INT_MAX;
    for (int j = 1; j <= k; j++) {
      t = min(t, abs(input[i] - input[i - j]) + score[i - j]);
    }
    score[i] = t;
  }

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

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

using namespace std;

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

  vector<int> input(n, 0);
  vector<int> score(n, 0);

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

  for (int i = 1; i < k && i < n; i++) {
    t = INT_MAX;
    for (int j = 1; j <= i; j++) {
      t = min(t, abs(input[i] - input[i - j]) + score[i - j]);
    }
    score[i] = t;
  }

  for (int i = k; i < n; i++) {
    t = INT_MAX;
    for (int j = 1; j <= k; j++) {
      t = min(t, abs(input[i] - input[i - j]) + score[i - j]);
    }
    score[i] = t;
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;
using vb = vector<bool>;
const ll oo = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define mp make_pair
#define pb push_back
#define has(c, i) ((c).find(i) != end(c))
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = ll(b) - 1; i >= a; i--)

int main() {
  cout << fixed << setprecision(8);
  map<ll, ll> cost;
  ll n, k;
  cin >> n >> k;
  vl l(n);

  FOR(i, 0, n) { cin >> l[i]; }

  cost[0] = 0;
  FOR(i, 0, n - 1) {
    FOR(j, 1, k + 1) {
      if (i + j == n)
        break;

      if (has(cost, i + j))
        cost[i + j] = min(cost[i + j], cost[i] + abs(l[i] - l[i + j]));
      else
        cost[i + j] = cost[i] + abs(l[i] - l[i + j]);
    }
  }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;
using vb = vector<bool>;
const ll oo = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define mp make_pair
#define pb push_back
#define has(c, i) ((c).find(i) != end(c))
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = ll(b) - 1; i >= a; i--)

int main() {
  cout << fixed << setprecision(8);
  unordered_map<ll, ll> cost;
  ll n, k;
  cin >> n >> k;
  vl l(n);

  FOR(i, 0, n) { cin >> l[i]; }

  cost[0] = 0;
  FOR(i, 0, n - 1) {
    FOR(j, 1, k + 1) {
      if (i + j == n)
        break;

      if (has(cost, i + j))
        cost[i + j] = min(cost[i + j], cost[i] + abs(l[i] - l[i + j]));
      else
        cost[i + j] = cost[i] + abs(l[i] - l[i + j]);
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
const long long INF = numeric_limits<long long>::max();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int h[100100];

  for (int i = 0; i < n; i++)
    cin >> h[i];
  vector<long long> cost(n, 0);
  cost[0] = 0;

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
const long long INF = numeric_limits<long long>::max();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int h[100100];

  for (int i = 0; i < n; i++)
    cin >> h[i];
  vector<long long> cost(n, 0);
  cost[0] = 0;

  for (int i = 1; i < n; i++) {
    long long tmp = INF;
    for (int j = 1; j <= k; j++) {
      if (i - j >= 0)
        tmp = min(tmp, cost[i - j] + abs(h[i] - h[i - j]));
      else
        continue;
    }
    cost[i] = tmp;
  }
  cout << cost[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

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

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


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

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

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#define ll long long
#define ull unsigned long long
#define sl(a) scanf("%lld", &a);
using namespace std;
ll dp[10005], fr[10005];
int main() {
  ll n, i, j, k, l, m, mini = 100000000000;
  cin >> n >> k;
  for (i = 1; i <= n; i++) {
    cin >> fr[i];
  }
  if (k >= n) {
    k = n - 1;
  }
  dp[1] = 0;
  for (i = 2; i <= k + 1; i++) {
    dp[i] = abs(fr[i] - fr[1]);
  }
  for (i = k + 2; i <= n; i++) {
    for (j = i - 1; j >= i - k; j--) {
      l = dp[j] + abs(fr[i] - fr[j]);
      mini = min(l, mini);
    }
    dp[i] = mini;
    mini = 100000000000;
  }
  cout << dp[n] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#define ll long long
#define ull unsigned long long
#define sl(a) scanf("%lld", &a);
using namespace std;
ll dp[100005], fr[100005];
int main() {
  ll n, i, j, k, l, m, mini = 100000000000;
  cin >> n >> k;
  for (i = 1; i <= n; i++) {
    cin >> fr[i];
  }
  if (k >= n) {
    k = n - 1;
  }
  dp[1] = 0;
  for (i = 2; i <= k + 1; i++) {
    dp[i] = abs(fr[i] - fr[1]);
  }
  for (i = k + 2; i <= n; i++) {
    for (j = i - 1; j >= i - k; j--) {
      l = dp[j] + abs(fr[i] - fr[j]);
      mini = min(l, mini);
    }
    dp[i] = mini;
    mini = 100000000000;
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p03161 (C++)

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

#define int long long

template <class T> inline bool amin(T &a, T const &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1001001001;

signed main() {

  int n;
  cin >> n;
  int k;
  cin >> k;
  vector<int> a(n);
  for (int i = 0, i_len = (int)n; i < i_len; ++i)
    cin >> a[i];

  vector<int> dp(n + 1, INF);

  dp[0] = 0;

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

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

  return 0;
}


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

#define int long long

template <class T> inline bool amin(T &a, T const &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1001001001;

signed main() {

  int n;
  cin >> n;
  int k;
  cin >> k;
  vector<int> a(n);
  for (int i = 0, i_len = (int)n; i < i_len; ++i)
    cin >> a[i];

  vector<int> dp(n + 1, INF);

  dp[0] = 0;

  for (int i = 0; i < n; ++i) {
    for (int j = 1; j <= k; ++j) {
      amin(dp[i + j], dp[i] + abs(a[i] - a[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

int const N = 1e5 + 20;
int const INF = 1e9 + 7;

ll h[N], t, memo[N], k;

int main() {

  memset(memo, INF, sizeof(memo));

  cin >> t >> k;

  vector<ll> v(k);

  for (int i = 0; i < t; ++i)
    cin >> h[i];
  int a = 1;
  for (auto &x : v) {
    x = a;
    a++;
  }
  memo[0] = 0;
  for (int i = 0; i < t; ++i)
    for (int j : v)
      if (j < t)
        memo[i + j] = min(memo[i + j], memo[i] + abs(h[i] - h[j + i]));

  cout << memo[t - 1];
  return 0;
}


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

using namespace std;

#define ll long long

int const N = 1e5 + 20;
int const INF = 1e9 + 7;

ll h[N], t, memo[N], k;

int main() {

  memset(memo, INF, sizeof(memo));

  cin >> t >> k;

  vector<ll> v(k);

  for (int i = 0; i < t; ++i)
    cin >> h[i];
  int a = 1;
  for (auto &x : v) {
    x = a;
    a++;
  }
  memo[0] = 0;
  for (int i = 0; i < t; ++i)
    for (int j : v)
      if (i + j < t)
        memo[i + j] = min(memo[i + j], memo[i] + abs(h[i] - h[j + i]));

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

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

const int INF = 1000000000;

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  vector<int> cost(n, INF);
  cost[0] = 0;
  for (int i = 1; i <= n - 1; i++) {
    int it = k;
    for (int j = 1; j <= it; j++) {
      chmin(cost[i], cost[i - j] + abs(h[i] - h[i - j]));
    }
  }
  cout << cost[n - 1] << '\n';
  return 0;
}

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

const int INF = 1000000000;

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  vector<int> cost(n, INF);
  cost[0] = 0;
  for (int i = 1; i <= n - 1; i++) {
    int it = min(i, k);
    for (int j = 1; j <= it; j++) {
      chmin(cost[i], cost[i - j] + abs(h[i] - h[i - j]));
    }
  }
  cout << cost[n - 1] << '\n';
  return 0;
}
Problem p03161 (C++)

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

using namespace std;

// ifstream fin("test.in");

// #define int_max 0x3f3f3f3f
// #define int_min -0x3f3f3f3f

int main() {
  long long int n, k;
  cin >> n >> k;
  vector<long long int> heights(n + 1, 0);
  vector<long long int> dp(n + 1, 0);
  long long int h;

  for (long long int i = 1; i <= n; i++) {
    cin >> h;
    heights.at(i) = h;
  }

  dp[1] = 0;
  dp[2] = abs(heights[2] - heights[1]);

  for (long long int i = 3; i <= n; i++) {
    long long int best = dp[i - 1] + abs(heights[i] - heights[i - 1]);

    for (long long int j = 2; j <= k && j < n; j++)
      best = min(best, dp[i - j] + abs(heights[i] - heights[i - j]));

    dp[i] = best;
  }

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

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

using namespace std;

// ifstream fin("test.in");

// #define int_max 0x3f3f3f3f
// #define int_min -0x3f3f3f3f

int main() {
  long long int n, k;
  cin >> n >> k;
  vector<long long int> heights(n + 1, 0);
  vector<long long int> dp(n + 1, 0);
  long long int h;

  for (long long int i = 1; i <= n; i++) {
    cin >> h;
    heights.at(i) = h;
  }

  dp[1] = 0;
  dp[2] = abs(heights[2] - heights[1]);

  for (long long int i = 3; i <= n; i++) {
    long long int best = dp[i - 1] + abs(heights[i] - heights[i - 1]);

    for (long long int j = 2; j <= k && j < i; j++)
      best = min(best, dp[i - j] + abs(heights[i] - heights[i - j]));

    dp[i] = best;
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define debug(x) cout << "### x is: " << x << "###" << endl
#define all(a) a.begin(), a.end()
#define tc                                                                     \
  int t;                                                                       \
  scanf("%d", &t);                                                             \
  while (t-- 0)
#define int long long
typedef vector<int> vll;
typedef vector<vll> vvll;
const int N = 2e5 + 5;

const int MOD = 1e9 + 7;

int power(int x, int p) {
  int res = 1;
  while (p) {
    if (p % 2) {
      res = (res * x) % MOD;
      --p;
    } else {
      x = (x * x) % MOD;
      p /= 2;
    }
  }
  return res;
}
int invmod(int x) { return power(x, MOD - 2); }
int dp[N];
int a[N];
int n, k;

void go(int s, int pre, int p) {
  if (s > n)
    return;
  dp[s] = min(dp[s], pre + abs(a[p] - a[s]));
  for (int i = 1; i <= k; i++) {
    go(s + i, dp[s], s);
  }
}
int32_t main() {

  cin >> n >> k;

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

  for (int i = 0; i <= n + 5; i++) {
    dp[i] = LLONG_MAX;
  }
  go(1, 0, 1);
  cout << dp[n] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define debug(x) cout << "### x is: " << x << "###" << endl
#define all(a) a.begin(), a.end()
#define tc                                                                     \
  int t;                                                                       \
  scanf("%d", &t);                                                             \
  while (t-- 0)
#define int long long
typedef vector<int> vll;
typedef vector<vll> vvll;
const int N = 2e5 + 5;

const int MOD = 1e9 + 7;

int power(int x, int p) {
  int res = 1;
  while (p) {
    if (p % 2) {
      res = (res * x) % MOD;
      --p;
    } else {
      x = (x * x) % MOD;
      p /= 2;
    }
  }
  return res;
}
int invmod(int x) { return power(x, MOD - 2); }
int dp[N];
int a[N];
int n, k;

void go(int s, int pre, int p) {
  if (s > n)
    return;
  dp[s] = min(dp[s], pre + abs(a[p] - a[s]));
  for (int i = 1; i <= k; i++) {
    go(s + i, dp[s], s);
  }
}
int32_t main() {

  cin >> n >> k;

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

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

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

typedef long long ll;
typedef vector<long long> vll;
typedef vector<int> vi;
typedef pair<int, int> pi;

#define pb push_back

#define rep(i, l, n) for (int i = (l); i < n; i++)
#define rep_cs(i, l, n) for (int i = (l); i <= n; i++)

#define rep_d(j, n) for (int j = n; j >= 0; j--)
#define rep_d_op(j, n, a) for (int j = n; j >= a; j--)

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 int MOD = 1000000007;

const vector<int> dx = {1, 0};
const vector<int> dy = {0, 1};
const ll INF = 1LL << 60;

// int max = *std::max_element(vec.begin(), vec.end());
// int sum = accumulate(list, list + size, 0);

int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }

using Graph = vector<vector<int>>;

Graph G;

vector<bool> seen;
void dfs(const Graph &G, int v) {
  seen[v] = true;

  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;
    dfs(G, next_v);
  }
}

int main() {
  ll n, k;
  cin >> n >> k;
  vll h(n);
  rep(i, 0, n) cin >> h[i];

  ll dp[100010];
  rep(i, 0, 100010) { dp[i] = INF; }

  dp[0] = 0;

  rep(i, 0, n) {
    rep_cs(j, 1, k) { chmin(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;

typedef long long ll;
typedef vector<long long> vll;
typedef vector<int> vi;
typedef pair<int, int> pi;

#define pb push_back

#define rep(i, l, n) for (int i = (l); i < n; i++)
#define rep_cs(i, l, n) for (int i = (l); i <= n; i++)

#define rep_d(j, n) for (int j = n; j >= 0; j--)
#define rep_d_op(j, n, a) for (int j = n; j >= a; j--)

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 int MOD = 1000000007;

const vector<int> dx = {1, 0};
const vector<int> dy = {0, 1};
const ll INF = 1LL << 60;

// int max = *std::max_element(vec.begin(), vec.end());
// int sum = accumulate(list, list + size, 0);

int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }

using Graph = vector<vector<int>>;

Graph G;

vector<bool> seen;
void dfs(const Graph &G, int v) {
  seen[v] = true;

  for (auto next_v : G[v]) {
    if (seen[next_v])
      continue;
    dfs(G, next_v);
  }
}

int main() {
  ll n, k;
  cin >> n >> k;
  vll h(n);
  rep(i, 0, n) cin >> h[i];

  ll dp[110000];
  rep(i, 0, 110000) { dp[i] = INF; }

  dp[0] = 0;

  rep(i, 0, n) {
    rep_cs(j, 1, k) { chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); }
  }

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

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

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

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

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

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

void solve(std::istream &in, std::ostream &out);

int main() {
#ifdef LOCAL
  freopen("../IO/a.in", "r", stdin);
//  freopen("../IO/a.in", "w", stdout);
#else
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
#endif
  solve(std::cin, std::cout);
  return 0;
}

void solve(std::istream &in, std::ostream &out) {
  int n, k;
  in >> n >> k;
  vector<int> dp(n + 1);
  vector<int> h(n);
  for (int &i : h) {
    in >> i;
  }
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < n; ++i) {
    dp[i] = numeric_limits<int>::max();
    for (int j = 1; j <= k; ++j) {
      dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
    }
  }
  out << dp[n - 1] << '\n';
}


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

void solve(std::istream &in, std::ostream &out);

int main() {
#ifdef LOCAL
  freopen("../IO/a.in", "r", stdin);
//  freopen("../IO/a.in", "w", stdout);
#else
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
#endif
  solve(std::cin, std::cout);
  return 0;
}

void solve(std::istream &in, std::ostream &out) {
  int n, k;
  in >> n >> k;
  vector<int> dp(n + 1);
  vector<int> h(n);
  for (int &i : h) {
    in >> i;
  }
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < n; ++i) {
    dp[i] = numeric_limits<int>::max();
    for (int j = 1; j <= min(k, i); ++j) {
      dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
    }
  }
  out << dp[n - 1] << '\n';
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <climits>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#define pb push_back
#define f first
#define s second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vi vector<int>
#define vll vector<ll>
#define be begin
#define en end
#define endl '\n'
#define SIZE 1000000
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
#define value_at_idx find_by_order
#define idx order_of_key
#define MOD 1000000007
#define reset(dp) memset(dp, 0, sizeof(dp))

typedef long long ll;
using namespace std;
using namespace __gnu_pbds;

int dp[SIZE];

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

  reset(dp);
  dp[1] = 0;
  int n, k;
  cin >> n >> k;
  vi v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];

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

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

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <climits>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#define pb push_back
#define f first
#define s second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vi vector<int>
#define vll vector<ll>
#define be begin
#define en end
#define endl '\n'
#define SIZE 1000000
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
#define value_at_idx find_by_order
#define idx order_of_key
#define MOD 1000000007
#define reset(dp) memset(dp, 0, sizeof(dp))

typedef long long ll;
using namespace std;
using namespace __gnu_pbds;

int dp[SIZE];

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

  reset(dp);
  dp[1] = 0;
  int n, k;
  cin >> n >> k;
  vi v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];

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

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

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define MAX 2000000000
int dp[10000];
int main() {
  int N, K;
  cin >> N >> K;
  int A[N];
  for (int i = 0; N > i; i++) {
    cin >> A[i];
  }
  for (int i = 0; N > i; i++) {
    dp[i] = MAX;
  }
  dp[0] = 0;
  for (int i = 0; N > i; i++) {
    for (int j = 0; min(K + i + 1, N) > i + j + 1; j++) {
      dp[i + j + 1] = min(dp[i + j + 1], dp[i] + abs(A[i + j + 1] - A[i]));
      // cout << i << " " << j << " " << i+j+1 << endl;
    }
  }
  cout << dp[N - 1] << endl;
}

Fixed submission:
#include <iostream>
using namespace std;
#define MAX 2000000000
int dp[100000];
int main() {
  int N, K;
  cin >> N >> K;
  int A[N];
  for (int i = 0; N > i; i++) {
    cin >> A[i];
  }
  for (int i = 0; N > i; i++) {
    dp[i] = MAX;
  }
  dp[0] = 0;
  for (int i = 0; N > i; i++) {
    for (int j = 0; min(K + i + 1, N) > i + j + 1; j++) {
      dp[i + j + 1] = min(dp[i + j + 1], dp[i] + abs(A[i + j + 1] - A[i]));
      // cout << i << " " << j << " " << i+j+1 << endl;
    }
  }
  cout << dp[N - 1] << endl;
}
Problem p03161 (C++)

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

#define watch(x) cerr << (#x) << ": " << (x) << endl
#define int long long

int32_t main() {

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

  vector<int> dp(n);
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);

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

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

  return 0;
}


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

#define watch(x) cerr << (#x) << ": " << (x) << endl
#define int long long

int32_t main() {

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

  vector<int> dp(n);
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);

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

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

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <limits.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int h[100000];
  int dp[100000];
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    dp[i] = INT_MAX;
  }
  dp[0] = 0;
  for (int p = 0; p < n; p++) {
    for (int q = 1; q <= k; q++) {
      dp[p + q] = min(dp[p + q], dp[p] + abs(h[p] - h[p + q]));
    }
  }
  cout << dp[n - 1] << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <limits.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int h[100000];
  int dp[100000];
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    dp[i] = INT_MAX;
  }
  dp[0] = 0;
  for (int p = 0; p < n; p++) {
    for (int q = 1; q <= k; q++) {
      if (p + q >= n)
        break;
      dp[p + q] = min(dp[p + q], dp[p] + abs(h[p] - h[p + q]));
    }
  }
  cout << dp[n - 1] << endl;
}
Problem p03161 (C++)

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

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, m, n) for (int i = (int)m; i < (int)n; i++)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vii; // 2次元配列
typedef vector<ll> vll;
typedef vector<string> vs;

const int inf = 1e9;
/*FUNCs=================================================*/

/*MAIN==================================================*/
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false); // cin cout 高速化

  int n, k;
  cin >> n >> k;
  vi h(n);
  rep(i, n) cin >> h[i];
  vi cost(n, inf);
  cost[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= k; j++) { // 1-orijin
      cost[i] = min(cost[i], cost[i - j] + abs(h[i] - h[i - j]));
    }
  }
  cout << cost[n - 1] << '\n';
}


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

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, m, n) for (int i = (int)m; i < (int)n; i++)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vii; // 2次元配列
typedef vector<ll> vll;
typedef vector<string> vs;

const int inf = 1e9;
/*FUNCs=================================================*/

/*MAIN==================================================*/
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false); // cin cout 高速化

  int n, k;
  cin >> n >> k;
  vi h(n);
  rep(i, n) cin >> h[i];
  vi cost(n, inf);
  cost[0] = 0;
  for (int i = 1; i < n; i++) {
    for (int j = 1; (i < k ? j <= i : j <= k); j++) {
      cost[i] = min(cost[i], cost[i - j] + abs(h[i] - h[i - j]));
    }
  }
  cout << cost[n - 1] << '\n';
}
Problem p03161 (C++)

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, K, dp[100001], h[100001];
  fill(dp, dp + 100001, 1e9);
  dp[0] = 0;
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> h[i];
  for (int i = 0; i < N; i++)
    for (int k = 1; k <= K; k++)
      if (i + k < N)
        dp[i + k] = min(abs(h[i + k] - h[i]) + dp[i], dp[i + k]);
  cout << dp[N - 1] << endl;
}
Problem p03161 (C++)

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

typedef long long ll;

#define INF 1e18
#define MAX_N 100005

ll N, K, h[MAX_N], memo[MAX_N];

ll dp(int p) {
  if (p < 1) {
    return INF;
  }
  if (p == 1) {
    return 0;
  }
  if (p <= K + 1) {
    return abs(h[p] - h[1]);
  }
  ll opt = INF;
  for (int i = 1; i <= K; i++) {
    opt = min(opt, abs(h[p] - h[p - i]) + dp(p - i));
  }
  return memo[p] = opt;
}

int main() {
  scanf("%lli %lli", &N, &K);
  for (int i = 1; i <= N; i++) {
    scanf("%lli", &h[i]);
  }
  printf("%lli\n", dp(N));
}

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

typedef long long ll;

#define INF 1e18
#define MAX_N 100005

ll N, K, h[MAX_N], memo[MAX_N];

ll dp(int p) {
  if (p < 1) {
    return INF;
  }
  if (p == 1) {
    return 0;
  }
  if (p <= K + 1) {
    return abs(h[p] - h[1]);
  }
  ll opt = INF;
  for (int i = 1; i <= K; i++) {
    opt = min(opt, abs(h[p] - h[p - i]) + dp(p - i));
  }
  return memo[p] = opt;
}

int main() {
  scanf("%lli %lli", &N, &K);
  for (int i = 1; i <= N; i++) {
    scanf("%lli", &h[i]);
  }
  // printf("%lli\n", dp(N));
  for (int i = 2; i <= 1 + K; i++) {
    memo[i] = abs(h[i] - h[1]);
  }
  for (int i = 2 + K; i <= N; i++) {
    ll opt = INF;
    for (int j = 1; j <= K; j++) {
      opt = min(opt, abs(h[i] - h[i - j]) + memo[i - j]);
    }
    memo[i] = opt;
  }
  printf("%lli\n", memo[N]);
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)

using namespace std;
typedef long long ll;

int main() {
  ll inf = 1LL << 60;
  int N, K;
  cin >> N >> K;

  ll dp[100010];
  vector<ll> hs(100100, 0);
  ll h;
  for (int i = 0; i < N; ++i) {
    dp[i] = inf;
    cin >> h;
    hs[i] = h;
  }
  dp[0] = 0;
  for (int i = 0; i < N; ++i) {
    for (int y = 1; y <= K; ++y) {
      dp[i + y] = min(dp[i + y], dp[i] + abs(hs[i] - hs[i + y]));
    }
  }

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


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)

using namespace std;
typedef long long ll;

int main() {
  ll inf = 1LL << 60;
  int N, K;
  cin >> N >> K;

  ll dp[100100];
  vector<ll> hs(100100, 0);
  ll h;
  for (int i = 0; i < N; ++i) {
    dp[i] = inf;
    cin >> h;
    hs[i] = h;
  }
  dp[0] = 0;
  for (int i = 0; i < N; ++i) {
    for (int y = 1; y <= K; ++y) {
      dp[i + y] = min(dp[i + y], dp[i] + abs(hs[i] - hs[i + y]));
    }
  }

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

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

using namespace std;

typedef long long LL;

const LL MAX = 100005;
const LL INF = 1000 * 1000 * 1000 + 9;

LL n, k, DP[MAX] = {}, stone[MAX] = {};

LL calc(LL idx) {
  if (DP[idx] != INF)
    return DP[idx];
  else if (idx == n - 1)
    return DP[idx] = 0;
  else if (idx > n - 1)
    return INF;
  else {
    LL MIN = INF;
    for (LL i0 = 1; i0 <= k; i0++) {
      if (idx + i0 > n - 1)
        break;
      MIN = min(MIN, calc(DP[idx + i0] + abs(stone[idx + i0] - stone[idx])));
    }
    return DP[idx] = MIN;
  }
}

int main(void) {
  cin >> n >> k;
  for (LL i0 = 0; i0 < n; i0++) {
    cin >> stone[i0];
    DP[i0] = INF;
  }

  cout << calc(0) << endl;
  return 0;
}

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

using namespace std;

typedef long long LL;

const LL MAX = 100005;
const LL INF = 1000 * 1000 * 1000 + 9;

LL n, k, DP[MAX] = {}, stone[MAX] = {};

LL calc(LL idx) {
  if (DP[idx] != INF)
    return DP[idx];
  else if (idx == n - 1)
    return DP[idx] = 0;
  else if (idx > n - 1)
    return INF;
  else {
    LL MIN = INF;
    for (LL i0 = 1; i0 <= k; i0++) {
      if (idx + i0 > n - 1)
        break;
      MIN = min(MIN, calc(idx + i0) + abs(stone[idx + i0] - stone[idx]));
    }
    return DP[idx] = MIN;
  }
}

int main(void) {
  cin >> n >> k;
  for (LL i0 = 0; i0 < n; i0++) {
    cin >> stone[i0];
    DP[i0] = INF;
  }

  cout << calc(0) << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
// B - Frog 2
#include <bits/stdc++.h>
using namespace std;
#define rp(i, s, e) for (int i = (s); i < (e); ++i)
#define chmin(x, y) x = min(x, y)

int main() {
  int INF = 1e+9, N, K;
  cin >> N >> K;
  vector<int> H(N), dp(N, INF);
  rp(i, 0, N) cin >> H[i]; // i:[0,N-1]
  dp[0] = 0;
  rp(i, 1, N) {                          // i:[1,N-1]
    for (int j = i - 1; j >= i - K; --j) // j:i-k:[i-1,i-K]
      chmin(dp[i], dp[j] + abs(H[j] - H[i]));
  }
  cout << dp[N - 1] << endl;
}


Fixed submission:
// B - Frog 2
#include <bits/stdc++.h>
using namespace std;
#define rp(i, s, e) for (int i = (s); i < (e); ++i)
#define chmin(x, y) x = min(x, y)

int main() {
  int INF = 1e+9, N, K;
  cin >> N >> K;
  vector<int> H(N), dp(N, INF);
  rp(i, 0, N) cin >> H[i]; // i:[0,N-1]
  dp[0] = 0;
  rp(i, 1, N) {                                  // i:[1,N-1]
    for (int j = i - 1; j >= max(0, i - K); --j) // j:i-k:[i-1,max(0,i-K)]
      chmin(dp[i], dp[j] + abs(H[j] - H[i]));
  }
  cout << dp[N - 1] << endl;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define all(v) ((v).begin()), ((v).end())
#define rall(v) ((v).rbegin()), ((v).rend())
#define clr(v, line) memset(v, line, sizeof(v))
#define endl '\n'
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *coma = strchr(names + 1, ',');
  cerr.write(names, coma - names) << " : " << arg1 << " | ";
  __f(coma + 1, args...);
}
#else
#define trace(...)
#endif

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

void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}

int N, k;

std::vector<int> dp(100005, INT_MAX), h;

int main(int argc, char const *argv[]) {
  file();
  fast();

  cin >> N >> k;

  h.resize(N);

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

  dp[0] = 0;

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

Fixed submission:
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define all(v) ((v).begin()), ((v).end())
#define rall(v) ((v).rbegin()), ((v).rend())
#define clr(v, line) memset(v, line, sizeof(v))
#define endl '\n'
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *coma = strchr(names + 1, ',');
  cerr.write(names, coma - names) << " : " << arg1 << " | ";
  __f(coma + 1, args...);
}
#else
#define trace(...)
#endif

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

void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}

int N, k;

std::vector<int> dp(100005, INT_MAX), h;

int main(int argc, char const *argv[]) {
  // file();
  fast();

  cin >> N >> k;

  h.resize(N);

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

  dp[0] = 0;

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

const long long INF = 1LL << 60;
long long h[10010];
long long dp[100010];

int main() {
  int N, K;
  scanf("%d%d", &N, &K);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &h[i]);
  }

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

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

  printf("%lld", dp[N - 1]);
}


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

const long long INF = 1LL << 60;
long long h[100010];
long long dp[100010];

int main() {
  int N, K;
  scanf("%d%d", &N, &K);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &h[i]);
  }

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

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

  printf("%lld", dp[N - 1]);
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define ll unsigned long long int
#define loop(i, a, b) for (int i = a; i < b; i++)

int main() {
  int n, k;
  int h[100005];
  cin >> n >> k;
  loop(i, 0, n) cin >> h[i];
  ll dp[100005];
  loop(i, 0, 100005) dp[i] = -1;

  dp[0] = 0;
  int i, j;

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

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

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define ll unsigned long long int
#define loop(i, a, b) for (int i = a; i < b; i++)

int main() {
  int n, k;
  int h[100005];
  cin >> n >> k;
  loop(i, 0, n) cin >> h[i];
  ll dp[100005];
  loop(i, 0, 100005) dp[i] = -1;

  dp[0] = 0;
  int i, j;

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

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

  return 0;
}