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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <stack>
using namespace std;
typedef long long ll;
typedef long double lxd;
#define INF 1000000007
#define mem(dp, a) memset(dp, a, sizeof dp)
#define flash                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define repb(i, a, b) for (ll i = a; i >= b; i--)
#define f(i, n) for (ll i = 0; i < n; i++)
#define fo(i, x, n) for (ll i = x; i <= n; i++)
#define pb(x) push_back(x)
#define tr(c, it) for (((c).begin())it = (c).begin(); it != (c).end(); it++)
#define test                                                                   \
  ll t;                                                                        \
  cin >> t;
#define fls fflush(stdout);
int main() {
  ll n, k;
  cin >> n >> k;
  ll height[n], dp[n];
  for (ll i = 0; i < n; i++)
    cin >> height[i];
  dp[0] = 0;
  dp[1] = abs(height[1] - height[0]);
  ll mn;
  for (ll i = 2; i < k + 1; i++) {
    mn = INT_MAX;
    for (ll j = i - 1; j >= 0; j--) {
      mn = min(mn, dp[j] + abs(height[j] - height[i]));
      dp[i] = mn;
    }
  }
  for (ll i = k + 1; i < n; i++) {
    mn = INT_MAX;
    for (ll j = i - 1; j >= i - k; j--) {
      mn = min(mn, dp[j] + abs(height[j] - height[i]));
      dp[i] = mn;
    }
  }
  // for(ll i=0;i<n;i++)cout<<dp[i]<<" ";cout<<endl;
  cout << dp[n - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <stack>
using namespace std;
typedef long long ll;
typedef long double lxd;
#define INF 1000000007
#define mem(dp, a) memset(dp, a, sizeof dp)
#define flash                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define repb(i, a, b) for (ll i = a; i >= b; i--)
#define f(i, n) for (ll i = 0; i < n; i++)
#define fo(i, x, n) for (ll i = x; i <= n; i++)
#define pb(x) push_back(x)
#define tr(c, it) for (((c).begin())it = (c).begin(); it != (c).end(); it++)
#define test                                                                   \
  ll t;                                                                        \
  cin >> t;
#define fls fflush(stdout);
int main() {
  ll n, k;
  cin >> n >> k;
  ll height[n], dp[n];
  for (ll i = 0; i < n; i++)
    cin >> height[i];
  dp[0] = 0;
  dp[1] = abs(height[1] - height[0]);
  ll mn;
  for (ll i = 2; i < k + 1 && i < n; i++) {
    mn = INT_MAX;
    for (ll j = i - 1; j >= 0; j--) {
      mn = min(mn, dp[j] + abs(height[j] - height[i]));
      dp[i] = mn;
    }
  }
  for (ll i = k + 1; i < n; i++) {
    mn = INT_MAX;
    for (ll j = i - 1; j >= i - k; j--) {
      mn = min(mn, dp[j] + abs(height[j] - height[i]));
      dp[i] = mn;
    }
  }
  // for(ll i=0;i<n;i++)cout<<dp[i]<<" ";cout<<endl;
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
// In The Name Of God
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define sqr(A) ((A) * (A))
#define X first
#define Y second
#define MP make_pair
#define bsz __builtin_popcount
#define all(A) A.begin(), A.end()
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int MOD2 = 1e9 + 9;
const int PR = 727;
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;

const int N = 1e5 + 20;
ll dp[N], n, h[N], k;

int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = n - 2; i >= 0; i--) {
    dp[i] = INF;
    for (int j = i + 1; j <= i + k; j++)
      dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]));
  }
  cout << dp[0] << endl;
}


Fixed submission:
// In The Name Of God
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define sqr(A) ((A) * (A))
#define X first
#define Y second
#define MP make_pair
#define bsz __builtin_popcount
#define all(A) A.begin(), A.end()
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int MOD2 = 1e9 + 9;
const int PR = 727;
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;

const int N = 1e5 + 20;
ll dp[N], n, h[N], k;

int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = n - 2; i >= 0; i--) {
    dp[i] = INF;
    for (int j = i + 1; j <= min(i + k, n - 1); j++)
      dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]));
  }
  cout << dp[0] << endl;
}
Problem p03161 (C++)

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

  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll dp[n];
  dp[0] = 0;
  for (int i = 1; i < n; i++)
    dp[i] = 1e9;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j <= i + k; j++)
      dp[j] = min(dp[j], abs(a[i] - a[j]) + dp[i]);
  }
  cout << dp[n - 1];
  return 0;
}


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

  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll dp[n];
  dp[0] = 0;
  for (int i = 1; i < n; i++)
    dp[i] = 1e9;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j <= i + k; j++)
      if (j < n)
        dp[j] = min(dp[j], abs(a[i] - a[j]) + dp[i]);
      else
        break;
  }
  cout << dp[n - 1];
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, k;
  cin >> n >> k;
  ll arr[n + 1];
  for (int a = 1; a <= n; a++)
    cin >> arr[a];
  ll dp[n + 1];
  dp[1] = 0;
  for (int a = 2; a <= k + 1; a++) {
    dp[a] = abs(arr[a] - arr[1]);
  }
  for (int a = k + 2; a <= n; a++) {
    ll x, y, z;
    ll mini = 1000000000000000;
    for (int b = 1; b <= k; b++) {
      x = dp[a - b] + abs(arr[a] - arr[a - b]);
      mini = min(mini, x);
    }
    dp[a] = mini;
  }
  cout << dp[n] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, k;
  cin >> n >> k;
  ll arr[n + 1];
  for (int a = 1; a <= n; a++)
    cin >> arr[a];
  ll dp[n + 1];
  dp[1] = 0;
  for (int a = 2; a <= min(n, k + 1); a++) {
    dp[a] = abs(arr[a] - arr[1]);
  }
  for (int a = k + 2; a <= n; a++) {
    ll x, y, z;
    ll mini = 1000000000000000;
    for (int b = 1; b <= k; b++) {
      x = dp[a - b] + abs(arr[a] - arr[a - b]);
      mini = min(mini, x);
    }
    dp[a] = mini;
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p03161 (C++)

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

using namespace std;

inline int ctoi(char c) {
  if (c < '0' || '9' < c)
    throw invalid_argument("ctoi error");
  return c - '0';
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

template <typename T> inline T gcd(T x, T y) {
  if (x <= 0 || y <= 0)
    throw invalid_argument("gcd error: x <= 0 or y <= 0");

  if (x < y)
    swap(x, y);
  T r = x % y;

  while (r != 0) {
    x = y;
    y = r;
    r = x % y;
  }

  return y;
}
template <typename T> inline T lcm(T x, T y) {
  if (x <= 0 || y <= 0)
    throw invalid_argument("lcm error: x <= 0 or y <= 0");

  return x * y / gcd(x, y);
}

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

  vector<int> vec;
  rep(i, n) {
    int x;
    cin >> x;
    vec.push_back(x);
  }

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

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

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

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

using namespace std;

inline int ctoi(char c) {
  if (c < '0' || '9' < c)
    throw invalid_argument("ctoi error");
  return c - '0';
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

template <typename T> inline T gcd(T x, T y) {
  if (x <= 0 || y <= 0)
    throw invalid_argument("gcd error: x <= 0 or y <= 0");

  if (x < y)
    swap(x, y);
  T r = x % y;

  while (r != 0) {
    x = y;
    y = r;
    r = x % y;
  }

  return y;
}
template <typename T> inline T lcm(T x, T y) {
  if (x <= 0 || y <= 0)
    throw invalid_argument("lcm error: x <= 0 or y <= 0");

  return x * y / gcd(x, y);
}

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

  vector<int> vec;
  rep(i, n) {
    int x;
    cin >> x;
    vec.push_back(x);
  }

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

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

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

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

long long frog(long long arr[], int n, int k) {
  long long output[n + 1];
  for (int i = 0; i < n; i++)
    output[i] = INT_MAX;
  output[0] = 0;
  for (int i = 1; i < k; i++) {
    for (int j = i - 1; j >= 0; j--) {
      if (output[j] + abs(arr[i] - arr[j]) < output[i])
        output[i] = output[j] + abs(arr[i] - arr[j]);
    }
  }
  for (int i = k; i < n; i++) {
    for (int j = i - 1; j >= i - k; j--) {
      if (output[j] + abs(arr[i] - arr[j]) < output[i])
        output[i] = output[j] + abs(arr[i] - arr[j]);
    }
  }
  return output[n - 1];
}

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

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAX 100007
#define MOD 1000000007

long long frog(long long arr[], int n, int k) {
  long long output[n + 1];
  for (int i = 0; i < n; i++)
    output[i] = INT_MAX;
  output[0] = 0;
  for (int i = 1; i < min(k, n); i++) {
    for (int j = i - 1; j >= 0; j--) {
      if (output[j] + abs(arr[i] - arr[j]) < output[i])
        output[i] = output[j] + abs(arr[i] - arr[j]);
    }
  }
  for (int i = k; i < n; i++) {
    for (int j = i - 1; j >= i - k; j--) {
      if (output[j] + abs(arr[i] - arr[j]) < output[i])
        output[i] = output[j] + abs(arr[i] - arr[j]);
    }
  }
  return output[n - 1];
}

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

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

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
int v[N];
int dp[N];
int n, k;
int calc(int idx) {
  if (idx >= n - 1)
    return 0;
  int &ret = dp[idx];
  if (ret != -1)
    return ret;
  ret = 1e9;
  for (int i = 1; i <= k; ++i)
    if (idx + i < n)
      ret = min(ret, calc(idx + i) + abs(v[idx] - v[idx + i]));
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("test.txt", "r", stdin);
#endif
  cin >> n >> k;
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  memset(dp, -1, sizeof(dp));
  cout << calc(0) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
int v[N];
int dp[N];
int n, k;
int calc(int idx) {
  if (idx >= n - 1)
    return 0;
  int &ret = dp[idx];
  if (ret != -1)
    return ret;
  ret = 1e9;
  for (int i = 1; i <= k; ++i)
    if (idx + i < n)
      ret = min(ret, calc(idx + i) + abs(v[idx] - v[idx + i]));
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  memset(dp, -1, sizeof(dp));
  cout << calc(0) << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
void solve() {
  ll n, i, j, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (i = 0; i < n; i++)
    cin >> a[i];
  ll dp[n];
  dp[0] = 0;
  dp[1] = abs(a[0] - a[1]);
  ll m = INT_MAX;
  for (i = 2; i < n; i++) {
    for (j = i - 1; j >= (i - k > 0 ? i - k : 0); j--) {
      m = min(m, abs(a[i] - a[j]) + dp[j]);
      // if(i==n-1)
      // cout<<abs(a[i]-a[j])<<" "<<dp[i-j]<<" "<<i<<" "<<j<<"\n";
    }
    dp[i] = m;
    m = INT_MAX;
  }
  cout << dp[n - 1];
  // for(i=0;i<n;i++)
  // cout<<dp[i]<<" ";
}
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
  clock_t tStart = clock();
  ll t, k;
  // cin>>t;
  // while(t--)
  solve();
  // printf("\nTime taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
void solve() {
  ll n, i, j, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (i = 0; i < n; i++)
    cin >> a[i];
  ll dp[n];
  dp[0] = 0;
  dp[1] = abs(a[0] - a[1]);
  ll m = INT_MAX;
  for (i = 2; i < n; i++) {
    for (j = i - 1; j >= (i - k > 0 ? i - k : 0); j--) {
      m = min(m, abs(a[i] - a[j]) + dp[j]);
      // if(i==n-1)
      // cout<<abs(a[i]-a[j])<<" "<<dp[i-j]<<" "<<i<<" "<<j<<"\n";
    }
    dp[i] = m;
    m = INT_MAX;
  }
  cout << dp[n - 1];
  // for(i=0;i<n;i++)
  // cout<<dp[i]<<" ";
}
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
  clock_t tStart = clock();
  ll t, k;
  // cin>>t;
  // while(t--)
  solve();
  // printf("\nTime taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
  return 0;
}
Problem p03161 (C++)

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける
int MOD = 1000000007;   // 10^9 + 7

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 argc, char *argv[]) {
  int n, k;
  cin >> n >> k;
  vi h(n);
  rep(i, n) cin >> h[i];

  // 無限大の値
  const long long INF = 1LL << 60;

  // DP テーブル
  long long dp[100010];

  // DP テーブル全体を初期化 (最小化問題:INF else:0)
  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 + 1; ++j) {
      chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
    }
  }
  // 解を得て出力
  cout << dp[n - 1] << endl;
  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける
int MOD = 1000000007;   // 10^9 + 7

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 argc, char *argv[]) {
  int n, k;
  cin >> n >> k;
  vi h(n);
  rep(i, n) cin >> h[i];

  // 無限大の値
  const long long INF = 1LL << 60;

  // DP テーブル
  long long dp[100010];

  // DP テーブル全体を初期化 (最小化問題:INF else:0)
  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 + 1; ++j) {
      if (i + j < n)
        chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
    }
  }
  // 解を得て出力
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

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

int main() {
  int N;
  cin >> N;
  int K;
  cin >> K;
  vector<int> h(N);
  REP(i, N) cin >> h[i];
  vector<int> dp(N, INF);
  dp[0] = 0;
  FOR(i, 1, N) {
    FOR(j, max(i - K, 0), N) { dp[i] = min(dp[i], dp[j] + abs(h[j] - h[i])); }
  }
  cout << dp[N - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define lsb(x) (x & -x)

using namespace std;

int main() {
  ios::sync_with_stdio(false);

  cin.tie(0);
  cout.tie(0);
  // freopen("a.in", "r", stdin);

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

  vector<int> dp(n + 1, 1000000000);
  dp[1] = 0;
  for (int i = 2; i <= n; i++)
    for (int j = i - 1; j >= i - k; j--)
      dp[i] = min(dp[i], dp[j] + abs(v[i] - v[j]));
  cout << dp[n];

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define lsb(x) (x & -x)

using namespace std;

int main() {
  ios::sync_with_stdio(false);

  cin.tie(0);
  cout.tie(0);
  // freopen("a.in", "r", stdin);

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

  vector<int> dp(n + 1, 1000000000);
  dp[1] = 0;
  for (int i = 2; i <= n; i++)
    for (int j = i - 1; j >= max(i - k, 1); j--)
      dp[i] = min(dp[i], dp[j] + abs(v[i] - v[j]));
  cout << dp[n];

  return 0;
}
Problem p03161 (C++)

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

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

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

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


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

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

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, x, n) for (int i = (x); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e9;
template <typename T> void cmax(T &a, T b) { a = max(a, b); }
template <typename T> void cmin(T &a, T b) { a = min(a, b); }

signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  rep(i, n) cin >> h[i];
  vector<int> dp(n + 10, INF);
  dp[0] = 0;
  rep(i, n) loop(j, 1, k + 1) cmin(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
  cout << dp[n - 1] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, x, n) for (int i = (x); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e9;
template <typename T> void cmax(T &a, T b) { a = max(a, b); }
template <typename T> void cmin(T &a, T b) { a = min(a, b); }

signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  rep(i, n) cin >> h[i];
  vector<int> dp(n + 200, INF);
  dp[0] = 0;
  rep(i, n) loop(j, 1, k + 1) cmin(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
/**
 *      purpose :
 *      author  : kyomukyomupurin
 *      created :
 **/

#include <bits/stdc++.h>
using namespace std;
using int64 = int64_t;
using pii = pair<int, int>;
using pll = pair<int64_t, int64_t>;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define print(x) cout << (x) << '\n'
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define fi first
#define se second

const int64 INF = 1e15;

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

  int n, k;
  cin >> n >> k;
  int64 h[n];
  rep(i, n) cin >> h[i];

  int64 dp[n]; // dp[i] : minimum const to reach 'i'
  fill(dp, dp + n, INF);

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

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

  return 0;
}

Fixed submission:
/**
 *      purpose :
 *      author  : kyomukyomupurin
 *      created :
 **/

#include <bits/stdc++.h>
using namespace std;
using int64 = int64_t;
using pii = pair<int, int>;
using pll = pair<int64_t, int64_t>;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define print(x) cout << (x) << '\n'
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define fi first
#define se second

const int64 INF = 1e15;

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

  int n, k;
  cin >> n >> k;
  int64 h[n];
  rep(i, n) cin >> h[i];

  int64 dp[100000 + 100]; // dp[i] : minimum const to reach 'i'
  rep(i, 100000 + 100) dp[i] = INF;

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

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

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define int long long int
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF 100000000000
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
int mpow(int base, int exp);
void ipgraph(int m);
const int mod = 1000000007;
const int N = 1e5 + 5, M = N;
//=========================================

int h[N], dp[N];

int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("inputf.in", "r", stdin);
  freopen("outputf.in", "w", stdout);
#endif

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

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

  dp[1] = 0;
  for (int i = 2; i <= n; ++i) {
    // dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]);
    dp[i] = 2 * 1e9;
    for (int j = i - 1; j >= max(i - k, 1LL); --j) {
      dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]));
    }
  }

  cout << dp[n] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define int long long int
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF 100000000000
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
int mpow(int base, int exp);
void ipgraph(int m);
const int mod = 1000000007;
const int N = 1e5 + 5, M = N;
//=========================================

int h[N], dp[N];

int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

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

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

  dp[1] = 0;
  for (int i = 2; i <= n; ++i) {
    // dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]);
    dp[i] = 2 * 1e9;
    for (int j = i - 1; j >= max(i - k, 1LL); --j) {
      dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]));
    }
  }

  cout << dp[n] << endl;

  return 0;
}
Problem p03161 (C++)

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

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

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

  std::vector<int> dp(n);

  for (int i = 0; i < k; i++) {
    dp[i] = std::abs(in[i] - in[0]);
  }

  // O(n*k)
  for (int i = 1; i < n; i++) {
    std::vector<int> currCosts;
    for (int j = 1; j <= k; j++) {
      if (i - j < 0) {
        break;
      }
      currCosts.push_back(dp[i - j] + std::abs(in[i] - in[i - j]));
    }
    dp[i] = *std::min_element(currCosts.begin(), currCosts.end());
  }

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

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

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

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

  std::vector<int> dp(n);

  // O(n*k)
  for (int i = 1; i < n; i++) {
    std::vector<int> currCosts;
    for (int j = 1; j <= k; j++) {
      if (i - j < 0) {
        break;
      }
      currCosts.push_back(dp[i - j] + std::abs(in[i] - in[i - j]));
    }
    dp[i] = *std::min_element(currCosts.begin(), currCosts.end());
  }

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

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

#define END '\n'
#define int long long
#define pb push_back
#define pii pair<int, int>
#define loop(i, a, b) for (int i = (a); i < (b); i++)
#define loopb(i, b, a) for (int i = (b); i > (a); --i)
void fastscan(int &x) {
  bool neg = false;
  register int c;
  x = 0;
  c = getchar();
  if (c == '-') {
    neg = true;
    c = getchar();
  }
  for (; (c > 47 && c < 58); c = getchar())
    x = (x << 1) + (x << 3) + c - 48;
  if (neg)
    x *= -1;
}
string alpha = "abcdefghijklmnopqrstuvwxyz";
const int mod = 1e9 + 7;
const int inf = 2e18 + 5;
const int nax = 100010;
const int mod1 = 998244353;

signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> v;
  loop(i, 0, n) {
    int x;
    cin >> x;
    v.pb(x);
  }
  int dp[n];
  loop(i, 0, n) dp[i] = inf;
  dp[0] = 0;
  loop(i, 0, n) {
    loop(j, i + 1, i + k + 1) { dp[j] = min(dp[i] + abs(v[j] - v[i]), dp[j]); }
  }
  cout << dp[n - 1];
  return 0;
}

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

#define END '\n'
#define int long long
#define pb push_back
#define pii pair<int, int>
#define loop(i, a, b) for (int i = (a); i < (b); i++)
#define loopb(i, b, a) for (int i = (b); i > (a); --i)
void fastscan(int &x) {
  bool neg = false;
  register int c;
  x = 0;
  c = getchar();
  if (c == '-') {
    neg = true;
    c = getchar();
  }
  for (; (c > 47 && c < 58); c = getchar())
    x = (x << 1) + (x << 3) + c - 48;
  if (neg)
    x *= -1;
}
string alpha = "abcdefghijklmnopqrstuvwxyz";
const int mod = 1e9 + 7;
const int inf = 2e18 + 5;
const int nax = 100010;
const int mod1 = 998244353;

signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> v;
  loop(i, 0, n) {
    int x;
    cin >> x;
    v.pb(x);
  }
  int dp[n];
  loop(i, 0, n) dp[i] = inf;
  dp[0] = 0;
  loop(i, 0, n) {
    loop(j, i + 1, min(i + k + 1, n)) {
      dp[j] = min(dp[i] + abs(v[j] - v[i]), dp[j]);
    }
  }
  cout << dp[n - 1];
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
/* Author: Rennan Rocha
 * Time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$
 **/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define F first
#define S second
#define PB push_back
#define MP make_pair

#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define pi pair<int, int>
#define pll pair<ll, ll>
#define vpi vector<pi>
#define vpll vector<pll>
#define INF 1000000000000000000

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
//*find_by_order(); order_of_key();

int n;
ll h[100010];
ll dp[100010];
int k;

ll solve(int i) {
  if (i == n - 1)
    return 0;
  if (i >= n)
    return INF;
  if (dp[i] != -1)
    return dp[i];

  for (int j = 1; j <= k; j++) {
    dp[i] = min(dp[i] == -1 ? INF : dp[i],
                min(abs(h[i] - h[i + j]) + solve(i + j),
                    abs(h[i] - h[i + j]) + solve(i + j)));
  }
  return dp[i];
}

int main() {
  ios_base::sync_with_stdio(false);
  memset(dp, -1, sizeof dp);
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  cout << solve(0) << "\n";
}


Fixed submission:
/* Author: Rennan Rocha
 * Time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$
 **/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define F first
#define S second
#define PB push_back
#define MP make_pair

#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define pi pair<int, int>
#define pll pair<ll, ll>
#define vpi vector<pi>
#define vpll vector<pll>
#define INF 1000000000000000000

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
//*find_by_order(); order_of_key();

int n;
ll h[100110];
ll dp[100110];
int k;

ll solve(int i) {
  if (i == n - 1)
    return 0;
  if (i >= n)
    return INF;
  if (dp[i] != -1)
    return dp[i];

  for (int j = 1; j <= k; j++) {
    dp[i] = min(dp[i] == -1 ? INF : dp[i],
                min(abs(h[i] - h[i + j]) + solve(i + j),
                    abs(h[i] - h[i + j]) + solve(i + j)));
  }
  return dp[i];
}

int main() {
  ios_base::sync_with_stdio(false);
  memset(dp, -1, sizeof dp);
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> h[i];
  cout << solve(0) << "\n";
}
Problem p03161 (C++)

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

typedef long long ll;
typedef double ld;
#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 F first
#define S second
#define maxx(a, b) a = max(a, b)
#define minn(a, b) a = min(a, b)
#define db(x) cerr << #x << " = " << x << endl
#define N 1111111
#define mod 1000000007
#define rt return
#define inf 12345678908761

using namespace std;

int main() {

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

  // freopen("Ulug'bek","r",stdin);
  // freopen("Abdimanabov","w",stdout);

  int n, k;
  cin >> n >> k;
  vector<int> v(n), ans(n, INT_MAX);
  for (auto &x : v)
    cin >> x;
  ans[0] = 0;
  rep(i, 2, n) rep(j, 1, k + 1) if (i - j >= 0)
      minn(ans[i], abs(v[i] - v[i - k]) + ans[i - k]);

  cout << ans[n - 1];

  rt 0;
}


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

typedef long long ll;
typedef double ld;
#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 F first
#define S second
#define maxx(a, b) a = max(a, b)
#define minn(a, b) a = min(a, b)
#define db(x) cerr << #x << " = " << x << endl
#define N 1111111
#define mod 1000000007
#define rt return
#define inf 12345678908761

using namespace std;

int main() {

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

  // freopen("Ulug'bek","r",stdin);
  // freopen("Abdimanabov","w",stdout);

  int n, k;
  cin >> n >> k;
  vector<int> v(n), ans(n, INT_MAX);
  for (auto &x : v)
    cin >> x;
  ans[0] = 0;
  rep(i, 1, n) rep(j, 1, k + 1) if (i - j >= 0)
      minn(ans[i], abs(v[i] - v[i - j]) + ans[i - j]);

  cout << ans[n - 1];

  rt 0;
}
Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;

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

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

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

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

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

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;

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

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

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

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

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

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define mod 1000000007
#define inf 1000000000000000007
#define eps 0.000001
#define pi 3.141592653589793
#define pii pair<int, int>
#define pdd pair<ld, ld>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define vii vector<int>
#define vpl vector<pll>
#define vll vector<ll>
#define sti stack<int>
#define stll stack<ll>
#define mseti set<ll>
#define msetd multiset<ll, greater<ll>>
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define stp setprecision(20) // use fixed before stp
#define endl '\n'
int main() {
  FAST
#ifndef ONLINE_JUDGE
      freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  ll n, k;
  cin >> n >> k;
  ll a[n], dp[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    dp[i] = inf;
  }
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (ll i = 2; i < n; i++) {
    for (ll j = (i - 1); j >= (i - k); j--) {
      if (j < 0)
        break;
      dp[i] = min(dp[i], (dp[j] + abs(a[j] - a[i])));
    }
  }
  cout << dp[n - 1];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define mod 1000000007
#define inf 1000000000000000007
#define eps 0.000001
#define pi 3.141592653589793
#define pii pair<int, int>
#define pdd pair<ld, ld>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define vii vector<int>
#define vpl vector<pll>
#define vll vector<ll>
#define sti stack<int>
#define stll stack<ll>
#define mseti set<ll>
#define msetd multiset<ll, greater<ll>>
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define stp setprecision(20) // use fixed before stp
#define endl '\n'
int main() {
  FAST ll n, k;
  cin >> n >> k;
  ll a[n], dp[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    dp[i] = inf;
  }
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (ll i = 2; i < n; i++) {
    for (ll j = (i - 1); j >= (i - k); j--) {
      if (j < 0)
        break;
      dp[i] = min(dp[i], (dp[j] + abs(a[j] - a[i])));
    }
  }
  cout << dp[n - 1];
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int frog(std::vector<int> v, int n, int k) {
  int dp[100001] = {0};
  dp[0] = 0;
  dp[1] = abs(v[0] - v[1]);
  for (int i = 2; i <= n; i++) {
    int ans = INT_MAX;
    for (int j = 1; j <= k; j++) {
      int a1 = dp[i - j] + abs(v[i] - v[i - j]);
      ans = min(ans, a1);
    }

    dp[i] = ans;
  }

  return dp[n - 1];
}
int main() {
  int n;
  cin >> n;
  int k;
  cin >> k;
  std::vector<int> v;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    v.push_back(a);
  }

  int ans = frog(v, n, k);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int frog(std::vector<int> v, int n, int k) {
  int dp[100001] = {0};
  dp[0] = 0;
  dp[1] = abs(v[0] - v[1]);
  for (int i = 2; i <= n; i++) {
    int ans = INT_MAX;
    for (int j = 1; j <= k; j++) {
      int a1 = 0;
      if ((i - j) >= 0) {
        a1 = dp[i - j] + abs(v[i] - v[i - j]);
      } else {
        break;
      }
      ans = min(ans, a1);
    }

    dp[i] = ans;
  }

  return dp[n - 1];
}
int main() {
  int n;
  cin >> n;
  int k;
  cin >> k;
  std::vector<int> v;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    v.push_back(a);
  }

  int ans = frog(v, n, k);
  cout << ans << endl;
  return 0;
}
Problem p03161 (C++)

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

using namespace std;

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

  int min_jumps[n];
  min_jumps[0] = 0;
  for (int i = 1; i < k; i++)
    min_jumps[i] = abs(arr[0] - arr[i]);
  for (int i = k; i < n; i++) {
    min_jumps[i] = INT_MAX;
    // cout << min_jumps[i] << " " << i << " " <<
    // abs(arr[i]-arr[i-1])+min_jumps[i-1] << endl;
    for (int j = 1; j <= k; j++) {
      if (min_jumps[i] > abs(arr[i] - arr[i - j]) + min_jumps[i - j])
        min_jumps[i] = abs(arr[i] - arr[i - j]) + min_jumps[i - j];
    }
  }
  // for(int i =0; i<n;i++)
  cout << min_jumps[n - 1] << endl;
  return 0;
}

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

using namespace std;

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

  int min_jumps[max(n, k)];
  min_jumps[0] = 0;
  for (int i = 1; i < k; i++)
    min_jumps[i] = abs(arr[0] - arr[i]);
  for (int i = k; i < n; i++) {
    min_jumps[i] = INT_MAX;
    // cout << min_jumps[i] << " " << i << " " <<
    // abs(arr[i]-arr[i-1])+min_jumps[i-1] << endl;
    for (int j = 1; j <= k; j++) {
      if (min_jumps[i] > abs(arr[i] - arr[i - j]) + min_jumps[i - j])
        min_jumps[i] = abs(arr[i] - arr[i - j]) + min_jumps[i - j];
    }
  }
  // for(int i =0; i<n;i++)
  cout << min_jumps[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll i, a[10000] = {0}, dp[10000] = {0}, n, cost, k;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 2; i <= n; i++) {
    if (i == 2)
      dp[i] = abs(a[i - 1] - a[i - 2]);
    else {
      ll mini = INT_MAX, z;
      for (z = 1; z <= k; z++) {
        cost = dp[i - z] + abs(a[i - 1] - a[i - 1 - z]);
        mini = min(cost, mini);
        if (i - 1 - z == 0)
          break;
      }
      dp[i] = mini;
    }
  }
  cout << dp[n];
}


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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> arr;
int min(int a, int b) { return a < b ? a : b; }
int main() {
  int n, k;
  cin >> n >> k;
  arr.resize(n);
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  vector<int> ans(n, INT_MAX);
  ans[0] = 0;
  ans[1] = abs(arr[0] - arr[1]);
  for (int i = 2; i < n; i++) {
    // ans[i]=min(abs(arr[i]-arr[i-1])+ans[i-1],abs(arr[i]-arr[i-2])+ans[i-2]);
    for (int j = i - 1; j >= i - k; j--) {
      if (j < 0)
        break;
      ans[i] = min(ans[i], abs(arr[i] - arr[j]) + ans[j]);
    }
  }
  cout << ans[n - 1];
  return 0;
}
Problem p03161 (C++)

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

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

const long long INF = 1LL << 60;

int N;
long long h[1000010];

long long dp[1000010];

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

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

  dp[0] = 0;

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

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


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

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

const long long INF = 1LL << 60;

int N;
long long h[1000010];

long long dp[1000010];

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

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

  dp[0] = 0;

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

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