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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ln '\n';
int lim = 1e6;
ll inf = 1e15;
ll solve(ll *dp, int n, int i, ll *h, int k) {
  if (i >= n) {
    return inf;
  }
  if (dp[i] != -1) {
    return dp[i];
  }
  if (i == n - 1) {
    return 0;
  } else {
    ll ans = inf;
    for (int j = 1; j <= min(n - i - 1, k); j++) {
      ll tmp = solve(dp, n, i + j, h, k);
      tmp += abs(h[i] - h[i + j]);
      ans = min(ans, tmp);
    }
    return ans;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  ll h[n];
  int k;
  cin >> k;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  ll dp[n];
  for (int i = 0; i < n; i++) {
    dp[i] = -1;
  }
  cout << solve(dp, n, 0, h, k) << ln;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ln '\n';
int lim = 1e6;
ll inf = 1e15;
ll solve(ll *dp, int n, int i, ll *h, int k) {
  if (i >= n) {
    return inf;
  }
  if (dp[i] != -1) {
    return dp[i];
  }
  if (i == n - 1) {
    return 0;
  } else {
    ll ans = inf;
    for (int j = 1; j <= min(n - i - 1, k); j++) {
      ll tmp = solve(dp, n, i + j, h, k);
      tmp += abs(h[i] - h[i + j]);
      ans = min(ans, tmp);
    }
    dp[i] = ans;
    return ans;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  ll h[n];
  int k;
  cin >> k;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  ll dp[n];
  for (int i = 0; i < n; i++) {
    dp[i] = -1;
  }
  cout << solve(dp, n, 0, h, k) << ln;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long int ull;
#define pb push_back
#define For(ii, aa, bb) for (ll ii = aa; ii < bb; ii++)
#define Rof(ii, aa, bb) for (ll ii = aa; ii >= bb; ii--)
#define st first
#define nd second
#define MP make_pair
#define MAX 1000000009
#define MOD 1000000007
#define all(v6) v6.begin(), v6.end()
#define IT iterator
#define ln "\n"
#define F(ii) ((abs(ii) + ii) / 2)
#define N 500005
using namespace std;
ll n, k, m;
ll dp[N] = {0};
ll arr[N];
void solve(ll pl) {
  dp[pl] = MAX;
  For(i, 1, k + 1) {
    if (pl - i < 1) {
      break;
    }
    dp[pl] = min(dp[pl], abs(arr[pl] - arr[pl - i]) + dp[pl - i]);
  }
  if (dp[pl] == MAX) {
    dp[pl] = 0;
  }
  if (pl != n) {
    solve(pl + 1);
  }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> k;
  For(i, 1, n + 1) { cin >> arr[i]; }
  solve(1);
  cout << dp[n];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long int ull;
#define pb push_back
#define For(ii, aa, bb) for (ll ii = aa; ii < bb; ii++)
#define Rof(ii, aa, bb) for (ll ii = aa; ii >= bb; ii--)
#define st first
#define nd second
#define MP make_pair
#define MAX 1000000009
#define MOD 1000000007
#define all(v6) v6.begin(), v6.end()
#define IT iterator
#define ln "\n"
#define F(ii) ((abs(ii) + ii) / 2)
#define N 500005
using namespace std;
ll n, k, m;
ll dp[N] = {0};
ll arr[N];
void solve(ll pl) {
  dp[pl] = MAX;
  For(i, 1, k + 1) {
    if (pl - i < 1) {
      break;
    }
    dp[pl] = min(dp[pl], abs(arr[pl] - arr[pl - i]) + dp[pl - i]);
  }
  if (dp[pl] == MAX) {
    dp[pl] = 0;
  }
  if (pl != n) {
    solve(pl + 1);
  }
}
int main() {
#ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> k;
  For(i, 1, n + 1) { cin >> arr[i]; }
  solve(1);
  cout << dp[n];
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int stones[101];
int steps[101];

int main() {
  int N, K;
  scanf("%d %d", &N, &K);
  int pos = 0;
  for (int i = 0; i < N; ++i) {
    int stone;
    scanf("%d", &stone);
    if (i == 0) {
      steps[pos] = 0;
      stones[pos] = stone;
    } else {
      int m = 10000000001;
      for (int j = 1; j <= K || j <= i; j++) {
        int tmp =
            abs(stone - stones[(pos - j + K) % K]) + steps[(pos - j + K) % K];
        m = min(m, tmp);
      }
      steps[pos] = m;
      stones[pos] = stone;
    }
    pos = (pos + 1) % K;
  }

  printf("%d\n", steps[(pos - 1 + K) % K]);
  system("pause");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int stones[101];
int steps[101];

int main() {
  int N, K;
  scanf("%d %d", &N, &K);
  int pos = 0;
  for (int i = 0; i < N; ++i) {
    int stone;
    scanf("%d", &stone);
    if (i == 0) {
      steps[pos] = 0;
      stones[pos] = stone;
    } else {
      int m = 0x7FFFFFFF;
      for (int j = 1; j <= K && j <= i; j++) {
        int tmp =
            abs(stone - stones[(pos - j + K) % K]) + steps[(pos - j + K) % K];
        m = min(m, tmp);
      }
      steps[pos] = m;
      stones[pos] = stone;
    }
    pos = (pos + 1) % K;
  }

  printf("%d\n", steps[(pos - 1 + K) % K]);
  system("pause");
  return 0;
}
Problem p03161 (C++)

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

int N, K;

int flog(vector<int> &dp, vector<int> h, int n) {
  if (n == 0) {
    return 0;
  }

  if (dp[n] != -1) {
    return dp[n];
  }

  int mn = 2147483647;
  for (int i = 1; i <= K; i++) {
    if (n - i < 0)
      break;
    mn = min(mn, abs(h[n] - h[n - i]) + flog(dp, h, n - i));
  }
  dp[n] = mn;
  return dp[n];
}

int main() {
  cin >> N >> K;
  vector<int> h(N);
  rep(i, N) cin >> h[i];
  vector<int> dp(N, -1);

  // dp[0]=0;
  // for(int i=0; i<N-1; i++){
  //     for(int 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]));
  //     }
  // }
  int ans = flog(dp, h, N - 1);
  cout << ans << endl;
}


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

int N, K;

int flog(vector<int> &dp, vector<int> &h, int n) {
  if (n == 0) {
    return 0;
  }

  if (dp[n] != -1) {
    return dp[n];
  }

  int mn = 2147483647;
  for (int i = 1; i <= K; i++) {
    if (n - i < 0)
      break;
    mn = min(mn, abs(h[n] - h[n - i]) + flog(dp, h, n - i));
  }
  dp[n] = mn;
  return dp[n];
}

int main() {
  cin >> N >> K;
  vector<int> h(N);
  rep(i, N) cin >> h[i];
  vector<int> dp(N, -1);

  // dp[0]=0;
  // for(int i=0; i<N-1; i++){
  //     for(int 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]));
  //     }
  // }
  int ans = flog(dp, h, N - 1);
  cout << ans << endl;
}
Problem p03161 (C++)

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


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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long int ll;
#define forn(i, p, n)                                                          \
  ;                                                                            \
  for (long long i = p; i < n; ++i)
#define pb push_back

void solve() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  forn(i, 0, n) { cin >> a[i]; }
  ll dp[n];
  dp[0] = 0;
  // dp[n-2] = abs(a[n-2]-a[n-1]);
  forn(i, 1, n) {
    dp[i] = INT_MAX;
    for (int j = 1; j <= k; ++j) {
      int temp = abs(a[i] - a[i - j]);
      dp[i] = min(dp[i], dp[i - j] + temp);
    }
  }
  cout << dp[n - 1];
  return;
}

int main() {
  fast;
  // int test;
  // cin >> test;
  // while(test--){
  solve();
  // 	cout<<"\n";
  // }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long int ll;
#define forn(i, p, n)                                                          \
  ;                                                                            \
  for (long long i = p; i < n; ++i)
#define pb push_back

void solve() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  forn(i, 0, n) { cin >> a[i]; }
  ll dp[n];
  dp[0] = 0;
  // dp[n-2] = abs(a[n-2]-a[n-1]);
  forn(i, 1, n) {
    dp[i] = INT_MAX;
    for (int j = 1; j <= k && (i - j) >= 0; ++j) {
      int temp = abs(a[i] - a[i - j]);
      dp[i] = min(dp[i], dp[i - j] + temp);
    }
  }
  cout << dp[n - 1];
  return;
}

int main() {
  fast;
  // int test;
  // cin >> test;
  // while(test--){
  solve();
  // 	cout<<"\n";
  // }
}
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 int long long int
#define endl "\n"
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

const int N = 1e5 + 5;
int n, h[N], cache[N], k;

int dp(int pos) {
  if (pos == n)
    return 0;
  if (pos == n - 1)
    return abs(h[pos] - h[n]);
  int &ans = cache[pos];
  if (ans != -1)
    return ans;
  ans = 1e15;
  for (int i = pos + 1; i <= (pos + k); i++)
    ans = min(dp(i) + abs(h[pos] - h[i]), ans);
  return ans;
}

int32_t main() {
  IOS;

  memset(cache, -1, sizeof cache);
  cin >> n >> k;

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

  cout << dp(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 int long long int
#define endl "\n"
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

const int N = 1e5 + 5;
int n, h[N], cache[N], k;

int dp(int pos) {
  if (pos == n)
    return 0;
  if (pos == n - 1)
    return abs(h[pos] - h[n]);
  int &ans = cache[pos];
  if (ans != -1)
    return ans;
  ans = 1e15;
  for (int i = pos + 1; i <= min(n, (pos + k)); i++)
    ans = min(dp(i) + abs(h[pos] - h[i]), ans);
  return ans;
}

int32_t main() {
  IOS;

  memset(cache, -1, sizeof cache);
  cin >> n >> k;

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

  cout << dp(1);

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef vector<long long> vi;
typedef long long ll;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define endl "\n"
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define fr(n) for (ll i = 0; i < n; i++)
#define tr(a) for (auto it = a.begin(); it != a.end(); it++)
#define N 10001
#define PI 3.1415926535897932384
#define F first
#define S second
#define elasped_time 1.0 * clock() / CLOCKS_PER_SEC
#define FAST                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mp make_pair

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  ll n, k;
  cin >> n >> k;
  ll a[n];
  fr(n) cin >> a[i];
  ll dp[n];
  dp[0] = 0;
  fr(k - 1) dp[i + 1] = abs(a[0] - a[i]);
  rep(i, 1, n) {
    ll mn = 1e10;
    rep(j, 1, k + 1) {
      if (j <= i)
        mn = min(dp[i - j] + abs(a[i] - a[i - j]), mn);
    }
    // cout<<x<<" "<<y<<endl;
    dp[i] = mn;
  }
  // fr(n) cout<<dp[i]<<" ";
  cout << dp[n - 1];

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef vector<long long> vi;
typedef long long ll;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define endl "\n"
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define fr(n) for (ll i = 0; i < n; i++)
#define tr(a) for (auto it = a.begin(); it != a.end(); it++)
#define N 10001
#define PI 3.1415926535897932384
#define F first
#define S second
#define elasped_time 1.0 * clock() / CLOCKS_PER_SEC
#define FAST                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mp make_pair

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  ll n, k;
  cin >> n >> k;
  ll a[n];
  fr(n) cin >> a[i];
  ll dp[n];
  dp[0] = 0;
  // fr(k-1) dp[i+1]=abs(a[0]-a[i]);
  rep(i, 1, n) {
    ll mn = 1e10;
    rep(j, 1, k + 1) {
      if (j <= i)
        mn = min(dp[i - j] + abs(a[i] - a[i - j]), mn);
    }
    // cout<<x<<" "<<y<<endl;
    dp[i] = mn;
  }
  // fr(n) cout<<dp[i]<<" ";
  cout << dp[n - 1];

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <vector>

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

  if (1 + k <= n) {
    cout << abs(h[n - 1] - h[0]) << endl;
    return 0;
  }

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

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

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

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <vector>

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

  if (n <= 1 + k) {
    cout << abs(h[n - 1] - h[0]) << endl;
    return 0;
  }

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

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

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

  return 0;
}
Problem p03161 (C++)

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

using namespace std;
typedef long long ll;

#define out(ans) cout << ans << endl
#define in(n) cin >> n
#define floop(i, N) for (ll i = 0; i < N; i++)
#define floop1(i, N) for (ll i = 1; i <= N; i++)
#define floopi(i, j, N) for (ll j = i + 1; j < N; j++)

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

constexpr ll inf = 1e18;

void solve();

int main() { solve(); }

void solve() {
  ll n, k;
  in(n);
  in(k);
  ll h[n];
  floop(i, n) in(h[i]);
  ll dp[n];
  floop(i, n) dp[i] = inf;
  dp[0] = 0;
  floop(i, n) {
    floop1(j, k) { chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); }
  }
  out(dp[n - 1]);
}

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

using namespace std;
typedef long long ll;

#define out(ans) cout << ans << endl
#define in(n) cin >> n
#define floop(i, N) for (ll i = 0; i < N; i++)
#define floop1(i, N) for (ll i = 1; i <= N; i++)
#define floopi(i, j, N) for (ll j = i + 1; j < N; j++)

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

constexpr ll inf = 1e18;

void solve();

int main() { solve(); }

void solve() {
  ll n, k;
  in(n);
  in(k);
  ll h[n];
  floop(i, n) in(h[i]);
  ll dp[110000];
  floop(i, 110000) dp[i] = inf;
  dp[0] = 0;
  floop(i, n) {
    floop1(j, k) { chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); }
  }
  out(dp[n - 1]);
}
Problem p03161 (C++)

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

using namespace std;

const int INF = 1e9;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ///   freopen("input.txt", "r", stdin);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int &x : a)
    cin >> x;

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

  return 0;
}


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

using namespace std;

const int INF = 1e9;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ///   freopen("input.txt", "r", stdin);

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int &x : a)
    cin >> x;

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

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
#define ma 1000000000
int mod(int a) {
  if (a >= 0)
    return a;
  else
    return -a;
}

using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a;
  a.resize(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int b[n];
  b[0] = 0;
  b[1] = mod(a[1] - a[0]);
  for (int i = 2; i <= k; i++)
    b[i] = mod(a[i] - a[0]);
  for (int i = k + 1; i < n; i++) {
    int t = ma;
    for (int k1 = 1; k1 <= k; k1++) {
      if (t > b[i - k1] + mod(a[i] - a[i - k1]))
        t = b[i - k1] + mod(a[i] - a[i - k1]);
    }
    b[i] = t;
  }
  cout << b[n - 1];
}


Fixed submission:
#include <iostream>
#include <vector>
#define ma 1000000000
int mod(int a) {
  if (a >= 0)
    return a;
  else
    return -a;
}

using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a;
  a.resize(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int b[n];
  b[0] = 0;
  b[1] = mod(a[1] - a[0]);
  for (int i = 2; i <= k && i < n; i++)
    b[i] = mod(a[i] - a[0]);
  for (int i = k + 1; i < n; i++) {
    int t = ma;
    for (int k1 = 1; k1 <= k; k1++) {
      if (t > b[i - k1] + mod(a[i] - a[i - k1]))
        t = b[i - k1] + mod(a[i] - a[i - k1]);
    }
    b[i] = t;
  }
  cout << b[n - 1];
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define max1 1000005
#define siz 100000
#define mod 998244353
#define inf 1e18 + 5
#define ll long long int
#define debug(x) cout << #x << " " << x << endl
#define jam(x) cout << "Case #" << x << ": "
typedef pair<ll, ll> pr;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  /*#ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif*/
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<ll> dp(n, inf);
  ll k1 = k;
  dp[0] = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = i - 1; j >= 0, k1 > 0; k1--, j--) {
      dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j]));
    }
    k1 = k;
  }
  cout << dp[n - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define max1 1000005
#define siz 100000
#define mod 998244353
#define inf 1e18 + 5
#define ll long long int
#define debug(x) cout << #x << " " << x << endl
#define jam(x) cout << "Case #" << x << ": "
typedef pair<ll, ll> pr;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  /*#ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif*/
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<ll> dp(n, inf);
  ll k1 = k;
  dp[0] = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = i - 1; (j >= 0 && k1 > 0); k1--, j--) {
      dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j]));
    }
    k1 = k;
  }
  cout << dp[n - 1] << endl;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
const int INF = 1e9 + 7;

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

  vector<int> dp(n, INF);

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

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

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
const int INF = 1e9 + 7;

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

  vector<int> dp(n, INF);

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

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

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

#define ll long long

#define fr(i, a, b) for (ll i = (a), _b = (b); i <= _b; i++)
#define frr(i, a, b) for (ll i = (a), _b = (b); i >= _b; i--)
#define rep(i, n) for (ll i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define fill(ar, val) memset(ar, val, sizeof(ar))
#define fill0(ar) fill((ar), 0)
#define debug(x) cout << #x << ": " << x << endl

#define ld double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<ii> vii;
typedef vector<int> vi;

#define INF 1000000000000000000

ll n, k;
ll h[100005];
ll ans[100005];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n >> k;

  fr(i, 1, n) { cin >> h[i]; }

  ans[1] = 0;
  fr(i, 2, n) { ans[i] = INF; }

  fr(i, 2, n) {
    fr(j, 1, min(i - 1, k)) {
      ll x = abs(h[i] - h[i - j]);
      ans[i] = min(ans[i], ans[i - j] + x);
    }
  }

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

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

#define ll long long

#define fr(i, a, b) for (ll i = (a), _b = (b); i <= _b; i++)
#define frr(i, a, b) for (ll i = (a), _b = (b); i >= _b; i--)
#define rep(i, n) for (ll i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define fill(ar, val) memset(ar, val, sizeof(ar))
#define fill0(ar) fill((ar), 0)
#define debug(x) cout << #x << ": " << x << endl

#define ld double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<ii> vii;
typedef vector<int> vi;

#define INF 1000000000000000000

ll n, k;
ll h[100005];
ll ans[100005];

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

  cin >> n >> k;

  fr(i, 1, n) { cin >> h[i]; }

  ans[1] = 0;
  fr(i, 2, n) { ans[i] = INF; }

  fr(i, 2, n) {
    fr(j, 1, min(i - 1, k)) {
      ll x = abs(h[i] - h[i - j]);
      ans[i] = min(ans[i], ans[i - j] + x);
    }
  }

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

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

using namespace std;
using ll = long long;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  for (int i = 0; i < N; ++i) {
    cin >> h[i];
  }
  vector<int> dp(N);
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < N; ++i) {
    int candi = dp[i - 1] + abs(h[i] - h[i - 1]);
    for (int j = 2; j <= K; ++j) {
      if (i - j < 0)
        break;
      candi = min(candi, dp[i - K] + abs(h[i] - h[i - j]));
    }
    dp[i] = candi;
  }
  cout << dp[N - 1] << endl;
  return 0;
}


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

using namespace std;
using ll = long long;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  for (int i = 0; i < N; ++i) {
    cin >> h[i];
  }
  vector<int> dp(N);
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < N; ++i) {
    int candi = dp[i - 1] + abs(h[i] - h[i - 1]);
    for (int j = 2; j <= K; ++j) {
      if (i - j < 0)
        break;
      candi = min(candi, dp[i - j] + abs(h[i] - h[i - j]));
    }
    dp[i] = candi;
  }
  cout << dp[N - 1] << endl;
  return 0;
}
Problem p03161 (C++)

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

using namespace std;

int frogCost(int h[], int d[], int n, int k) {
  if (d[n] != INT_MAX)
    return d[n];
  int cost = INT_MAX;
  for (int i = 1; i <= k; i++) {
    if ((n - i) >= 0) {
      int diff = abs(h[n - i] - h[n]);
      cost = min(cost, diff + frogCost(h, d, n - i, k));
    }
  }
  return cost;
}
int main() {
  int N, K;

  cin >> N >> K;
  int heights[N];
  int cost[N];
  for (int i = 0; i < N; i++) {
    int h;
    cin >> h;
    heights[i] = h;
    cost[i] = INT_MAX;
  }
  cost[0] = 0;
  // for(int j=0;j<N;j++){
  //   for(int k=1;k<=K;k++){
  //     if(j+k<N){
  //       int diff = abs(heights[j] - heights[j+k]);
  //       cost[j+k] = min(cost[j+k],cost[j]+diff);
  //     }
  //   }
  // }
  cout << frogCost(heights, cost, N - 1, K);
  return 0;
}


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

using namespace std;

int frogCost(int h[], int d[], int n, int k) {
  if (d[n] != INT_MAX)
    return d[n];
  int cost = INT_MAX;
  for (int i = 1; i <= k; i++) {
    if ((n - i) >= 0) {
      int diff = abs(h[n - i] - h[n]);
      cost = min(cost, diff + frogCost(h, d, n - i, k));
    }
  }
  d[n] = cost;
  return cost;
}
int main() {
  int N, K;

  cin >> N >> K;
  int heights[N];
  int cost[N];
  for (int i = 0; i < N; i++) {
    int h;
    cin >> h;
    heights[i] = h;
    cost[i] = INT_MAX;
  }
  cost[0] = 0;
  // for(int j=0;j<N;j++){
  //   for(int k=1;k<=K;k++){
  //     if(j+k<N){
  //       int diff = abs(heights[j] - heights[j+k]);
  //       cost[j+k] = min(cost[j+k],cost[j]+diff);
  //     }
  //   }
  // }
  cout << frogCost(heights, cost, N - 1, K);
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define MAXN 100005
#define INF 1e9 + 5
using namespace std;

int n, lake[MAXN], k;
int dp[MAXN];

int solve(int v) {
  if (v == n - 1)
    return 0;
  if (dp[v] >= 0)
    return dp[v];

  int temp = INF;
  for (int i = 1; i <= k; i++) {
    if (v + i >= n)
      break;
    temp = min(temp, abs(lake[v] - lake[v + i]) + solve(v + i));
  }
  return dp[k] = temp;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  memset(dp, -1, sizeof(dp));
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> lake[i];
  }

  cout << solve(0);
}

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

int n, lake[MAXN], k;
int dp[MAXN];

int solve(int v) {
  if (v == n - 1)
    return 0;
  if (dp[v] >= 0)
    return dp[v];

  int temp = INF;
  for (int i = 1; i <= k; i++) {
    if (v + i >= n)
      break;
    temp = min(temp, abs(lake[v] - lake[v + i]) + solve(v + i));
  }
  return dp[v] = temp;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  memset(dp, -1, sizeof(dp));
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> lake[i];
  }

  cout << solve(0);
}
Problem p03161 (C++)

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

void solve(vector<int> &h, vector<int> &v, int k = 2) {
  int n = h.size();
  for (int curr = 0; curr < n; curr++) {
    for (int j = curr + 1; j < n and j <= curr + k; j++) {
      int d1 = abs(h[curr] - h[curr + j]);
      v[j] = min(v[j], v[curr] + d1);
    }
  }
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> h(n), v(n);

  for (int i = 0; i < n; i++)
    cin >> h[i];
  v[0] = 0;
  for (int i = 1; i < n; i++)
    v[i] = INT_MAX;
  solve(h, v, k);
  cout << v[n - 1] << endl;
}

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

void solve(vector<int> &h, vector<int> &v, int k = 2) {
  int n = h.size();
  for (int curr = 0; curr < n; curr++) {
    for (int j = curr + 1; j < n and j <= curr + k; j++) {
      int d1 = abs(h[curr] - h[j]);
      v[j] = min(v[j], v[curr] + d1);
    }
  }
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> h(n), v(n);

  for (int i = 0; i < n; i++)
    cin >> h[i];
  v[0] = 0;
  for (int i = 1; i < n; i++)
    v[i] = INT_MAX;
  solve(h, v, k);
  cout << v[n - 1] << endl;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
ll n, k;
ll func(ll cost[], ll h[]) {
  cost[1] = 0;
  cost[2] = abs(h[1] - h[2]);
  for (ll i = 3; i <= k + 1; i++) {
    ll temp = cost[i - 1] + abs(h[i] - h[i - 1]);
    for (ll j = 2; j < i; j++) {
      temp = min(temp, cost[i - j] + abs(h[i] - h[i - j]));
    }
    cost[i] = temp;
  }
  for (ll i = k + 2; i <= n; i++) {
    ll temp = cost[i - 1] + abs(h[i] - h[i - 1]);
    for (ll j = 2; j <= k; j++) {
      temp = min(temp, cost[i - j] + abs(h[i] - h[i - j]));
    }
    cost[i] = temp;
  }
  return cost[n];
}
int32_t main() {
  IOS;
  cin >> n >> k;
  ll h[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  ll cost[n + 1];
  ll costn = func(cost, h);
  cout << costn;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
ll n, k;
ll func(ll cost[], ll h[]) {
  cost[1] = 0;
  cost[2] = abs(h[1] - h[2]);
  for (ll i = 3; i <= k + 1; i++) {
    ll temp = cost[i - 1] + abs(h[i] - h[i - 1]);
    for (ll j = 2; j < i; j++) {
      temp = min(temp, cost[i - j] + abs(h[i] - h[i - j]));
    }
    cost[i] = temp;
  }
  for (ll i = k + 2; i <= n; i++) {
    ll temp = cost[i - 1] + abs(h[i] - h[i - 1]);
    for (ll j = 2; j <= k; j++) {
      temp = min(temp, cost[i - j] + abs(h[i] - h[i - j]));
    }
    cost[i] = temp;
  }
  return cost[n];
}
int32_t main() {
  IOS;
  cin >> n >> k;
  ll h[n + 1];
  if (k > n) {
    k = n;
  }
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  ll cost[n + 1];
  ll costn = func(cost, h);
  cout << costn;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep0(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
#define REP(i, a, b) for (int i = a; i < (b); ++i)
#define mREP(i, a, b) for (int i = a; i > (b); --i)
#define all(x) (x).begin(), (x).end()
const int INF = 1000000007;
typedef long long ll;
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;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int main() {
  vector<int> dp(1000000, INF);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep0(i, n) cin >> a[i];
  dp[0] = 0;
  dp[1] = abs(a[0] - a[1]);
  REP(i, 2, n) {
    rep1(j, k + 1) {
      if (k > j)
        continue;
      dp[i] = min(dp[i], (dp[i - j] + abs(a[i - j] - a[i])));
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep0(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
#define REP(i, a, b) for (int i = a; i < (b); ++i)
#define mREP(i, a, b) for (int i = a; i > (b); --i)
#define all(x) (x).begin(), (x).end()
const int INF = 1000000007;
typedef long long ll;
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;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int main() {
  vector<int> dp(1000000, INF);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep0(i, n) cin >> a[i];
  dp[0] = 0;
  dp[1] = abs(a[0] - a[1]);
  REP(i, 2, n) {
    rep1(j, k + 1) {
      if (j > i)
        continue;
      dp[i] = min(dp[i], (dp[i - j] + abs(a[i - j] - a[i])));
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

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

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

int main() {
  int n, k;
  cin >> n;
  cin >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cout << minCost(a, n, k);
}

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

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

int main() {
  int n, k;
  cin >> n;
  cin >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cout << minCost(a, n, k);
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
/*
    ##############################
    #  Author: Pratyush Gaurav   #
    #  College: NIT ROURKELA     #
    ##############################
*/
#include <bits/stdc++.h>

using namespace std;

typedef long long lli;
typedef long double ld;
typedef pair<lli, lli> plli;
typedef vector<lli> vlli;
typedef vector<plli> vplli;

#define IOS ios_base::sync_with_stdio(false);
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define sqr(x) (x) * (x)
#define all(a) a.begin(), a.end()
#define UN(v) sort(all(v)), v.resize(unique(all(v)) - v.begin())

#define endl '\n'

const long long INF = 100000000000000000;
const long long MOD = 1000000007;
const long long MAXN = 100005;

lli dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
lli dy[] = {1, -1, 0, 0, 1, -1, -1, 1};

lli n, k;
lli a[MAXN], dp[MAXN];

lli f(lli x) {
  if (x > n)
    return INF;
  if (x == n)
    return 0;
  if (dp[x] != -1)
    return dp[x];

  lli ans = INF;
  for (int i = 1; i <= k; ++i) {
    ans = min(ans, abs(a[x] - a[x + i]) + f(x + i));
  }
  return dp[x] = ans;
}
void solve() {
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  memset(dp, -1, sizeof(dp));
  lli ans = f(1);
  cout << ans << endl;
}

int main() {
  IOS;
  lli t = 1;
  // cin >> t;

  solve();

  return 0;
}

Fixed submission:
/*
    ##############################
    #  Author: Pratyush Gaurav   #
    #  College: NIT ROURKELA     #
    ##############################
*/
#include <bits/stdc++.h>

using namespace std;

typedef long long lli;
typedef long double ld;
typedef pair<lli, lli> plli;
typedef vector<lli> vlli;
typedef vector<plli> vplli;

#define IOS ios_base::sync_with_stdio(false);
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define sqr(x) (x) * (x)
#define all(a) a.begin(), a.end()
#define UN(v) sort(all(v)), v.resize(unique(all(v)) - v.begin())

#define endl '\n'

const long long INF = 100000000000000000;
const long long MOD = 1000000007;
const long long MAXN = 100005;

lli dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
lli dy[] = {1, -1, 0, 0, 1, -1, -1, 1};

lli n, k;
lli a[MAXN], dp[MAXN];

lli f(lli x) {
  if (x > n)
    return INF;
  if (x == n)
    return 0;
  if (dp[x] != -1)
    return dp[x];

  lli ans = INF;
  for (int i = 1; i <= k; ++i) {
    if (x + i > n)
      continue;
    ans = min(ans, abs(a[x] - a[x + i]) + f(x + i));
  }
  return dp[x] = ans;
}
void solve() {
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  memset(dp, -1, sizeof(dp));
  lli ans = f(1);
  cout << ans << endl;
}

int main() {
  IOS;
  lli t = 1;
  // cin >> t;

  solve();

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  int k;
  cin >> k;
  int a[n + 1];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  vector<ll> dp(n + 1, INT_MAX);
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (int i = 2; i < n; i++) {
    for (int j = i - 1, jump = 0; j >= 0, jump < k; j--, jump++) {

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

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

  return 0;
}


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

  vector<ll> dp(n + 1, INT_MAX);
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (int i = 2; i < n; i++) {
    for (int j = i - 1, jump = 0; j >= 0 && jump < k; j--, jump++) {

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

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

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define ALL(x) (x).begin(), (x).end()
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N, K;
  cin >> N;
  cin >> K;
  int h[0] = {};

  REP(i, N) cin >> h[i];

  ll DP[N];
  REP(i, N) DP[i] = INF;
  DP[0] = 0;

  REPS(i, N - 1) {
    REPS(j, K) {
      if (i - j >= 0)
        chmin(DP[i], DP[i - j] + abs(h[i] - h[i - j]));
    }
  }
  cout << DP[N - 1];

  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define ALL(x) (x).begin(), (x).end()
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int N, K;
  cin >> N;
  cin >> K;
  int h[N] = {};

  REP(i, N) cin >> h[i];

  ll DP[N];
  REP(i, N) DP[i] = INF;
  DP[0] = 0;

  REPS(i, N - 1) {
    REPS(j, K) {
      if (i - j >= 0)
        chmin(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 sa(int a, int b) { return max(a - b, b - a); }
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> he(N);
  for (int i = 0; i < N; i++)
    cin >> he[i];
  vector<int> dp(N);
  for (int i = 0; i <= K; i++) {
    dp[i] = sa(he[0], he[i]);
  }
  for (int i = K + 1; i < N; i++) {
    dp[i] = sa(he[i], he[i - 1]) + dp[i - 1];
    for (int j = 2; j <= K; j++) {
      int kuma = sa(he[i], he[i - j]) + dp[i - j];
      if (dp[i] > kuma)
        dp[i] = kuma;
    }
  }
  cout << dp[N - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int sa(int a, int b) { return max(a - b, b - a); }
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> he(N);
  for (int i = 0; i < N; i++)
    cin >> he[i];
  if (N - 1 <= K) {
    cout << sa(he[0], he[N - 1]) << endl;
    return 0;
  }
  vector<int> dp(N);
  for (int i = 0; i <= K; i++) {
    dp[i] = sa(he[0], he[i]);
  }
  for (int i = K + 1; i < N; i++) {
    dp[i] = sa(he[i], he[i - 1]) + dp[i - 1];
    for (int j = 2; j <= K; j++) {
      int kuma = sa(he[i], he[i - j]) + dp[i - j];
      if (dp[i] > kuma)
        dp[i] = kuma;
    }
  }
  cout << dp[N - 1] << endl;
}
Problem p03161 (C++)

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

using namespace std;

int h[10002] = {0};
int memo[10002] = {-1};

int compute(int startPos, int n, int k) {
  if (memo[startPos] != -1) {
    return memo[startPos];
  }
  if (startPos == n - 1) {
    return 0;
  }
  if (startPos == n - 2) {
    return abs(h[startPos] - h[startPos + 1]);
  }
  int value = abs(h[startPos] - h[startPos + 1]) + compute(startPos + 1, n, k);
  for (int i = 2; i <= k; i++) {
    if (startPos + i <= n - 1)
      value = min(value, abs(h[startPos] - h[startPos + i]) +
                             compute(startPos + i, n, k));
  }
  memo[startPos] = value;
  return value;
}

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    memo[i] = -1;
  }
  int answer = compute(0, n, k);
  cout << answer << endl;
  return 0;
}

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

using namespace std;

int h[100002] = {0};
int memo[100002] = {-1};

int compute(int startPos, int n, int k) {
  if (memo[startPos] != -1) {
    return memo[startPos];
  }
  if (startPos == n - 1) {
    return 0;
  }
  if (startPos == n - 2) {
    return abs(h[startPos] - h[startPos + 1]);
  }
  int value = abs(h[startPos] - h[startPos + 1]) + compute(startPos + 1, n, k);
  for (int i = 2; i <= k; i++) {
    if (startPos + i <= n - 1)
      value = min(value, abs(h[startPos] - h[startPos + i]) +
                             compute(startPos + i, n, k));
  }
  memo[startPos] = value;
  return value;
}

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    memo[i] = -1;
  }
  int answer = compute(0, n, k);
  cout << answer << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
////////////////////////////////////
///     Please Give Me AC!!!     ///
////////////////////////////////////

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define forin(in)                                                              \
  for (int i = 0; i < (int)in.size(); i++)                                     \
  cin >> in[i]
#define forout(out)                                                            \
  for (int i = 0; i < (int)out.size(); i++)                                    \
  cout << out[i] << endl
#define rep(i, n) for (int i = 0; i < (n); i++)

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

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

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

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

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

Fixed submission:
////////////////////////////////////
///     Please Give Me AC!!!     ///
////////////////////////////////////

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define forin(in)                                                              \
  for (int i = 0; i < (int)in.size(); i++)                                     \
  cin >> in[i]
#define forout(out)                                                            \
  for (int i = 0; i < (int)out.size(); i++)                                    \
  cout << out[i] << endl
#define rep(i, n) for (int i = 0; i < (n); i++)

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

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

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

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

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