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

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

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

  cost[0] = 0;
  cost[1] = abs(h[0] - h[1]);

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

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

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

  cost[0] = 0;
  cost[1] = abs(h[0] - h[1]);

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

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

#define ll long long
#define mod 1000000007
#define inf 1000000000000000000
#define fr first
#define sc second
#define pb push_back
#define pf push_front
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define per(i, a, n) for (ll i = n - 1; i >= a; i--)
#define pll pair<ll, ll>
#define vll vector<ll>
#define umap unordered_map<ll, ll>
#define lpq priority_queue<ll, vector<ll>, greater<ll>>
ll powe(ll n, ll r) {
  if (!r)
    return 1;
  else if (r % 2 == 0)
    return powe(n * n, r / 2);
  else
    return n * powe(n * n, (r - 1) / 2);
}

int main() {

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

  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll t = 1;
  // cin>>t;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    vector<ll> h(n);
    vector<ll> dp(n, inf);
    for (ll i = 0; i < n; i++)
      cin >> h[i];
    dp[0] = 0;
    dp[1] = abs(h[1] - h[0]);
    for (ll i = 2; i < n; i++) {
      for (ll j = i - 1; j >= 0 && j >= i - k; j--) {
        dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]));
      }
    }
    cout << dp[n - 1] << endl;
  }

  return 0;
}

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

#define ll long long
#define mod 1000000007
#define inf 1000000000000000000
#define fr first
#define sc second
#define pb push_back
#define pf push_front
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define per(i, a, n) for (ll i = n - 1; i >= a; i--)
#define pll pair<ll, ll>
#define vll vector<ll>
#define umap unordered_map<ll, ll>
#define lpq priority_queue<ll, vector<ll>, greater<ll>>
ll powe(ll n, ll r) {
  if (!r)
    return 1;
  else if (r % 2 == 0)
    return powe(n * n, r / 2);
  else
    return n * powe(n * n, (r - 1) / 2);
}

int main() {
  /*
  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif*/

  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll t = 1;
  // cin>>t;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    vector<ll> h(n);
    vector<ll> dp(n, inf);
    for (ll i = 0; i < n; i++)
      cin >> h[i];
    dp[0] = 0;
    dp[1] = abs(h[1] - h[0]);
    for (ll i = 2; i < n; i++) {
      for (ll j = i - 1; j >= 0 && j >= i - k; j--) {
        dp[i] = min(dp[i], dp[j] + abs(h[i] - h[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 fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define mod 1000000007

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

  fast;

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

  ll a[n], i;
  rep(i, 0, n) { cin >> a[i]; }
  ll dp[n];
  memset(dp, 10001, sizeof(dp));
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (i = 2; i < min(n, k); i++) {

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define mod 1000000007

int main() {

  fast;

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

  ll a[n], i;
  rep(i, 0, n) { cin >> a[i]; }
  ll dp[n];
  memset(dp, 10001, sizeof(dp));
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (i = 2; i < min(n, k); i++) {

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

  vector<int> dp(N);
  dp[0] = h[0];
  dp[1] = abs(h[0] - h[1]);
  for (int i = 2; i <= K; i++) {
    int this_min = INT_MAX;
    for (int j = i - 1; j >= 1; j--) {
      int curr_min = dp[j] + abs(h[j] - h[i]);
      this_min = (curr_min < this_min ? curr_min : this_min);
    }
    dp[i] = min(abs(h[0] - h[i]), this_min);
  }

  for (int i = K + 1; i < N; i++) {
    int dp_min = INT_MAX;
    for (int j = i - 1; j >= max((i - 1 - (K - 1)), 1); j--) {
      int dp_curr_min = dp[j] + abs(h[j] - h[i]);
      dp_min = (dp_curr_min < dp_min ? dp_curr_min : dp_min);
    }
    dp[i] = dp_min;
  }

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

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

using namespace std;

int main() {

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

  vector<int> dp(N);
  dp[0] = h[0];
  dp[1] = abs(h[0] - h[1]);
  for (int i = 2; i <= min(K, N - 1); i++) {
    int this_min = INT_MAX;
    for (int j = i - 1; j >= 1; j--) {
      int curr_min = dp[j] + abs(h[j] - h[i]);
      this_min = (curr_min < this_min ? curr_min : this_min);
    }
    dp[i] = min(abs(h[0] - h[i]), this_min);
  }

  for (int i = K + 1; i < N; i++) {
    int dp_min = INT_MAX;
    for (int j = i - 1; j >= max((i - 1 - (K - 1)), 1); j--) {
      int dp_curr_min = dp[j] + abs(h[j] - h[i]);
      dp_min = (dp_curr_min < dp_min ? dp_curr_min : dp_min);
    }
    dp[i] = dp_min;
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

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

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

int main() {
  int N, K;
  int h[100005];
  cin >> N >> K;
  rep(i, N) cin >> h[i];

  ll cost[100005];
  for (int i = 0; i < 100005; i++)
    cost[i] = INT_MAX;
  cost[0] = 0;
  cost[1] = abs(h[0] - h[1]);
  for (int i = 0; i < N; i++) {
    for (int k = 1; k <= K; k++) {
      cost[i + k] = min(cost[i + k], cost[i] + abs(h[i] - h[i + k]));
    }
  }
  cout << cost[N - 1] << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

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

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

int main() {
  int N, K;
  int h[100005];
  cin >> N >> K;
  rep(i, N) cin >> h[i];

  ll cost[100005];
  for (int i = 0; i < 100005; i++)
    cost[i] = INT_MAX;
  cost[0] = 0;
  cost[1] = abs(h[0] - h[1]);
  for (int i = 0; i < N; i++) {
    for (int k = 1; k <= min(N - i - 1, K); k++) {
      cost[i + k] = min(cost[i + k], cost[i] + abs(h[i] - h[i + k]));
    }
  }
  cout << cost[N - 1] << endl;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

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

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

int main() {
  int N, K;
  int h[100005];
  cin >> N >> K;
  rep(i, N) cin >> h[i];

  ll cost[100005];
  for (int i = 0; i < 100005; i++)
    cost[i] = INT_MAX;
  cost[0] = 0;
  cost[1] = abs(h[0] - h[1]);
  for (int i = 0; i < N; i++) {
    for (int k = 1; k <= min(N, K); k++) {
      cost[i + k] = min(cost[i + k], cost[i] + abs(h[i] - h[i + k]));
    }
  }
  cout << cost[N - 1] << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

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

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

int main() {
  int N, K;
  int h[100005];
  cin >> N >> K;
  rep(i, N) cin >> h[i];

  ll cost[100005];
  for (int i = 0; i < 100005; i++)
    cost[i] = INT_MAX;
  cost[0] = 0;
  cost[1] = abs(h[0] - h[1]);
  for (int i = 0; i < N; i++) {
    for (int k = 1; k <= min(N - i - 1, K); k++) {
      cost[i + k] = min(cost[i + k], cost[i] + abs(h[i] - h[i + k]));
    }
  }
  cout << cost[N - 1] << endl;
}
Problem p03161 (C++)

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

int main() {
  int N, K;
  vector<int> Distances;
  vector<int> Heights;
  cin >> N >> K;
  Distances.reserve(N);
  Heights.reserve(N);
  int i, j;
  int min = 2147483647;
  int dist;
  for (i = 0; i < N; i++) {
    cin >> Heights[i];
    if (i == 0) {
      Distances[i] = 0;
    } else {
      for (j = i - K; j < i; j++) {
        dist = Distances[j] + abs(Heights[i] - Heights[j]);
        if (dist < min) {
          min = dist;
        }
      }
      Distances[i] = min;
      min = 2147483647;
    }
  }
  cout << Distances[N - 1];
  // for(i=0;i<N;i++)
  return 0;
}


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

int main() {
  int N, K;
  vector<int> Distances;
  vector<int> Heights;
  cin >> N >> K;
  Distances.reserve(N);
  Heights.reserve(N);
  int i, j;
  int min = 2147483647;
  int dist;
  for (i = 0; i < N; i++) {
    cin >> Heights[i];
    if (i == 0) {
      Distances[i] = 0;
    } else if (i < K) {
      for (j = 0; j < i; j++) {
        dist = Distances[j] + abs(Heights[i] - Heights[j]);
        if (dist < min) {
          min = dist;
        }
      }
      Distances[i] = min;
      min = 2147483647;
    } else {
      for (j = i - K; j < i; j++) {
        dist = Distances[j] + abs(Heights[i] - Heights[j]);
        if (dist < min) {
          min = dist;
        }
      }
      Distances[i] = min;
      min = 2147483647;
    }
  }
  cout << Distances[N - 1];
  // for(i=0;i<N;i++)
  return 0;
}
Problem p03161 (C++)

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

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

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

int main() {
  const long long INF = 1ll << 60;
  int n, k;
  cin >> n >> k;
  long long h[100010];
  for (int i = 0; i < n; i++)
    cin >> h[i];

  long long dp[100010];
  for (int i = 0; i < 100010; i++)
    dp[i] = INF;

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

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

int main() {
  const long long INF = 1ll << 60;
  int n, k;
  cin >> n >> k;
  long long h[100010];
  for (int i = 0; i < n; i++)
    cin >> h[i];

  long long dp[100010];
  for (int i = 0; i < 100010; i++)
    dp[i] = INF;

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> dis(n);

  for (int i = 0; i < n; i++) {
    cin >> dis[i];
    // cout<<dis[i]<<" ";
  } // cout<<"\n";
  vector<int> costs(n, INT_MAX);
  costs[0] = 0;
  for (int i = 1; i < n; i++) {
    int count = k, j = i - 1;
    while (count-- && j >= 0) {
      int cost = costs[j] + abs(dis[i] - dis[j]);
      // cout<<cost<<"\n";
      if (cost < costs[i])
        costs[i] = cost;
      j--;
    }
  }
  cout << costs[n - 1];
  return 1;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> dis(n);

  for (int i = 0; i < n; i++) {
    cin >> dis[i];
    // cout<<dis[i]<<" ";
  } // cout<<"\n";
  vector<int> costs(n, INT_MAX);
  costs[0] = 0;
  for (int i = 1; i < n; i++) {
    int count = k, j = i - 1;
    while (count-- && j >= 0) {
      int cost = costs[j] + abs(dis[i] - dis[j]);
      // cout<<cost<<"\n";
      if (cost < costs[i])
        costs[i] = cost;
      j--;
    }
  }
  cout << costs[n - 1];
  return 0;
}
Problem p03161 (C++)

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

int min_cost(int *arr, int n, int k) {

  int *dp = new int[n + 1];

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

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

    dp[i] = temp;
  }

  int ans = dp[n];
  delete[] dp;
  return ans;
}

int main() {

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

  cout << min_cost(arr, n, k);

  return 0;
}

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

int min_cost(int *arr, int n, int k) {

  int *dp = new int[n + 1];

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

  for (int i = 3; i <= n; ++i) {
    int temp = INT_MAX;
    for (int j = 1; j <= k && (i - j) > 0; ++j) {
      temp = min(temp, abs(arr[i] - arr[i - j]) + dp[i - j]);
    }

    dp[i] = temp;
  }

  int ans = dp[n];
  delete[] dp;
  return ans;
}

int main() {

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

  cout << min_cost(arr, n, k);

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long int ll;

int main() {
  int N, K, dp[1000] = {};
  cin >> N >> K;
  vector<int> h(N);
  for (int i = 0; i < N; i++)
    cin >> h[i];
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < N; i++) {
    dp[i] = 1000000000;
    for (int j = max(i - K, 0); j < i; j++) {
      dp[i] = min(dp[i], dp[j] + abs(h[j] - h[i]));
    }
  }
  cout << dp[N - 1];
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long int ll;

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

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

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

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

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

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

  cout << minimumcost(n, k, v);
}

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

int minimumcost(int n, int k, vector<int> &v) {
  if (k >= n) {
    return v[n - 1] - v[0];
  }
  vector<int> dp(n, 0);
  dp[0] = 0;
  for (int i = 1; i <= k; i++) {
    dp[i] = abs(v[i] - v[0]);
  }

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

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

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

  cout << minimumcost(n, k, v);
}
Problem p03161 (C++)

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

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

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

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

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  vector<int> ht;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    ht.push_back(x);
  }
  long long cost[100010] = {0};
  for (int i = 2; i <= n; i++) {
    cost[i] = pow(2, 30);
  }
  for (int i = 1; i <= n; i++) {
    for (int k = 1; k <= m; k++) {
      cost[i + k] = min(cost[i + k], cost[i] + abs(ht[i + k - 1] - ht[i - 1]));
    }
  }
  cout << cost[n] << endl;
  return 0;
}

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  vector<int> ht;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    ht.push_back(x);
  }
  long long cost[100010] = {0};
  for (int i = 2; i <= n; i++) {
    cost[i] = pow(2, 30);
  }
  for (int i = 1; i <= n; i++) {
    for (int k = 1; k <= m; k++) {
      if (i + k - 1 < n)
        cost[i + k] =
            min(cost[i + k], cost[i] + abs(ht[i + k - 1] - ht[i - 1]));
    }
  }
  cout << cost[n] << endl;
  return 0;
}
Problem p03161 (C++)

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

int absdiff(int a, int b) { return (abs(a - b)); }

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

  vector<int> cost(n, 0);
  cost[1] = abs(height[1] - height[0]);
  for (int i = 2; i < n; i++) {
    int min_val = 1e9;
    for (int j = 1; j <= k; j++) {
      int temp = cost[i - j] + absdiff(height[i - j], height[i]);
      min_val = min(min_val, temp);
    }
    cost[i] = min_val;
  }

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

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

int absdiff(int a, int b) { return (abs(a - b)); }

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

  vector<int> cost(n, 0);
  cost[1] = abs(height[1] - height[0]);
  for (int i = 2; i < n; i++) {
    int min_val = 1e9;
    for (int j = 1; j <= k and i - j >= 0; j++) {
      int temp = cost[i - j] + absdiff(height[i - j], height[i]);
      min_val = min(min_val, temp);
    }
    cost[i] = min_val;
  }

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

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

#define ll long long
#define llinf 4e17
#define nllinf -4e18

#define vll vector<ll>
#define sll set<ll>
#define pll pair<ll, ll>
#define vpll vector<pair<ll, ll>>

#define repup(a, b, i) for (i = a; i <= b; i++)
#define repdown(a, b, i) for (i = b; i >= a; i--)
#define repupx(a, b, i, x) for (i = a; i <= b; i += x)

#define repit(v, it) for (it = v.begin(); it != v.end(); it++)
#define itall(v) v.begin(), v.end()
#define pb push_back
#define pf push_front
#define mp make_pair
#define fr first
#define sc second

#define err(n) cout << n << " ";
#define t1 cout << ",";

ll MOD = 998244353;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll i, j;

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

  ll a[n + 5];
  repup(0, n - 1, i) cin >> a[i];

  ll ans[n + 5];
  ans[0] = 0;
  repup(1, k - 1, i) {
    ans[i] = abs(a[i] - a[0]);
    repup(1, i - 1, j) { ans[i] = min(ans[i], ans[j] + abs(a[i] - a[j])); }
  }
  repup(k, n - 1, i) {
    ans[i] = (ll)llinf;
    repdown(1, k, j) {
      ans[i] = min(ans[i], ans[i - j] + abs(a[i] - a[i - j]));
    }
  }

  cout << ans[n - 1];
}

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

#define ll long long
#define llinf 4e17
#define nllinf -4e18

#define vll vector<ll>
#define sll set<ll>
#define pll pair<ll, ll>
#define vpll vector<pair<ll, ll>>

#define repup(a, b, i) for (i = a; i <= b; i++)
#define repdown(a, b, i) for (i = b; i >= a; i--)
#define repupx(a, b, i, x) for (i = a; i <= b; i += x)

#define repit(v, it) for (it = v.begin(); it != v.end(); it++)
#define itall(v) v.begin(), v.end()
#define pb push_back
#define pf push_front
#define mp make_pair
#define fr first
#define sc second

#define err(n) cout << n << " ";
#define t1 cout << ",";

ll MOD = 998244353;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll i, j;

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

  ll a[n + 5];
  repup(0, n - 1, i) cin >> a[i];

  ll ans[n + 5];
  ans[0] = 0;
  repup(1, min(n - 1, k - 1), i) {
    ans[i] = abs(a[i] - a[0]);
    repup(1, i - 1, j) { ans[i] = min(ans[i], ans[j] + abs(a[i] - a[j])); }
  }
  repup(k, n - 1, i) {
    ans[i] = (ll)llinf;
    repdown(1, k, j) {
      ans[i] = min(ans[i], ans[i - j] + abs(a[i] - a[i - j]));
    }
  }

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

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

ll INF = 101010101010;
int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> h(n);
  vector<ll> dp(n);
  rep(i, n) cin >> h[i];
  rep(i, n) dp[i] = INF;

  dp[0] = 0;
  for (int i = 1; i < n; i++) {

    for (int j = 0; j < i; j++) {
      if (j < i - k)
        continue;
      dp[i] = min(dp[i], abs(h[i] - h[j]) + dp[j]);
    }
    /*
    最高２こ飛ばしの場合
    dp[i]=dp[i-1]+abs(h[i]-h[i-1]);
    if(i>=2)dp[i]=min(dp[i],dp[i-2]+abs(h[i]-h[i-2]));
    */
  }

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


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

ll INF = 101010101010;
int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> h(n);
  vector<ll> dp(n);
  rep(i, n) cin >> h[i];
  rep(i, n) dp[i] = INF;

  dp[0] = 0;
  for (int i = 1; i < n; i++) {

    for (int j = i - k; j < i; j++) {
      if (j < 0)
        continue;
      dp[i] = min(dp[i], abs(h[i] - h[j]) + dp[j]);
    }
    /*
    最高２こ飛ばしの場合
    dp[i]=dp[i-1]+abs(h[i]-h[i-1]);
    if(i>=2)dp[i]=min(dp[i],dp[i-2]+abs(h[i]-h[i-2]));
    */
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int dp[1000000];
int ans(vector<int> &v, int n, int i, int k) {
  if (i == 0)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  int mn = INT_MAX;
  for (int j = i - 1; j >= i - k; j--) {
    int p = ans(v, n, j, k) + abs(v[j] - v[i]);
    mn = min(p, mn);
  }
  return dp[i] = mn;
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  for (int i = 0; i < 1000000; i++)
    dp[i] = -1;
  int a = ans(v, n, n - 1, k);
  cout << a;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[1000000];
int ans(vector<int> &v, int n, int i, int k) {
  if (i == 0)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  int mn = INT_MAX;
  for (int j = i - 1; j >= max(0, i - k); j--) {
    int p = ans(v, n, j, k) + abs(v[j] - v[i]);
    mn = min(p, mn);
  }
  return dp[i] = mn;
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  for (int i = 0; i < 1000000; i++)
    dp[i] = -1;
  int a = ans(v, n, n - 1, k);
  cout << a;
}
Problem p03161 (C++)

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

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

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

  dp[0] = 0;

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

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

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

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

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

  dp[0] = 0;

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

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

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

#include <bits/stdc++.h>
#include <cmath>
#include <sstream>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORD(i, a, b) for (int i = (a - 1); i >= (b); i--)
using namespace std;

#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define sz(w) (int)w.size()

#define FASTER                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

typedef vector<int> vi;
typedef long long int lli;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;

int main() {
  int n, k;
  cin >> n >> k;
  vi arr(n + 1);
  FOR(i, 1, n + 1) cin >> arr[i];
  // cout<<getAns(1);
  int dp[n + 1];
  FOR(i, 0, n + 1) dp[i] = INT_MAX;
  FOR(i, 1, k + 1) dp[i] = arr[i];
  // FOR(i, 1, n+1)cout<<dp[i]<<" ";cout<<endl;
  FOR(i, 1, n + 1) {
    FOR(j, 1, k + 1) {
      if (i - j > 0)
        dp[i] = min(dp[i], dp[i - j] + abs(arr[i] - arr[i - j]));
    }
  }
  // FOR(i, 1, n+1)cout<<dp[i]<<" ";cout<<endl;
  cout << dp[n] << endl;
  return 0;
}


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

#include <bits/stdc++.h>
#include <cmath>
#include <sstream>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORD(i, a, b) for (int i = (a - 1); i >= (b); i--)
using namespace std;

#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define sz(w) (int)w.size()

#define FASTER                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

typedef vector<int> vi;
typedef long long int lli;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;

int main() {
  int n, k;
  cin >> n >> k;
  vi arr(n + 1);
  FOR(i, 1, n + 1) cin >> arr[i];
  // cout<<getAns(1);
  int dp[n + 1];
  FOR(i, 1, n + 1) dp[i] = INT_MAX;
  dp[0] = dp[1] = 0;
  FOR(i, 1, n + 1) {
    FOR(j, 1, k + 1) {
      if (i - j > 0)
        dp[i] = min(dp[i], dp[i - j] + abs(arr[i] - arr[i - j]));
    }
  }
  // FOR(i, 1, n+1)cout<<dp[i]<<" ";cout<<endl;
  cout << dp[n] << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
// Resting_Prince
#include <bits/stdc++.h>
#define mod 1000000007
#define inf 1e18
#define PI 3.141592653589
#define fi first
#define se second
#define sv(v)                                                                  \
  sort(all(v));                                                                \
  reverse(all(v))
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define mk make_pair
#define en '\n'
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef long double ld;

int main() {
  ll n, h[100005], i, j, k, l, dp[100005];
  cin >> n >> k;
  rep(i, 0, n - 1) cin >> h[i + 1], dp[i + 1] = inf;
  dp[0] = 0;
  dp[1] = 0;
  rep(i, 1, n) {
    rep(j, 1, k) dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
  }
  cout << dp[n];
  return 0;
}


Fixed submission:
// Resting_Prince
#include <bits/stdc++.h>
#define mod 1000000007
#define inf 1e18
#define PI 3.141592653589
#define fi first
#define se second
#define sv(v)                                                                  \
  sort(all(v));                                                                \
  reverse(all(v))
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define mk make_pair
#define en '\n'
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef long double ld;

int main() {
  ll n, h[200005], i, j, k, l, dp[200005];
  cin >> n >> k;
  rep(i, 0, n - 1) cin >> h[i + 1], dp[i + 1] = inf;
  dp[0] = 0;
  dp[1] = 0;
  rep(i, 1, n) {
    rep(j, 1, k) dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
  }
  cout << dp[n];
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <math.h>
#include <unordered_map>
#include <vector>
using namespace std;

#define ll long long
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define pb push_back

ll dp[100001];

int solve(int i, int *h, int n, int k) {

  if (i == n) {
    dp[n] = 0;
    return dp[n];
  }

  ll ans = 1000000009;
  for (int j = 1; j <= k and i + j <= n; ++j) {
    if (dp[i + j] == -1) {
      dp[i + j] = solve(i + j, h, n, k);
    }
    ans = min(ans, abs(h[i] - h[i + j]) + dp[i + j]);
  }
  dp[i] = ans;
  return ans;
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

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

  for (int i = 0; i < 100001; ++i)
    dp[i] = -1;

  cout << solve(1, h, n, k);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <math.h>
#include <unordered_map>
#include <vector>
using namespace std;

#define ll long long
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define pb push_back

ll dp[100001];

int solve(int i, int *h, int n, int k) {

  if (i == n) {
    dp[n] = 0;
    return dp[n];
  }

  ll ans = 1000000009;
  for (int j = 1; j <= k and i + j <= n; ++j) {
    if (dp[i + j] == -1) {
      dp[i + j] = solve(i + j, h, n, k);
    }
    ans = min(ans, abs(h[i] - h[i + j]) + dp[i + j]);
  }
  dp[i] = ans;
  return ans;
}

int main() {

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

  for (int i = 0; i < 100001; ++i)
    dp[i] = -1;

  cout << solve(1, h, n, k);

  return 0;
}
Problem p03161 (C++)

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


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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n) for (int i = 1; i <= (n); i++)
#define Jrep(j, n) for (int j = 0; j < (n); j++)
#define Jreps(j, n) for (int j = 1; j <= (n); j++)
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;

using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  vector<int> dp(100010, INF);
  cin >> n >> k;
  int h[100010];
  reps(i, n) { cin >> h[i]; }
  dp[1] = 0;
  dp[2] = abs(h[1] - h[2]);
  for (int i = 3; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      dp[i] = min(abs(h[i] - h[i - j]) + dp[i - j], dp[i]);
    }
  }
  cout << dp[n] << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n) for (int i = 1; i <= (n); i++)
#define Jrep(j, n) for (int j = 0; j < (n); j++)
#define Jreps(j, n) for (int j = 1; j <= (n); j++)
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;

using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  vector<int> dp(100010, INF);
  cin >> n >> k;
  int h[100010];
  reps(i, n) { cin >> h[i]; }
  dp[1] = 0;
  dp[2] = abs(h[1] - h[2]);
  for (int i = 3; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      if (i - j == 0)
        break;
      dp[i] = min(abs(h[i] - h[i - j]) + dp[i - j], dp[i]);
    }
  }
  cout << dp[n] << '\n';
  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++)
#define FOR(i, n, m) for (int i = n; i < m; i++)
#define ll long long int

using namespace std;

int main() {
  int n, k;
  int h[10005];
  int dp[10005] = {};
  cin >> n >> k;
  REP(i, n) { cin >> h[i]; }

  REP(i, n) {
    int result = 1110001110;
    FOR(j, 1, k + 1) {
      if (i <= k) {
        result = abs(h[i] - h[0]);
        break;
      }

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

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


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

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n, m) for (int i = n; i < m; i++)
#define ll long long int

using namespace std;

int main() {
  int n, k;
  int h[100000];
  int dp[100000] = {};
  cin >> n >> k;
  REP(i, n) { cin >> h[i]; }

  REP(i, n) {
    int result = 1110001110;
    FOR(j, 1, k + 1) {
      if (i <= k) {
        result = abs(h[i] - h[0]);
        break;
      }

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 99999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(20) << S << endl
#define Vecpr(K, L1, L2, N) vector<pair<L1, L2>> K(N)
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, R) vector<vector<L>> K(N, vector<L>(M, R))
#define pint pair<int, int>
#define Lower(v, X) lower_bound(v.begin(), v.end(), X) - v.begin();
#define mod 1000000007
#define MAX 5100000
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
  lli A, B, C, L, R, N, M, K, X = 0, Y = 0, W, H = INF, sum = 0, num = 0,
                              flag = 0;
  string S, T, O;
  cin >> N >> M;
  Vec(P, lli, N, 0);
  Vec(DP, lli, 10000, INF);
  DP[0] = 0;
  rep(i, 0, N) cin >> P[i];
  rep(i, 0, N) {
    rep(j, 1, M + 1) {
      if (i + j > N - 1)
        continue;
      chmin(DP[i + j], DP[i] + abs(P[i + j] - P[i]));
    }
  }
  Out(DP[N - 1]);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 99999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(20) << S << endl
#define Vecpr(K, L1, L2, N) vector<pair<L1, L2>> K(N)
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, R) vector<vector<L>> K(N, vector<L>(M, R))
#define pint pair<int, int>
#define Lower(v, X) lower_bound(v.begin(), v.end(), X) - v.begin();
#define mod 1000000007
#define MAX 5100000
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
  lli A, B, C, L, R, N, M, K, X = 0, Y = 0, W, H = INF, sum = 0, num = 0,
                              flag = 0;
  string S, T, O;
  cin >> N >> M;
  Vec(P, lli, N, 0);
  Vec(DP, lli, 10000000, INF);
  DP[0] = 0;
  rep(i, 0, N) cin >> P[i];
  rep(i, 0, N) {
    rep(j, 1, M + 1) {
      if (i + j > N - 1)
        continue;
      chmin(DP[i + j], DP[i] + abs(P[i + j] - P[i]));
    }
  }
  Out(DP[N - 1]);
}
Problem p03161 (C++)

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

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  rep(i, n) cin >> h[i];
  cout << solve(n, k, h) << 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;
const ll INF = 10e9;

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  rep(i, n) cin >> h[i];
  cout << solve(n, k, h) << endl;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long dp[10000];
int main(void) {
  int N, K;
  cin >> N >> K;
  long long h[N + 1];
  for (int i = 1; i <= N; i++)
    cin >> h[i];
  // 現在地から+1 +2 ..+Kまで移動することが出来る
  // dp[x]はxまでいくのにかかる最小のコスト
  // 初期コストのセット
  for (int i = 1; i <= K + 1; i++) {
    dp[i] = abs(h[1] - h[i]);
  }
  // K+1~を計算して行くンゴ
  for (int i = K + 1; i <= N; i++) {
    long long temp1 = dp[i - 1] + abs(h[i - 1] - h[i]);
    long long temp2;
    for (int j = 1; j <= K; j++) {
      temp2 = dp[i - j] + abs(h[i - j] - h[i]);
      if (temp1 > temp2)
        temp1 = temp2;
    }
    dp[i] = temp1;
  }
  cout << dp[N];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long dp[100001];
int main(void) {
  int N, K;
  cin >> N >> K;
  long long h[N + 1];
  for (int i = 1; i <= N; i++)
    cin >> h[i];
  // 現在地から+1 +2 ..+Kまで移動することが出来る
  // dp[x]はxまでいくのにかかる最小のコスト
  // 初期コストのセット
  for (int i = 1; i <= K + 1; i++) {
    dp[i] = abs(h[1] - h[i]);
  }
  // K+1~を計算して行くンゴ
  for (int i = K + 1; i <= N; i++) {
    long long temp1 = dp[i - 1] + abs(h[i - 1] - h[i]);
    long long temp2;
    for (int j = 1; j <= K; j++) {
      temp2 = dp[i - j] + abs(h[i - j] - h[i]);
      if (temp1 > temp2)
        temp1 = temp2;
    }
    dp[i] = temp1;
  }
  cout << dp[N];
}