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

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

#define deb(x) cout << #x << "=" << x << endl;
#define fo(i, n) for (ll i = 0; i < n; i++)
#define fo1(i, n)                                                              \
  for (ll i = 1; i <= n; i++)                                                  \
  i
#define sn(x) cin >> x;
#define pn(x) cout << x << "\n"
#define ps(x) cout << x << " ";
#define pb() cout << "\n";
#define sort(x) sort(x.begin(), x.end())

using namespace std;

typedef long long ll;
ll INF = INT_MAX;

void solve() {

  ll n, jumps;
  cin >> n >> jumps;
  vector<ll> v(n), dp(n);

  fo(i, n) {
    cin >> v[i];
    dp[i] = INF;
  }

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

  for (ll i = 2; i < n; i++) {
    for (ll j = 1; j <= jumps; j++) {
      dp[i] = min(dp[i], abs(v[i - j] - v[i]) + dp[i - j]);
    }
  }

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

int main() {

#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
  freopen("err.txt", "w", stderr);
#endif

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

  ll test = 1;
  // cin >> test;
  while (test--) {
    solve();
  }
}


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

#define deb(x) cout << #x << "=" << x << endl;
#define fo(i, n) for (ll i = 0; i < n; i++)
#define fo1(i, n)                                                              \
  for (ll i = 1; i <= n; i++)                                                  \
  i
#define sn(x) cin >> x;
#define pn(x) cout << x << "\n"
#define ps(x) cout << x << " ";
#define pb() cout << "\n";
#define sort(x) sort(x.begin(), x.end())

using namespace std;

typedef long long ll;
ll INF = INT_MAX;

void solve() {

  ll n, jumps;
  cin >> n >> jumps;
  vector<ll> v(n), dp(n);

  fo(i, n) {
    cin >> v[i];
    dp[i] = INF;
  }

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

  for (ll i = 2; i < n; i++) {
    for (ll j = 1; j <= jumps; j++) {
      if (j <= i)
        dp[i] = min(dp[i], abs(v[i - j] - v[i]) + dp[i - j]);
    }
  }

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

int main() {

#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
  freopen("err.txt", "w", stderr);
#endif

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

  ll test = 1;
  // cin >> test;
  while (test--) {
    solve();
  }
}
Problem p03161 (C++)

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

#define ll long long
#define rep(i, x, y) for (int i = x; i < y; i++)
#define rel(i, x, y) for (int i = x - 1; i >= y; i--)
#define all(x) x.begin(), x.end()
ll inf = 1e18 + 7;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  ll h[n];
  rep(i, 0, n) cin >> h[i];
  ll dp[100010];
  rep(i, 0, 100010) dp[i] = inf;
  dp[0] = 0;
  rep(i, 0, n) {
    rep(j, 1, k + 1) {
      dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
    }
  }
  cout << dp[n - 1] << endl;
}

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

#define ll long long
#define rep(i, x, y) for (int i = x; i < y; i++)
#define rel(i, x, y) for (int i = x - 1; i >= y; i--)
#define all(x) x.begin(), x.end()
ll inf = 1e18 + 7;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  ll h[n];
  rep(i, 0, n) cin >> h[i];
  ll dp[100010];
  rep(i, 0, 100010) dp[i] = inf;
  dp[0] = 0;
  rep(i, 0, n) {
    rep(j, 1, k + 1) {
      if (i + j >= n)
        continue;
      dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
    }
  }
  cout << dp[n - 1] << endl;
}
Problem p03161 (C++)

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

const int INF = 1e9 + 5;

int main() {
  int n, k;
  scanf("%d%d", &n, &k);

  vector<int> h(n);
  for (int &x : h)
    scanf("%d", &x);

  vector<int> dp(n, INF);
  dp[0] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= i + k; j++) {
      if (j < n) {
        dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
      }
    }
  }
  printf("%d\n", dp.back());
}

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

const int INF = 1e9 + 5;

int main() {
  int n, k;
  scanf("%d%d", &n, &k);

  vector<int> h(n);
  for (int &x : h)
    scanf("%d", &x);

  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]));
      }
    }
  }
  printf("%d\n", dp.back());
}
Problem p03161 (C++)

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

int main() {
  long long n, k, *p, *dp;
  cin >> n;
  cin >> k;
  p = new long long[n];
  for (long long y = 0; y < n; y++) {
    cin >> p[y];
  }
  dp = new long long[n];
  dp[0] = 0;
  dp[1] = abs(p[1] - p[0]);
  for (long long i = 2; i < n; i++) {
    long long minimo = dp[i - 1] + abs(p[i] - p[i - 1]);
    for (long long t = 1; t < min(k, n - 1); t++) {
      minimo = min(minimo, dp[i - (t + 1)] + abs(p[i] - p[i - (t + 1)]));
    }
    dp[i] = minimo;
  }
  cout << dp[n - 1];

  delete[] p;
  delete[] dp;
  return 0;
}

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

int main() {
  long long n, k, *p, *dp;
  cin >> n;
  cin >> k;
  p = new long long[n];
  for (long long y = 0; y < n; y++) {
    cin >> p[y];
  }
  dp = new long long[n];
  dp[0] = 0;
  dp[1] = abs(p[1] - p[0]);
  for (long long i = 2; i < n; i++) {
    long long minimo = dp[i - 1] + abs(p[i] - p[i - 1]);
    for (long long t = 1; t < min(i, k); t++) {
      minimo = min(minimo, dp[i - (t + 1)] + abs(p[i] - p[i - (t + 1)]));
    }
    dp[i] = minimo;
  }
  cout << dp[n - 1];

  delete[] p;
  delete[] dp;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define pd(a, n)                                                               \
  for (int i = 0; i < n; i++)                                                  \
  cout << a[i] << ((i == n - 1) ? "\n" : " ")
#define pdd(a, n, m)                                                           \
  for (int i = 0; i < m; i++)                                                  \
  pd(a, m)
#define mp make_pair
#define pb push_back
#define ll __int64
#define __int64 long long
#define P pair<int, int>
#define PP pair<P, int>

int n, k;
int h[11111];
int dp[111111];
const int INF = 1111111111;

int main() {
  cin >> n >> k;
  rep(i, n) cin >> h[i];
  rep(i, n) dp[i] = INF;
  dp[0] = 0;
  rep(i, n) {
    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;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define pd(a, n)                                                               \
  for (int i = 0; i < n; i++)                                                  \
  cout << a[i] << ((i == n - 1) ? "\n" : " ")
#define pdd(a, n, m)                                                           \
  for (int i = 0; i < m; i++)                                                  \
  pd(a, m)
#define mp make_pair
#define pb push_back
#define ll __int64
#define __int64 long long
#define P pair<int, int>
#define PP pair<P, int>

int n, k;
int h[111111];
int dp[111111];
const int INF = 1111111111;

int main() {
  cin >> n >> k;
  rep(i, n) cin >> h[i];
  rep(i, n) dp[i] = INF;
  dp[0] = 0;
  rep(i, n) {
    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;

  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;

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

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

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

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

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

typedef long long ll;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};
const int INF = 1e9;
#define FOR(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
int guki(int a) {
  if (a % 2 == 0)
    return 0;
  else
    return 1;
}
int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else {
    return (gcd(b, a % b));
  }
}
int lcm(int a, int b) {
  int x = gcd(a, b);
  return (a * b / x);
}
ll nCr(ll n, ll r) {
  ll ans = 1;
  for (ll i = n; i > n - r; i--) {
    ans *= i;
  }
  for (ll i = 1; i <= r; i++) {
    ans /= i;
  }
  return ans;
}

int nPr(int n, int r) {
  int ans = 1;
  for (int i = n; i > n - r; i--) {
    ans *= i;
  }
  return ans;
}

using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> vec(n), dp(n);

  REP(i, n) { cin >> vec[i]; }

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

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


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

typedef long long ll;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};
const int INF = 1e9;
#define FOR(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
int guki(int a) {
  if (a % 2 == 0)
    return 0;
  else
    return 1;
}
int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else {
    return (gcd(b, a % b));
  }
}
int lcm(int a, int b) {
  int x = gcd(a, b);
  return (a * b / x);
}
ll nCr(ll n, ll r) {
  ll ans = 1;
  for (ll i = n; i > n - r; i--) {
    ans *= i;
  }
  for (ll i = 1; i <= r; i++) {
    ans /= i;
  }
  return ans;
}

int nPr(int n, int r) {
  int ans = 1;
  for (int i = n; i > n - r; i--) {
    ans *= i;
  }
  return ans;
}

using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> vec(n), dp(n);

  REP(i, n) { cin >> vec[i]; }

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

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

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

int main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  int dp[n];
  for (int i = 0; i < n; i++) {
    dp[i] = INT_MAX;
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  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(a[i + j] - a[i]));
    }
  }
  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 a[n];
  int dp[n];
  for (int i = 0; i < n; i++) {
    dp[i] = INT_MAX;
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  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(a[i + j] - a[i]));
    }
  }
  cout << dp[n - 1] << "\n";
  return 0;
}
Problem p03161 (C++)

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

#define for0(i, n) for (long long i = 0; i < n; i++)
#define for1(i, n) for (long long i = 1; i <= n; i++)
#define forc(i, l, r) for (long long i = l; i <= r; ++i)
#define forr0(i, n) for (long long i = n - 1; i >= 0; i--)
#define forr1(i, n) for (long long i = n; i >= 1; i--)

#define pb push_back
#define fi first
#define se second

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin, (x).rend()

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef long long int lli;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;

const ll N = 1e5 + 20;
const ll mod = 1e9 + 7;

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

  ll i, j, k, n;
  cin >> n >> k;
  ll a[N], dp[N];
  for0(i, n) cin >> a[i];
  for0(i, N) dp[i] = 1e15;
  dp[0] = 0;
  for (i = 0; i < n; i++) {
    for (j = i + 1; j <= i + k; j++) {
      dp[j] = min(dp[j], dp[i] + (ll)abs(a[j] - a[i]));
    }
  }
  // for0(i,n) cout<<dp[i]<<" ";
  cout << dp[n - 1] << endl;

  return 0;
}


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

#define for0(i, n) for (long long i = 0; i < n; i++)
#define for1(i, n) for (long long i = 1; i <= n; i++)
#define forc(i, l, r) for (long long i = l; i <= r; ++i)
#define forr0(i, n) for (long long i = n - 1; i >= 0; i--)
#define forr1(i, n) for (long long i = n; i >= 1; i--)

#define pb push_back
#define fi first
#define se second

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin, (x).rend()

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef long long int lli;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;

const ll N = 1e5 + 200;
const ll mod = 1e9 + 7;

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

  ll i, j, k, n;
  cin >> n >> k;
  ll a[N], dp[N];
  for0(i, n) cin >> a[i];
  for0(i, N) dp[i] = 1e15;
  dp[0] = 0;
  for (i = 0; i < n; i++) {
    for (j = i + 1; j <= i + k; j++) {
      dp[j] = min(dp[j], dp[i] + (ll)abs(a[j] - a[i]));
    }
  }
  // for0(i,n) cout<<dp[i]<<" ";
  cout << dp[n - 1] << endl;

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
// INCLUDE
//------------------------------------------
#include <algorithm>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
// DEFINE
//------------------------------------------
#define ll long long
#define ALLv(a) (a).begin(), (a).end()
#define ALL(a, n) (a), (a) + n
#define vi vector<long long>
#define vd vector<long double>
#define vs vector<string>
// CONST
//------------------------------------------
#define INF 1010000000000000017LL
#define MOD 1000000007LL
#define EPS 1e-12
#define PI 3.14159265358979323846
// REPEAT
//------------------------------------------
#define FOR(i, m, n) for (ll(i) = (m); (i) < (n); (i)++)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, x) for (ll(i) = 1; (i) <= (x); (i)++)
#define RREP(i, x) for (ll(i) = (x)-1; (i) >= 0; (i)--)
#define RREPS(i, x) for (ll(i) = (x); (i) > 0; (i)--)
#define WREP(i, in, j, jn) REP(i, in) REP(j, jn)
using namespace std;
//-------------------------------------------
int main(void) {
  ll N, K;
  cin >> N >> K;
  ll h[N + 1];
  REPS(i, N) cin >> h[i];

  vi dp(N * 2 + 1, INF);
  dp[1] = 0;
  REPS(i, N) {
    REPS(k, K) dp[i + k] = min(dp[i + k], dp[i] + labs(h[i + k] - h[i]));
  }
  cout << dp[N] << endl;
  return 0;
}


Fixed submission:
// INCLUDE
//------------------------------------------
#include <algorithm>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
// DEFINE
//------------------------------------------
#define ll long long
#define ALLv(a) (a).begin(), (a).end()
#define ALL(a, n) (a), (a) + n
#define vi vector<long long>
#define vd vector<long double>
#define vs vector<string>
// CONST
//------------------------------------------
#define INF 1010000000000000017LL
#define MOD 1000000007LL
#define EPS 1e-12
#define PI 3.14159265358979323846
// REPEAT
//------------------------------------------
#define FOR(i, m, n) for (ll(i) = (m); (i) < (n); (i)++)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, x) for (ll(i) = 1; (i) <= (x); (i)++)
#define RREP(i, x) for (ll(i) = (x)-1; (i) >= 0; (i)--)
#define RREPS(i, x) for (ll(i) = (x); (i) > 0; (i)--)
#define WREP(i, in, j, jn) REP(i, in) REP(j, jn)
using namespace std;
//-------------------------------------------
int main(void) {
  ll N, K;
  cin >> N >> K;
  ll h[N + 1];
  REPS(i, N) cin >> h[i];

  vi dp(N * 2 + 2, INF);
  dp[1] = 0;
  REPS(i, N) {
    REPS(k, K) dp[i + k] = min(dp[i + k], dp[i] + labs(h[i + k] - h[i]));
  }
  cout << dp[N] << endl;
  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, k;
  cin >> n >> k;
  vector<int> stone(n);
  for (int i = 0; i < n; i++)
    cin >> stone[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++)
      dp[j] = min(dp[j], dp[i] + abs(stone[j] - stone[i]));

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> stone(n);
  for (int i = 0; i < n; i++)
    cin >> stone[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(stone[j] - stone[i]));

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int mod(int x) {
  if (x > 0)
    return x;
  return -x;
}
int main() {
  int n, k;
  cin >> n >> k;
  int h[n], result[n];
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i <= k; i++)
    result[i] = mod(h[i] - h[0]);
  for (int i = k + 1; i < n; i++) {
    int min = INT_MAX;
    for (int j = i - k; j < i; j++) {
      int flag = result[j] + mod(h[i] - h[j]);
      if (min > flag)
        min = flag;
    }
    result[i] = min;
  }
  cout << result[n - 1];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int mod(int x) {
  if (x > 0)
    return x;
  return -x;
}
int main() {
  int n, k;
  cin >> n >> k;
  int h[n], result[n];
  for (int i = 0; i < n; i++)
    cin >> h[i];
  for (int i = 0; i <= k && i < n; i++)
    result[i] = mod(h[i] - h[0]);
  for (int i = k + 1; i < n; i++) {
    int min = INT_MAX;
    for (int j = i - k; j < i; j++) {
      int flag = result[j] + mod(h[i] - h[j]);
      if (min > flag)
        min = flag;
    }
    result[i] = min;
  }
  cout << result[n - 1];
  return 0;
}
Problem p03161 (C++)

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

int main() {

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

  vector<int> h(n);

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

  vector<int> c(n, 1e+9);

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

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

  return 0;
}

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

int main() {

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

  vector<int> h(n);

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

  vector<int> c(n, 1e+9);

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

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

  return 0;
}
Problem p03161 (C++)

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

#define int long long

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vp vector<pii>
#define vs vector<string>
#define all(v) v.begin(), v.end()

#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define ff first
#define ss second
#define sz(x) ((int)(x).size())
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))

#define pi 3.14159265358979323846 // 20 decimals
#define MOD 1000000007

int32_t main() {

  IOS
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, k;
  cin >> n >> k;
  vi h(n);
  for (int &x : h) {
    cin >> x;
  }
  vi dp(n, INT_MAX);
  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];
  return 0;
}

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

#define int long long

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vp vector<pii>
#define vs vector<string>
#define all(v) v.begin(), v.end()

#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define ff first
#define ss second
#define sz(x) ((int)(x).size())
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))

#define pi 3.14159265358979323846 // 20 decimals
#define MOD 1000000007

int32_t main() {

  IOS
      //  #ifndef ONLINE_JUDGE
      // freopen("input.txt", "r", stdin);
      // freopen("output.txt", "w", stdout);
      // #endif
      int n,
      k;
  cin >> n >> k;
  vi h(n);
  for (int &x : h) {
    cin >> x;
  }
  vi dp(n, INT_MAX);
  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];
  return 0;
}
Problem p03161 (C++)

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

void solve() {
  int n, k;
  cin >> n;
  cin >> k;
  const int INF = 1e9 + 5;
  vector<int> dp(n + 1, INF);
  int H[n + 1];
  for (int i = 1; i < n + 1; i++) {
    cin >> H[i];
  }
  dp[1] = 0;
  dp[2] = abs(H[1] - H[2]);

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

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

int main() { solve(); }

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

void solve() {
  int n, k;
  cin >> n;
  cin >> k;
  const int INF = 1e9 + 5;
  vector<int> dp(n + 1, INF);
  int H[n + 1];
  for (int i = 1; i < n + 1; i++) {
    cin >> H[i];
  }
  dp[1] = 0;
  dp[2] = abs(H[1] - H[2]);

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

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

int main() { solve(); }
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 dp[n];
  memset(dp, -1, sizeof(dp));

  dp[0] = 0;
  for (int i = 1; i <= k; i++) {
    dp[i] = abs(arr[0] - arr[i]);
  }

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

    dp[i] = ans;
  }

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

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

  if (k >= n - 1) {
    cout << abs(arr[0] - arr[n - 1]) << "\n";
    return 0;
  }

  int dp[n];
  memset(dp, -1, sizeof(dp));

  dp[0] = 0;
  for (int i = 1; i <= k; i++) {
    dp[i] = abs(arr[0] - arr[i]);
  }

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

    dp[i] = ans;
  }

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

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

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

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

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int h[n];
  for (int i = 0; i < n; i++)
    cin >> h[i];
  ll ans = 0;
  int dp[n]; // cost
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < n; i++)
    dp[i] = 1e9;
  for (int i = 2; i < n; i++) {
    for (int j = i - 1; j >= 0; j--) {
      if (i - j <= k) {
        int x = abs(h[i] - h[j]) + dp[j];
        dp[i] = min(dp[i], x);
      }
    }
  }
  cout << dp[n - 1] << '\n';
  return 0;
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define F first
#define S second
// cout<<fixed<<setprecision(11);
const string nl = "\n";
const ll MOD = 1000000007;
const ll ARR_MAX = 1e5 + 1;
const ll INF = 1e10 + 1;

void solve() {
  int n, k;
  cin >> n >> k;
  ll dp[n + 1]; // dp[i]=minimum cost required to reach rock i-1.
  ll h[n + 1];
  // h[0]=0;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    dp[i + 1] = INF;
  }
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= k && j < n; j++) {
      dp[i + j] = min(dp[i + j], dp[i] + max(h[i + j] - h[i], h[i] - h[i + j]));
    }
  }
  cout << dp[n - 1] << nl;
}

int main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define F first
#define S second
// cout<<fixed<<setprecision(11);
const string nl = "\n";
const ll MOD = 1000000007;
const ll ARR_MAX = 1e5 + 1;
const ll INF = 1e10 + 1;

void solve() {
  int n, k;
  cin >> n >> k;
  ll dp[n + 1]; // dp[i]=minimum cost required to reach rock i-1.
  ll h[n + 1];
  // h[0]=0;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
    dp[i + 1] = INF;
  }
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= k && i + j < n; j++) {
      dp[i + j] = min(dp[i + j], dp[i] + max(h[i + j] - h[i], h[i] - h[i + j]));
    }
  }
  cout << dp[n - 1] << nl;
}

int main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for (int i = st; i < en; i++)
#define tr(x) for (auto it = x.begin(); it != x.end(); it++)
#define fast                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define pb push_back
#define ll long long
#define int long long
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
#define MOD 1000000007
#define INF 1000000000000000007
#define MAXN 5206

signed main() {
  int a, b, c, d, k, t = 1;
  cin >> t >> k;
  vector<int> v;
  For(i, 0, t) {
    cin >> a;
    v.pb(a);
  }
  int dp[t];
  if (t == 2)
    cout << v[1] - v[0];
  else {
    dp[0] = 0;
    dp[1] = abs(v[1] - v[0]);
    For(j, 2, t) {
      d = INF;
      For(op, 1, k + 1) d = min(dp[j - op] + abs(v[j] - v[j - op]), d);
      dp[j] = d;
    }
    cout << dp[t - 1];
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for (int i = st; i < en; i++)
#define tr(x) for (auto it = x.begin(); it != x.end(); it++)
#define fast                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define pb push_back
#define ll long long
#define int long long
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
#define MOD 1000000007
#define INF 1000000000000000007
#define MAXN 5206

signed main() {
  int a, b, c, d, k, t = 1;
  cin >> t >> k;
  vector<int> v;
  For(i, 0, t) {
    cin >> a;
    v.pb(a);
  }
  int dp[t];
  if (t == 2)
    cout << v[1] - v[0];
  else {
    dp[0] = 0;
    dp[1] = abs(v[1] - v[0]);
    For(j, 2, t) {
      d = INF;
      For(op, 1, k + 1) {
        if (j - op < 0)
          break;
        d = min(dp[j - op] + abs(v[j] - v[j - op]), d);
      }
      dp[j] = d;
    }
    cout << dp[t - 1];
  }
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
/*
Author: Sanskar Agarwal
Nick: sanskarag
Birla Institute Of Technology, Mesra
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl "\n"
#define fast                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define F(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); i++)
#define RF(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); i--)
#define INF 100009
#define mod 1000000007
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define ff first
#define ss second
#define test                                                                   \
  while (t--)                                                                  \
    solve();

void solve() {
  ll n, k;
  cin >> n >> k;
  ll arr[n + 1];
  for (ll i = 1; i <= n; ++i)
    cin >> arr[i];
  ll dp[n + 1];
  dp[0] = 0;
  dp[1] = 0;
  for (ll i = 2; i <= n; ++i) {
    dp[i] = INT_MAX;
    ll j = max(i - k, 1LL);
    for (j; j < i; ++j) {
      ll temp = dp[j] + abs(arr[i] - arr[j]);
      dp[i] = min(dp[i], temp);
    }
  }
  cout << dp[n] << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast ll t;
  // cin >> t;
  t = 1;
  test return 0;
}

Fixed submission:
/*
Author: Sanskar Agarwal
Nick: sanskarag
Birla Institute Of Technology, Mesra
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl "\n"
#define fast                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define F(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); i++)
#define RF(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); i--)
#define INF 100009
#define mod 1000000007
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define ff first
#define ss second
#define test                                                                   \
  while (t--)                                                                  \
    solve();

void solve() {
  ll n, k;
  cin >> n >> k;
  ll arr[n + 1];
  for (ll i = 1; i <= n; ++i)
    cin >> arr[i];
  ll dp[n + 1];
  dp[0] = 0;
  dp[1] = 0;
  for (ll i = 2; i <= n; ++i) {
    dp[i] = INT_MAX;
    ll j = max(i - k, 1LL);
    for (j; j < i; ++j) {
      ll temp = dp[j] + abs(arr[i] - arr[j]);
      dp[i] = min(dp[i], temp);
    }
  }
  cout << dp[n] << endl;
}

int main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("../input.txt", "r", stdin);
  // 	freopen("../output.txt", "w", stdout);
  // #endif
  fast ll t;
  // cin >> t;
  t = 1;
  test return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define el '\n'
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define repd(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define large 1000000007
#define pb() push_back()
#define MAX 10e+7
int dp[100000];
int hp(int arr[], int n, int k, int i) {
  int min = INT_MAX;
  int z = i - k;
  if (z < 0)
    z = 0;
  rep(j, z, i) {
    if (dp[j] + abs(arr[j] - arr[i]) < min)
      min = dp[j] + abs(arr[j] - arr[i]);
  }
  return min;
}
void solve() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  rep(i, 0, n) { cin >> arr[i]; }
  dp[0] = 0;
  dp[1] = abs(arr[1] - arr[0]);
  rep(i, 2, n) { dp[i] = hp(arr, n, k, i); }
  // cout<<dp[1]<<el;
  cout << dp[n - 1] << el;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  long long int t;

  solve();

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define el '\n'
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define repd(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define large 1000000007
#define pb() push_back()
#define MAX 10e+7
int dp[100000];
int hp(int arr[], int n, int k, int i) {
  int min = INT_MAX;
  int z = i - k;
  if (z < 0)
    z = 0;
  rep(j, z, i) {
    if (dp[j] + abs(arr[j] - arr[i]) < min)
      min = dp[j] + abs(arr[j] - arr[i]);
  }
  return min;
}
void solve() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  rep(i, 0, n) { cin >> arr[i]; }
  dp[0] = 0;
  dp[1] = abs(arr[1] - arr[0]);
  rep(i, 2, n) { dp[i] = hp(arr, n, k, i); }
  // cout<<dp[1]<<el;
  cout << dp[n - 1] << el;
}
int main() {

  long long int t;

  solve();

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const long long INF = 1LL << 60;

int main() {
  int N, k;
  long long h[10000];
  long long dp[10000];
  cin >> N >> k;
  for (int i = 0; i < N; ++i) {
    cin >> h[i];
  }

  for (int i = 1; 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] - h[i + j]));
    }
  }
  cout << dp[N - 1] << endl;
}


Fixed submission:
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const long long INF = 1LL << 60;

int main() {
  int N, k;
  long long h[110000];
  long long dp[110000];
  cin >> N >> k;
  for (int i = 0; i < N; ++i) {
    cin >> h[i];
  }

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

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

  for (i = 3; i <= k; i++) {
    // cout<<"**"<<endl;
    for (j = 1; j < i; j++)
      dp[i] = min(dp[j] + llabs(a[i - 1] - a[j - 1]), dp[i]);
  }

  for (i = k + 1; i <= n; i++) {
    // cout<<"&&"<<endl;
    for (j = i - k; j < i; j++)
      dp[i] = min(dp[j] + llabs(a[i - 1] - a[j - 1]), dp[i]);
  }

  cout << dp[n];
}


Fixed submission:
#include <bits/stdc++.h>
#define inf 1e18
#define ll long long int
using namespace std;
int main(void) {
  int i, j, n, k;
  cin >> n >> k;
  ll a[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  ll dp[n + 1];
  for (i = 0; i <= n; i++)
    dp[i] = inf;
  dp[1] = 0;
  dp[2] = abs(a[1] - a[0]);

  for (i = 3; i <= min(n, k); i++) {
    // cout<<"**"<<endl;
    for (j = 1; j < i; j++)
      dp[i] = min(dp[j] + llabs(a[i - 1] - a[j - 1]), dp[i]);
  }

  for (i = k + 1; i <= n; i++) {
    // cout<<"&&"<<endl;
    for (j = i - k; j < i; j++)
      dp[i] = min(dp[j] + llabs(a[i - 1] - a[j - 1]), dp[i]);
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define filename "frog"
#define int long long
#define oo 1e9
#define N 25

using namespace std;

int n, k;
int f[N], a[N];
void open() {
  // freopen(filename".inp","r",stdin) ;
  // freopen(filename".out","w",stdout) ;
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
void readin() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
}
void process() {
  for (int i = 1; i <= n; i++)
    f[i] = oo;
  f[1] = 0;
  for (int i = 1; i < n; i++)
    for (int j = 1; j <= k; j++) {
      if (i + j > n)
        break;
      int pre = f[i] + abs(a[i] - a[j + i]);
      f[i + j] = min(f[i + j], pre);
    }
}
signed main() {
  open();
  readin();
  process();
  cout << f[n];
}

Fixed submission:
#include <bits/stdc++.h>
#define filename "frog"
#define int long long
#define oo 1e9
#define N 200005

using namespace std;

int n, k;
int f[N], a[N];
void open() {
  // freopen(filename".inp","r",stdin) ;
  // freopen(filename".out","w",stdout) ;
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
void readin() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
}
void process() {
  for (int i = 1; i <= n; i++)
    f[i] = oo;
  f[1] = 0;
  for (int i = 1; i < n; i++)
    for (int j = 1; j <= k; j++) {
      if (i + j > n)
        break;
      int pre = f[i] + abs(a[i] - a[j + i]);
      f[i + j] = min(f[i + j], pre);
    }
}
signed main() {
  open();
  readin();
  process();
  cout << f[n];
}
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(NULL);

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

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

  int dp[n];
  for (int i = 0; i <= k; i++) {
    if (!i) {
      dp[i] = 0;
    } else {
      dp[i] = abs(h[i] - h[0]);
    }
  }

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

  cout << dp[n - 1];

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

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

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

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

  int dp[n];
  for (int i = 0; i <= k; i++) {
    if (!i) {
      dp[i] = 0;
    } else {
      dp[i] = abs(h[i] - h[0]);
    }
  }

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

  cout << dp[n - 1];

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

  const long long INF = 1e16;
  long long dp[N];
  fill(dp, dp + N, INF);
  dp[0] = 0;

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

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

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

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

  const long long INF = 1e16;
  long long dp[N];
  fill(dp, dp + N, INF);
  dp[0] = 0;

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

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

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<int> dp(n);
  dp[0] = 0;
  dp[1] = abs(v[0] - v[1]);
  int x;
  for (int i = 2; i < n; i++) {
    x = 1000000;
    for (int j = 0; j < k; j++) {
      x = min(x, abs(v[i] - v[i - j - 1]) + dp[i - j - 1]);
    }
    dp[i] = x;
  }
  cout << dp[n - 1] << '\n';
  return 0;
}


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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<int> dp(n);
  dp[0] = 0;
  dp[1] = abs(v[0] - v[1]);
  int x;
  for (int i = 2; i < n; i++) {
    x = INT_MAX;
    for (int j = 0; j < k && j < i; j++) {
      x = min(x, abs(v[i] - v[i - j - 1]) + dp[i - j - 1]);
    }
    dp[i] = x;
  }
  cout << dp[n - 1] << '\n';
  return 0;
}
Problem p03161 (C++)

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

ll dp[MAXN];
int n, k;
vector<ll> v;

ll solve(int i) {
  if (i < 0)
    return 0;
  if (dp[i] >= 0) {
    return dp[i];
  }

  ll res1 = -1;
  for (int j = 1; j <= k; ++j)
    if (res1 != -1)
      res1 = min(res1, abs(v[i] - v[i - j]) + solve(i - j));
    else
      res1 = abs(v[i] - v[i - j]) + solve(i - j);

  dp[i] = res1;

  return dp[i];
}

int main() {

  ll num;
  cin >> n >> k;

  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; ++i) {
    cin >> num;
    v.push_back(num);
  }

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

  cout << solve(n - 1) << endl;

  return 0;
}

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

ll dp[MAXN];
int n, k;
vector<ll> v;

ll solve(int i) {
  if (i < 0)
    return 0;
  if (dp[i] >= 0) {
    return dp[i];
  }

  ll res1 = -1;
  for (int j = 1; j <= min(k, i); ++j)
    if (res1 != -1)
      res1 = min(res1, abs(v[i] - v[i - j]) + solve(i - j));
    else
      res1 = abs(v[i] - v[i - j]) + solve(i - j);

  dp[i] = res1;

  return dp[i];
}

int main() {

  ll num;
  cin >> n >> k;

  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; ++i) {
    cin >> num;
    v.push_back(num);
  }

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

  cout << solve(n - 1) << endl;

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll n, h, ans[100005], arr[100005], sto[100005];
ll solve(int ind) {
  if (ind >= n)
    return 2e9;
  if (ind == n - 1)
    return 0;
  if (~sto[ind])
    return sto[ind];
  ll ans = 2e9;
  for (int i = 1; i <= min(n - ind, h); i++)
    ans = min(ans, abs(arr[ind] - arr[ind + i]) + solve(ind + i));
  return ans;
}
int main() {
  cin >> n >> h;
  memset(sto, -1, sizeof sto);
  for (int i = 0; i < n; cin >> arr[i++])
    ;
  cout << solve(0);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll n, h, ans[100005], arr[100005], sto[100005];
ll solve(int ind) {
  if (ind >= n)
    return 2e9;
  if (ind == n - 1)
    return 0;
  if (~sto[ind])
    return sto[ind];
  ll ans = 2e9;
  for (int i = 1; i <= min(n - ind, h); i++)
    ans = min(ans, abs(arr[ind] - arr[ind + i]) + solve(ind + i));
  sto[ind] = ans;
  return ans;
}
int main() {
  cin >> n >> h;
  memset(sto, -1, sizeof sto);
  for (int i = 0; i < n; cin >> arr[i++])
    ;
  cout << solve(0);
  return 0;
}