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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define all(a) (a).begin(), (a).end()
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define sz size
#define fr first
#define sc second
#define pb push_back
#define er erase
#define in insert
#define mp make_pair
#define pi pair
#define rc(s) return cout << s, 0
#define int long long
using namespace std;

int n, h[100005], dp[100005], k;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  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] = 1e18;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      dp[i + j] = min(dp[i] + abs(h[i] - h[i + j]), dp[i + j]);
    }
  }
  cout << dp[n];
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define all(a) (a).begin(), (a).end()
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define sz size
#define fr first
#define sc second
#define pb push_back
#define er erase
#define in insert
#define mp make_pair
#define pi pair
#define rc(s) return cout << s, 0
#define int long long
using namespace std;

int n, h[150005], dp[150005], k;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  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] = 1e18;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      dp[i + j] = min(dp[i] + abs(h[i] - h[i + j]), dp[i + j]);
    }
  }
  cout << dp[n];
}
Problem p03161 (C++)

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

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

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

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

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


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

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

int minCost(vector<int> &v, int index, int k, vector<int> &cost) {
  if (cost[index] != -1) {
    return cost[index];
  }
  if (index <= 0) {
    return 0;
  } else if (index == 1) {
    return cost[index] = abs(v[1] - v[0]);
  } else {
    int c = 0;
    int minimum = INF;
    for (int i = 1; i <= k; i++) {
      c = abs(v[index] - v[index - i]) + minCost(v, index - i, k, cost);
      minimum = min(minimum, c);
    }
    return cost[index] = minimum;
  }
}

int32_t main() {
  int n, k;
  cin >> n >> k;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  vector<int> cost;
  for (int i = 0; i < n; i++) {
    cost.push_back(-1);
  }
  cost[0] = 0;
  cout << minCost(v, v.size() - 1, k, cost) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define INF 1000000000
using namespace std;

int minCost(vector<int> &v, int index, int k, vector<int> &cost) {
  if (cost[index] != -1) {
    return cost[index];
  }
  if (index <= 0) {
    return 0;
  } else if (index == 1) {
    return cost[index] = abs(v[1] - v[0]);
  } else {
    int c = 0;
    int minimum = INF;
    for (int i = 1; i <= k; i++) {
      if (index - i >= 0) {
        c = abs(v[index] - v[index - i]) + minCost(v, index - i, k, cost);
        minimum = min(minimum, c);
      }
    }
    return cost[index] = minimum;
  }
}

int32_t main() {
  int n, k;
  cin >> n >> k;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  vector<int> cost;
  for (int i = 0; i < n; i++) {
    cost.push_back(-1);
  }
  cost[0] = 0;
  cout << minCost(v, v.size() - 1, k, cost) << endl;
  return 0;
}
Problem p03161 (C++)

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

using namespace std;

int main() {

  int N, K;
  cin >> N >> K;
  vector<int> heights(N);
  for (int i = 0; i < N; i++)
    cin >> heights[i];

  vector<long> dp(N, 0);

  for (int i = 1; i < N; i++) {
    dp[i] = dp[i - 1] + abs(heights[i] - heights[i - 1]);
    for (int k = 2; k <= K; k++) {
      dp[i] = min(dp[i], abs(heights[i] - heights[i - k]) + dp[i - k]);
    }
  }
  cout << dp[N - 1] << endl;
  return 0;
  // EDABC
}


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

using namespace std;

int main() {

  int N, K;
  cin >> N >> K;
  vector<int> heights(N);
  for (int i = 0; i < N; i++)
    cin >> heights[i];

  vector<long> dp(N, 0);

  for (int i = 1; i < N; i++) {
    dp[i] = dp[i - 1] + abs(heights[i] - heights[i - 1]);
    // i - k >= 0 i >= k
    for (int k = 2; k <= min(i, K); k++) {
      dp[i] = min(dp[i], abs(heights[i] - heights[i - k]) + dp[i - k]);
    }
  }
  cout << dp[N - 1] << endl;
  return 0;
  // EDABC
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
#define INF 1000000000
using namespace std;
int main(void) {
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  vector<int> cost(n, INF);
  cost[0] = 0;
  for (int i = 0; i < n; i++) {
    int m = min(k, n - i);
    for (int j = 1; j <= m; j++) {
      cost[i + j] = min(cost[i + j], cost[i] + abs(h[i + j] - h[i]));
    }
  }
  cout << cost[n - 1] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define INF 1000000000
using namespace std;
int main(void) {
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  vector<int> cost(n, INF);
  cost[0] = 0;
  for (int i = 0; i < n; i++) {
    int m = min(k, n - 1 - i);
    for (int j = 1; j <= m; j++) {
      cost[i + j] = min(cost[i + j], cost[i] + abs(h[i + j] - h[i]));
    }
  }
  cout << cost[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const int N = 1e5 + 5;
ll dp[N] = {-1}, v[N], k, n;
const int inf = 0x3f3f3f3f;
ll check(int index);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll i, j, m;
  memset(dp, -1, sizeof(dp));
  cin >> n >> k;
  for (i = 0; i < n; i++)
    cin >> v[i];
  cout << check(0) << "\n";

  return 0;
}

ll check(int index) {
  if (index >= n)
    return inf;
  else if (dp[index] != -1)
    return dp[index];

  else if (index == n - 1)
    return dp[index] = 0;
  ll var = inf;
  for (int i = index + 1; i <= min(n - 1, k + index); i++)
    var = min(var, abs(v[index] - v[i]) + check(i));
  return var;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const int N = 1e5 + 5;
ll dp[N] = {-1}, v[N], k, n;
const int inf = 0x3f3f3f3f;
ll check(int index);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll i, j, m;
  memset(dp, -1, sizeof(dp));
  cin >> n >> k;
  for (i = 0; i < n; i++)
    cin >> v[i];
  cout << check(0) << "\n";

  return 0;
}

ll check(int index) {
  if (index >= n)
    return inf;
  else if (dp[index] != -1)
    return dp[index];

  else if (index == n - 1)
    return dp[index] = 0;
  ll var = inf;
  for (int i = index + 1; i <= min(n - 1, k + index); i++)
    var = min(var, abs(v[index] - v[i]) + check(i));
  return dp[index] = var;
}
Problem p03161 (C++)

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

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}

int main() {
  int n = ri(), k = ri();
  int a[n];
  for (int i = 0; i < n; i++)
    a[i] = ri();

  std::set<std::pair<int, int>> to_list;
  for (int i = 0; i < k; i++)
    to_list.insert({a[i], i});

  int64_t dp[n];
  for (int i = 0; i < n; i++)
    dp[i] = 1000000000000000000;
  dp[0] = 0;
  auto dp_go = [&](int dest, int origin) {
    dp[dest] = std::min(dp[dest], dp[origin] + std::abs(a[dest] - a[origin]));
  };
  for (int i = 0; i + 1 < n; i++) {
    to_list.erase({a[i], i});
    if (i + k < n)
      to_list.insert({a[i + k], i + k});
    auto itr = to_list.lower_bound({a[i], 0});
    if (itr != to_list.end())
      dp_go(itr->second, i);
    if (itr != to_list.begin())
      dp_go(to_list.lower_bound({std::prev(itr)->first, 0})->second, i);
  }
  std::cout << dp[n - 1] << std::endl;
  return 0;
}


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

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}

int main() {
  int n = ri(), k = ri();
  k = std::min(k, n - 1);
  int a[n];
  for (int i = 0; i < n; i++)
    a[i] = ri();

  std::set<std::pair<int, int>> to_list;
  for (int i = 0; i < k; i++)
    to_list.insert({a[i], i});

  int64_t dp[n];
  for (int i = 0; i < n; i++)
    dp[i] = 1000000000000000000;
  dp[0] = 0;
  auto dp_go = [&](int dest, int origin) {
    dp[dest] = std::min(dp[dest], dp[origin] + std::abs(a[dest] - a[origin]));
  };
  for (int i = 0; i + 1 < n; i++) {
    to_list.erase({a[i], i});
    if (i + k < n)
      to_list.insert({a[i + k], i + k});
    auto itr = to_list.lower_bound({a[i], 0});
    if (itr != to_list.end())
      dp_go(itr->second, i);
    if (itr != to_list.begin())
      dp_go(to_list.lower_bound({std::prev(itr)->first, 0})->second, i);
  }
  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 lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define MOD 1000000007
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)

lli dp[101000];

int main() {

  lli n, k;
  cin >> n >> k;
  vector<lli> v;
  v.resize(n + k);

  REP(i, 0, 101000) dp[i] = INF;
  dp[0] = 0;

  REP(i, 0, n) cin >> v[i];
  REP(i, n, n + k + 10) v[i] = INF;

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

  return 0;
}


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

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define MOD 1000000007
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)

lli dp[101000];

int main() {

  lli n, k;
  cin >> n >> k;
  vector<lli> v;
  v.resize(n + k + 10);

  REP(i, 0, 101000) dp[i] = INF;
  dp[0] = 0;

  REP(i, 0, n) cin >> v[i];
  REP(i, n, n + k + 10) v[i] = INF;

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

  return 0;
}
Problem p03161 (C++)

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

const long long N = 200001;
const long long K = 201;
long long n, k;
long long arr[N];
long long matrix[N][K];

long long checkmin(long long i) {
  long long res = matrix[i][0];
  for (long long j = 0; j < k; j++) {
    if (matrix[i][j] >= 0) {
      res = min(res, matrix[i][j]);
    }
  }
  return res;
}

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

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

  for (long long r = 0; r < k; r++) {
    for (long long f = 0; f < n; f++) {
      if (r > n - f - 2)
        matrix[f][r] = -1;
    }
    matrix[n - 1][r] = 0;
    matrix[n - r - 2][r] = abs(arr[n - r - 2] - arr[n - 1]);
  }

  for (long long j = n - 2; j >= 0; j--) {
    for (long long h = 0; h < k; h++) {
      if (h < n - j - 1) {
        matrix[j][h] = abs(arr[j] - arr[j + h + 1]) + checkmin(j + h + 1);
      }
    }
  }

  long long res = matrix[0][0];
  long long maximo = -1;
  for (long long a = 0; a < k; a++) {
    res = min(res, matrix[0][a]);
    maximo = max(maximo, matrix[0][a]);
  }
  if (maximo == -1)
    res = 0;
  else if (res == -1) {
    res = maximo;
    for (long long a = 0; a < k; a++) {
      if (matrix[0][a] != -1) {
        res = min(res, matrix[0][a]);
      }
    }
  }
  cout << res;
}


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

const long long N = 200001;
const long long K = 201;
long long n, k;
long long arr[N];
long long matrix[N][K];

long long checkmin(long long i) {
  long long res = matrix[i][0];
  for (long long j = 0; j < k; j++) {
    if (matrix[i][j] >= 0) {
      res = min(res, matrix[i][j]);
    }
  }
  return res;
}

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

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

  for (long long r = 0; r < k; r++) {
    for (long long f = 0; f < n; f++) {
      if (r > n - f - 2)
        matrix[f][r] = -1;
    }
    matrix[n - 1][r] = 0;
    if (n - r - 2 > 0) {
      matrix[n - r - 2][r] = abs(arr[n - r - 2] - arr[n - 1]);
    }
  }

  for (long long j = n - 2; j >= 0; j--) {
    for (long long h = 0; h < k; h++) {
      if (h < n - j - 1) {
        matrix[j][h] = abs(arr[j] - arr[j + h + 1]) + checkmin(j + h + 1);
      }
    }
  }

  long long res = matrix[0][0];
  long long maximo = -1;
  for (long long a = 0; a < k; a++) {
    res = min(res, matrix[0][a]);
    maximo = max(maximo, matrix[0][a]);
  }
  if (maximo == -1)
    res = 0;
  else if (res == -1) {
    res = maximo;
    for (long long a = 0; a < k; a++) {
      if (matrix[0][a] != -1) {
        res = min(res, matrix[0][a]);
      }
    }
  }
  cout << res;
}
Problem p03161 (C++)

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

void frog2(vector<int> &cost, int m) {
  int n = cost.size();
  if (n == 0) {
    cout << 0 << endl;
  }
  int dp[n];
  dp[0] = 0;
  dp[1] = abs(cost[1] - cost[0]) + dp[0];
  int j;
  for (int i = 2; i < n; i++) {
    int mn = 1e9 + 5;
    for (int k = 0; k < m; k++) {
      mn = min(mn, abs(cost[i] - cost[i - k - 1]) + dp[i - k - 1]);
    }
    dp[i] = mn;
  }
  cout << dp[n - 1] << endl;
}

int main() {
  int n;
  int k;
  cin >> n;
  cin >> k;
  vector<int> cost;
  int val;
  for (int i = 0; i < n; i++) {
    cin >> val;
    cost.push_back(val);
  }
  frog2(cost, k);
  return 0;
}

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

void frog2(vector<int> &cost, int m) {
  int n = cost.size();
  if (n == 0) {
    cout << 0 << endl;
  }
  int dp[n];
  dp[0] = 0;
  dp[1] = abs(cost[1] - cost[0]) + dp[0];
  int j;
  for (int i = 2; i < n; i++) {
    int mn = 1e9 + 5;
    for (int k = 0; k < m; k++) {
      if (i - k - 1 >= 0)
        mn = min(mn, abs(cost[i] - cost[i - k - 1]) + dp[i - k - 1]);
    }
    dp[i] = mn;
  }
  cout << dp[n - 1] << endl;
}

int main() {
  int n;
  int k;
  cin >> n;
  cin >> k;
  vector<int> cost;
  int val;
  for (int i = 0; i < n; i++) {
    cin >> val;
    cost.push_back(val);
  }
  frog2(cost, k);
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, a, b) for (long long i = a; i < b; i++)
using namespace std;
long long n;
vector<long long> dp;
long long bf(long long p, long long pp, long long sum, vector<long long> &fec) {

  if (dp[p] != -1) {
    return dp[p];
  }
  if (pp == n - 1) {
    // cout << sum  << endl;
    return sum;
  } else if (pp >= n) {
    return 1000000000;
  }
  // cout << abs(fec[pp]-fec[p]) << "  " << p << "   " << pp << endl;
  return dp[p] = min(bf(p + 1, p, sum + abs(fec[pp] - fec[p]), fec),
                     bf(p + 2, p, sum + abs(fec[pp] - fec[p]), fec));
}

int main() {
  int k;
  cin >> n >> k;
  dp.assign(n, 10000000000000);

  vector<long long> fector(n + k, 0);
  rep(i, 0, n) { cin >> fector[i]; }
  dp[0] = 0;
  rep(i, 0, n - 1) {

    rep(j, 1, k + 1) {
      dp[i + j] = min(dp[i + j], dp[i] + abs(fector[i] - fector[i + j]));
    }
  }
  cout << dp[n - 1] << "\n";
  // cout << bf(0, 0, 0, fector) << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, a, b) for (long long i = a; i < b; i++)
using namespace std;
long long n;
vector<long long> dp;
long long bf(long long p, long long pp, long long sum, vector<long long> &fec) {

  if (dp[p] != -1) {
    return dp[p];
  }
  if (pp == n - 1) {
    // cout << sum  << endl;
    return sum;
  } else if (pp >= n) {
    return 1000000000;
  }
  // cout << abs(fec[pp]-fec[p]) << "  " << p << "   " << pp << endl;
  return dp[p] = min(bf(p + 1, p, sum + abs(fec[pp] - fec[p]), fec),
                     bf(p + 2, p, sum + abs(fec[pp] - fec[p]), fec));
}

int main() {
  int k;
  cin >> n >> k;
  dp.assign(n + k + k, 10000000000000);

  vector<long long> fector(n + k, 0);
  rep(i, 0, n) { cin >> fector[i]; }
  dp[0] = 0;
  rep(i, 0, n - 1) {

    rep(j, 1, k + 1) {
      dp[i + j] = min(dp[i + j], dp[i] + abs(fector[i] - fector[i + j]));
    }
  }
  cout << dp[n - 1] << "\n";
  // cout << bf(0, 0, 0, fector) << endl;
}
Problem p03161 (C++)

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

const int N = 100005;
const int K = 105;

int dp[K];
int vet[N];

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &vet[i]);
  }
  dp[n - 1] = 0;
  for (int i = n - 2; i >= 0; i--) {
    dp[i % (k + 1)] = oo;
    for (int j = i + 1; j < min(n, i + k + 1); j++) {
      dp[i % (k + 1)] =
          min(dp[i % (k + 1)], dp[j % (k + 1)] + abs(vet[i] - vet[j]));
    }
  }
  printf("%d\n", dp[0]);
}

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

const int N = 100005;
const int K = 105;

int dp[K];
int vet[N];

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &vet[i]);
  }
  dp[(n - 1) % (k + 1)] = 0;
  for (int i = n - 2; i >= 0; i--) {
    dp[i % (k + 1)] = oo;
    for (int j = i + 1; j < min(n, i + k + 1); j++) {
      dp[i % (k + 1)] =
          min(dp[i % (k + 1)], dp[j % (k + 1)] + abs(vet[i] - vet[j]));
    }
  }
  printf("%d\n", dp[0]);
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define F(i, a, n) for (int i = a; i < n; i++)
#define pb push_back
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
#define mod 1000000007
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int w = 1;
  // cin>>w;
  while (w--) {
    int n, k;
    cin >> n >> k;
    vi v(n);
    F(i, 0, n) { cin >> v[i]; }
    vi dp(n);
    dp[0] = 0;
    dp[1] = abs(v[0] - v[1]);
    F(i, 2, n) {
      dp[i] = INT_MAX;
      F(j, 1, k + 1)
      dp[i] = min(dp[i - j] + abs(v[i] - v[i - j]), dp[i]);
    }
    cout << dp[n - 1];
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F(i, a, n) for (int i = a; i < n; i++)
#define pb push_back
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
#define mod 1000000007
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int w = 1;
  // cin>>w;
  while (w--) {
    int n, k;
    cin >> n >> k;
    vi v(n);
    F(i, 0, n) { cin >> v[i]; }
    vi dp(n);
    dp[0] = 0;
    dp[1] = abs(v[0] - v[1]);
    F(i, 2, n) {
      dp[i] = INT_MAX;
      F(j, 1, k + 1)
      if (i - j >= 0)
        dp[i] = min(dp[i - j] + abs(v[i] - v[i - j]), dp[i]);
    }
    cout << dp[n - 1];
  }
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define pii pair<int, int>
#define makep make_pair
#define pb push_back
#define fi first
#define se second
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int main0();
int main() {
#ifndef ONLINE_JUDGE
  freopen("C:\\Users\\98497\\Desktop\\code\\file.in", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  main0();
#ifndef ONLINE_JUDGE
  fclose(stdin);
#endif
  return 0;
}

const int INF = 0x3f3f3f3f;
const int N = 1e6;

int a[N];
int dp[N];

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

  memset(dp, INF, sizeof dp);
  dp[1] = 0;
  for (int i = 2; i <= n; i++)
    for (int j = 1; j <= k; j++)
      if (i > j)
        dp[i] = min(dp[i], dp[i - j] + abs(a[i] - a[i - j]));
  cout << dp[n] << endl;
}


Fixed submission:
#define ONLINE_JUDGE
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define pii pair<int, int>
#define makep make_pair
#define pb push_back
#define fi first
#define se second
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int main0();
int main() {
#ifndef ONLINE_JUDGE
  freopen("C:\\Users\\98497\\Desktop\\code\\file.in", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  main0();
#ifndef ONLINE_JUDGE
  fclose(stdin);
#endif
  return 0;
}

const int INF = 0x3f3f3f3f;
const int N = 1e6;

int a[N];
int dp[N];

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
using P = pair<int, int>;
const int inf = 1e9;
const int minus_inf = -1e9;

struct edge {
  int to;
  int cost;
};
using graph = vector<vector<edge>>;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> dp(n, inf);
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  // initialization
  dp[0] = 0;
  for (int i = 1; i < k; i++) {
    dp[i] = abs(h[i] - h[0]);
  }
  if (k < n) {
    for (int i = k; i < n; i++) {
      for (int j = 1; j <= k; j++) {
        dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
      }
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
using P = pair<int, int>;
const int inf = 1e9;
const int minus_inf = -1e9;

struct edge {
  int to;
  int cost;
};
using graph = vector<vector<edge>>;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> dp(n, inf);
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  // initialization
  dp[0] = 0;
  if (k >= n) {
    for (int i = 1; i <= n; i++) {
      dp[i] = abs(h[i] - h[0]);
    }
  } else {
    for (int i = 1; i <= k; i++) {
      dp[i] = abs(h[i] - h[0]);
    }
    for (int i = k; i < n; i++) {
      for (int j = 1; j <= k; j++) {
        dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
      }
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

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

int main(void) {
  int N, K;
  cin >> N >> K;
  vector<long long> h(N);
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  vector<long long> dp(N, 100000007);
  dp[0] = 0;
  for (int i = 1; i < N; i++) {
    for (int j = 1; j <= i; j++) {
      if (j <= K) {
        dp[i] = min(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;

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

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

using namespace std;

int main() {
  int N, k;
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> N >> k;

  vector<int> H(N);
  vector<int> DP(N);

  for (int &x : H)
    cin >> x;
  DP[0] = 0;
  for (int i = 1; i < N; ++i) {
    DP[i] = 0x3f3f3f3f;
    for (int prev = i - 1; i - prev <= k; --prev) {
      DP[i] = min(DP[i], DP[prev] + abs(H[prev] - H[i]));
    }
  }
  cout << DP[N - 1] << endl;
  return 0;
}


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

using namespace std;

int main() {
  int N, k;
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> N >> k;

  vector<int> H(N);
  vector<int> DP(N);

  for (int &x : H)
    cin >> x;
  DP[0] = 0;
  for (int i = 1; i < N; ++i) {
    DP[i] = 0x3f3f3f3f;
    for (int prev = i - 1; i - prev <= k; --prev) {
      if (prev < 0)
        break;
      DP[i] = min(DP[i], DP[prev] + abs(H[prev] - 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_MAX);
  dp[n - 1] = 0;

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

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

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

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

  vector<int> dp(n, INT_MAX);
  dp[n - 1] = 0;

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

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAX_N 100000
#define MAX_M 100000
const int MAX = 10000000;
const int MOD = 1000000007;
#define vec vector<int>
#define vecll vector<ll>
#define vecllvec vector<vector<ll>>
#define vecb vector<bool>
#define vecbvecb vector<vector<bool>>
#define vecst vector<string>
#define vecch vector<char>
#define vecf vector<float>
#define vecvec vector<vector<int>>
#define vecHvec vector<vector<char>>
#define all(x) (x).begin(), (x).end()
const int INF = 1e9;
#define PI 3.141592653589793
// cout << setprecision(15) << std::fixed;

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAX_N 100000
#define MAX_M 100000
const int MAX = 10000000;
const int MOD = 1000000007;
#define vec vector<int>
#define vecll vector<ll>
#define vecllvec vector<vector<ll>>
#define vecb vector<bool>
#define vecbvecb vector<vector<bool>>
#define vecst vector<string>
#define vecch vector<char>
#define vecf vector<float>
#define vecvec vector<vector<int>>
#define vecHvec vector<vector<char>>
#define all(x) (x).begin(), (x).end()
const int INF = 1e9;
#define PI 3.141592653589793
// cout << setprecision(15) << std::fixed;

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

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

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

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

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

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

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

Buggy submission (status: Runtime Error):
N, K = map(int, input().split())
(*h,) = map(int, input().split())
a = [abs(h[0] - h[k]) for k in range(K)]
for n in range(N - K):
    a = a[1:] + [min([a[k] + abs(h[n + k] - h[n + K]) for k in range(K)])]
print(a[K - 1])


Fixed submission:
N, K = map(int, input().split())
K = min(N, K)
(*h,) = map(int, input().split())
a = [abs(h[0] - h[k]) for k in range(K)]
for n in range(N - K):
    a = a[1:] + [min([a[k] + abs(h[n + k] - h[n + K]) for k in range(K)])]
print(a[K - 1])
Problem p03161 (Python)

Buggy submission (status: Time Limit Exceeded):
# 配るDP
# Hは1-index
import sys

input = sys.stdin.readline
n, k = map(int, input().split())
H = [0] + list(map(int, input().split()))
dp = [10**10] * (n + 1)
dp[1] = 0
for i in range(1, n):
    for j in range(1, k + 1):
        if i + j <= n:
            dp[i + j] = min(dp[i + j], dp[i] + abs(H[i + j] - H[i]))
print(dp[n])


Fixed submission:
# 配るDP
# Hは1-index
# PyPyじゃないと厳しい。
# 二重for loop
import sys

input = sys.stdin.readline
n, k = map(int, input().split())
H = [0] + list(map(int, input().split()))
dp = [10**10] * (n + 1)
dp[1] = 0
for i in range(1, n):
    for j in range(1, k + 1):
        if i + j <= n:
            dp[i + j] = min(dp[i + j], dp[i] + abs(H[i + j] - H[i]))
print(dp[n])
Problem p03161 (Python)

Buggy submission (status: Time Limit Exceeded):
import math

n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [math.inf] * n
dp[0] = 0
for i in range(1, n):
    for j in range(max(0, i - k), i):
        dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]))
print(dp[n - 1])


Fixed submission:
import math

n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [math.inf] * n
dp[0] = 0
for i in range(1, n):
    dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(0, i - k), i))
print(dp[n - 1])
Problem p03161 (Python)

Buggy submission (status: Time Limit Exceeded):
def compute(n, k, h):
    inf = 1e9
    if n == 1:
        return 0
    dp = [inf] * n
    dp[0] = 0
    for i in range(1, n):
        cnt = 1
        while cnt <= k and i - cnt > -1:
            dp[i] = min(dp[i], dp[i - cnt] + abs(h[i] - h[i - cnt]))

    return dp[-1]


if __name__ == "__main__":
    N, K = map(int, input().split())
    h = list(map(int, input().split()))
    print(compute(N, K, h))


Fixed submission:
n, k = [int(_) for _ in input().split()]
h = [int(_) for _ in input().split()]
inf = 1e9
dp = [inf] * n
dp[0] = 0
for i in range(1, n):
    cnt = 1
    while cnt <= k and i - cnt > -1:
        dp[i] = min(dp[i], dp[i - cnt] + abs(h[i] - h[i - cnt]))
        cnt += 1
print(dp[-1])
Problem p03161 (Python)

Buggy submission (status: Time Limit Exceeded):
N, K = map(int, input().split())
H = list(map(int, input().split()))
H = [H[0]] * K + H

# dp[i] := 足場iに来るのにかかる最小のコスト
INF = 10**18
dp = [INF] * (K + N)
dp[0] = 0
for i in range(1, N + K):
    for j in range(1, K + 1):
        v = dp[i - j] + abs(H[i] - H[i - j])
        dp[i] = min(dp[i], v)
# print('dp', dp)
ans = dp[-1]
print(ans)


Fixed submission:
N, K = map(int, input().split())
H = list(map(int, input().split()))
H = [H[0]] * K + H

# dp[i] := 足場iに来るのにかかる最小のコスト
INF = 10**18
dp = [INF] * (K + N)
dp[0] = 0
for i in range(K, N + K):
    h = H[i]
    dp[i] = min(x + abs(y - h) for x, y in zip(dp[i - K : i], H[i - K : i]))
#     for j in range(1, K + 1):
#         v = dp[i - j] + abs(H[i] - H[i - j])
#         dp[i] = min(dp[i], v)
# print('dp', dp)
ans = dp[-1]
print(ans)
Problem p03161 (Python)

Buggy submission (status: Time Limit Exceeded):
#!/usr/bin/env python3

N, K = list(map(int, input().split()))
h_list = list(map(int, input().split()))
dp_list = [float("inf")] * N
dp_list[0] = 0

for i in range(1, N):
    # j番目の足場からi番目の足場に飛ぶ

    for j in range(max(i - K, 0), i + 1):
        # j番目の足場までの最適コスト
        cum_cost = dp_list[j]

        # j番目の足場から飛んだ場合のコスト
        cost = dp_list[j] + abs(h_list[j] - h_list[i])

        # 最適コストで更新
        dp_list[i] = min(dp_list[i], cost)

ans = dp_list[-1]
print(ans)


Fixed submission:
N, K = list(map(int, input().split()))
h_list = list(map(int, input().split()))
dp_list = [float("inf")] * N
dp_list[0] = 0

for i in range(1, N):
    # j番目の足場からi番目の足場に飛ぶ

    for j in range(max(i - K, 0), i + 1):
        # j番目の足場までの最適コスト
        cum_cost = dp_list[j]

        # j番目の足場から飛んだ場合のコスト
        cost = dp_list[j] + abs(h_list[j] - h_list[i])

        # 最適コストで更新
        dp_list[i] = min(dp_list[i], cost)

ans = dp_list[-1]
print(ans)
Problem p03161 (Python)

Buggy submission (status: Time Limit Exceeded):
n, k, *L = map(int, open(0).read().split())

# dp = [float("inf")] * n
dp = [0] * n

for i in range(1, n):
    j = max(0, i - k)
    dp[i] = min([DP + abs(L[i] - COST) for DP, COST in zip(dp[j:i], L[j:i])])

print(dp[-1])


Fixed submission:
n, k, *L = map(int, open(0).read().split())

# dp = [float("inf")] * n
dp = [0] * n

for i in range(1, n):
    j = max(0, i - k)
    e = L[i]
    dp[i] = min([DP + abs(e - COST) for DP, COST in zip(dp[j:i], L[j:i])])

print(dp[-1])
Problem p03161 (Python)

Buggy submission (status: Runtime Error):
N, K = map(int, input().split())
h = list(map(int, input().split()))
inf = 10**9
dp = [inf] * N
dp[0] = 0


def chmin(num: int, sbn: int):
    dp[num + sbn] = min((dp[num + sbn], dp[num] + abs(h[i + sbn] - h[num])))


for i in range(N):
    for sbn in range(K):
        try:
            chmin(dp, i, sbn + 1)
        except IndexError:
            break
print(dp[-1])


Fixed submission:
N, K = map(int, input().split())
h = list(map(int, input().split()))
inf = 10**9
dp = [inf] * N
dp[0] = 0


def chmin(num: int, sbn: int):
    dp[num + sbn] = min((dp[num + sbn], dp[num] + abs(h[i + sbn] - h[num])))


for i in range(N):
    for sbn in range(K):
        try:
            chmin(i, sbn + 1)
        except IndexError:
            break
print(dp[-1])
Problem p03161 (Python)

Buggy submission (status: Time Limit Exceeded):
n, k = map(int, input().split())
h = tuple(map(int, input().split()))

INF = 10**10
dp = [INF] * n
dp[0] = 0
# dp[i] = (足場iにたどり着いた時点でのコストの最小値)

for i in range(n):
    for j in range(i + 1, min(i + k + 1, n)):
        if dp[j] > dp[i] + abs(h[j] - h[i]):
            dp[j] = dp[i] + abs(h[j] - h[i])

print(dp[n - 1])


Fixed submission:
n, k = map(int, input().split())
h = tuple(map(int, input().split()))

INF = 10**10
dp = [INF] * n
dp[0] = 0
# dp[i] = (足場iにたどり着いた時点でのコストの最小値)

for i in range(1, n):
    dp[i] = min(dp[j] + abs(h[j] - h[i]) for j in range(max(0, i - k), i))

print(dp[n - 1])
Problem p03161 (Python)

Buggy submission (status: Runtime Error):
N, K = map(int, input().split())
H = list(map(int, input().split()))
dp = [1 << 62] * N
dp[0] = 0
for i in range(1, N):
    for j in range(i, min(i + 1 + K, N)):
        cost = abs(H[i - 1] - H[i + j])
        dp[i + j] = min(dp[i + j], dp[i - 1] + cost)
print(dp[-1])


Fixed submission:
N, K = map(int, input().split())
H = list(map(int, input().split()))
dp = [1 << 62] * N
dp[0] = 0
for i in range(1, N):
    for j in range(i, min(i + K, N)):
        cost = abs(H[i - 1] - H[j])
        dp[j] = min(dp[j], dp[i - 1] + cost)
print(dp[-1])
Problem p03161 (Python)

Buggy submission (status: Time Limit Exceeded):
n, k = map(int, input().split())
h = list(map(int, input().split()))
a = [0] * n
a[0] = 0
for i in range(1, n):
    a[i] = min([a[j] + abs(h[i] - h[j]) for j in range(max(0, i - k), i)])
print(a[-1])


Fixed submission:
def main():
    n, k = map(int, input().split())
    h = list(map(int, input().split()))
    a = [0 for i in range(n)]
    for i in range(1, n):
        a[i] = min([a[j] + abs(h[i] - h[j]) for j in range(max(0, i - k), i)])
    print(a[-1])


if __name__ == "__main__":
    main()
Problem p03161 (Python)

Buggy submission (status: Time Limit Exceeded):
from math import inf

n, k = map(int, input().split())
a = [int(i) for i in input().split()]
dp = [inf for _ in range(n)]
dp[0] = 0
for i in range(1, n):
    for j in range(1, k + 1):
        if i - j >= 0:
            dp[i] = min(dp[i], abs(a[i] - a[i - j]) + dp[i - j])

print(dp[-1])


Fixed submission:
from math import inf

n, k = map(int, input().split())
a = [int(i) for i in input().split()]
dp = [inf for _ in range(n)]
dp[0] = 0
for i in range(1, n):
    for j in range(1, k + 1):
        if i - j >= 0:
            dp[i] = min(dp[i], abs(a[i] - a[i - j]) + dp[i - j])
        else:
            break

print(dp[-1])
Problem p03161 (C++)

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

using namespace std;

#define pb push_back
#define mp make_pair
#define MOD ((int)(1e9) + 7)
#define fast                                                                   \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false)
#define filename "lynext"
#define freop                                                                  \
  freopen(filename ".cik", "w", stdout);                                       \
  freopen(filename ".gir", "r", stdin)
const long long int N = ((long long int)1e5) + 5;
const long long int M = ((long long int)1e3) + 5;
const long long int llinf = (long long int)1e18;
const int inf = INT_MAX;
typedef long long int lli;

lli dp[N];
lli n;
lli k;
lli h[N];

lli f(lli i) {
  if (dp[i] != -1)
    return dp[i];
  if (i == n)
    return 0;
  if (i == n - 1)
    return abs(h[n] - h[i]);
  lli t = llinf;
  for (int j = i + 1; j <= i + k; j++)
    t = min(t, f(j) + abs(h[i] - h[j]));
  return dp[i] = t;
}

int main() {
  fast;
  // freop;
  cin >> n >> k;
  memset(dp, -1, sizeof dp);
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  cout << f(1) << "\n";
}


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

using namespace std;

#define pb push_back
#define mp make_pair
#define MOD ((int)(1e9) + 7)
#define fast                                                                   \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false)
#define filename "lynext"
#define freop                                                                  \
  freopen(filename ".cik", "w", stdout);                                       \
  freopen(filename ".gir", "r", stdin)
const long long int N = ((long long int)1e5) + 5;
const long long int M = ((long long int)1e3) + 5;
const long long int llinf = (long long int)1e18;
const int inf = INT_MAX;
typedef long long int lli;

lli dp[N];
lli n;
lli k;
lli h[N];

lli f(lli i) {
  if (dp[i] != -1)
    return dp[i];
  if (i == n)
    return 0;
  if (i == n - 1)
    return abs(h[n] - h[i]);
  lli t = llinf;
  for (int j = i + 1; j <= min(i + k, n); j++)
    t = min(t, f(j) + abs(h[i] - h[j]));
  return dp[i] = t;
}

int main() {
  fast;
  // freop;
  cin >> n >> k;
  memset(dp, -1, sizeof dp);
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  cout << f(1) << "\n";
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define ll_MAX LONG_LONG_MAX
#define ll_MIN LONG_LONG_MIN
#define pi pair<int, int>
#define endl "\n"
#define MAXN 100005
#define mod 1000000007
using namespace std;

void solve() {}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  ll n, k;
  cin >> n >> k;
  ll h[n];
  for (ll i = 0; i < n; i++) {
    /* code */
    cin >> h[i];
  }
  vector<ll> dp(n, 1e10);
  dp[0] = 0;
  // dp[1] = abs(h[1] - h[0]);
  for (ll i = 1; i < n; i++) {
    for (ll j = 1; j <= k; j++) {
      dp[i] = min(dp[i - j] + abs(h[i] - h[i - j]), dp[i]);
    }
  }
  cout << dp[n - 1] << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define ll_MAX LONG_LONG_MAX
#define ll_MIN LONG_LONG_MIN
#define pi pair<int, int>
#define endl "\n"
#define MAXN 100005
#define mod 1000000007
using namespace std;

void solve() {}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  ll n, k;
  cin >> n >> k;
  ll h[n];
  for (ll i = 0; i < n; i++) {
    /* code */
    cin >> h[i];
  }
  vector<ll> dp(n, 1e10);
  dp[0] = 0;
  // dp[1] = abs(h[1] - h[0]);
  for (ll i = 1; i < n; i++) {
    for (ll j = 1; j <= k; j++) {
      if (i >= j)
        dp[i] = min(dp[i - j] + abs(h[i] - h[i - j]), dp[i]);
    }
  }
  cout << dp[n - 1] << '\n';
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

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

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

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

  rep(i, 1, n) {
    if (i == 1)
      dp[i] = abs(h[i] - h[i - 1]);
    else {
      int min_val = 1e9;
      rep(j, 1, k + 1) { chmin(min_val, dp[i - j] + abs(h[i] - h[i - j])); }
      chmin(dp[i], min_val);
    }
  }
  cout << dp[n - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

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

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

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <utility>
#include <vector>

#define ll long long
#define MOD 1000000007

using namespace std;

void solve() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  int dp[n];
  fill(dp, dp + n, INT_MAX);
  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(arr[j] - arr[i]));
    }
  }
  cout << dp[n - 1] << '\n';
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <utility>
#include <vector>

#define ll long long
#define MOD 1000000007

using namespace std;

void solve() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  int dp[n];
  fill(dp, 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(arr[j] - arr[i]));
      }
    }
  }
  cout << dp[n - 1] << '\n';
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}