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

Buggy submission (status: Time Limit Exceeded):
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
const int N = 2e7 + 7;
const int mod = 1e9 + 7;
const int INF = 1e18 + 7;
const int dx[] = {-1, 1, 0, 0}; // UDLR
const int dy[] = {0, 0, -1, 1}; // UDLR
inline int maximum(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
inline int minimum(int a, int b) {
  if (a > b)
    return b;
  else
    return a;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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


Fixed submission:
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
const int N = 2e7 + 7;
const int mod = 1e9 + 7;
const int INF = 1e18 + 7;
const int dx[] = {-1, 1, 0, 0}; // UDLR
const int dy[] = {0, 0, -1, 1}; // UDLR
inline int maximum(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
inline int minimum(int a, int b) {
  if (a > b)
    return b;
  else
    return a;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, k;
  cin >> n >> k;
  vector<int> h(n + 1);
  vector<int> dp(n + 1, INF);
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  dp[1] = 0;
  for (int i = 2; i <= n; i++) {
    for (int j = i - k >= 1 ? i - k : 1; j <= i; j++) {
      dp[i] = minimum(dp[j] + abs(h[i] - h[j]), dp[i]);
    }
  }
  cout << dp[n];
  return 0;
}
Problem p03161 (C++)

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

const int INF = 1001001001;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> h(100100);
  rep(i, n) cin >> h[i];
  vector<int> dp(100100, INF);
  dp[0] = 0;
  for (int i = 1; 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>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;

const int INF = 1001001001;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> h(100100);
  rep(i, n) cin >> h[i];
  vector<int> dp(100100, INF);
  dp[0] = 0;
  for (int i = 1; i < n; i++) {
    for (int j = 1; j <= k; j++) {
      if (i > j - 1) {
        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 <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int h[100005], k;
vector<int> mem(100005, -1);
int dp(int n);
signed main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  cout << dp(n) << "\n";
  return 0;
}
int dp(int n) {
  if (n == 1) {
    mem[1] = 0;
    return 0;
  }
  if (n == 2) {
    mem[2] = abs(h[n - 1] - h[n]);
    return abs(h[n - 1] - h[n]);
  }
  if (mem[n] < 0) {
    int minv = 1000000007;
    for (int i = 1; i <= k; i++) {
      minv = min(minv, abs(h[n] - h[n - i]) + dp(n - i));
    }
    mem[n] = minv;
  }
  return mem[n];
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int h[100005], k;
vector<int> mem(100005, -1);
int dp(int n);
signed main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  cout << dp(n) << "\n";
  return 0;
}
int dp(int n) {
  if (n == 1) {
    mem[1] = 0;
    return 0;
  }
  if (n == 2) {
    mem[2] = abs(h[n - 1] - h[n]);
    return abs(h[n - 1] - h[n]);
  }
  if (mem[n] < 0) {
    int minv = 1000000007;
    for (int i = 1; i <= k && n - i > 0; i++) {
      minv = min(minv, abs(h[n] - h[n - i]) + dp(n - i));
    }
    mem[n] = minv;
  }
  return mem[n];
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
#define fi first
#define se second
set<string> c;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const ll inf = 100000000000000000;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll c, ll d) { return c / gcd(c, d) * d; }
map<ll, ll> mp;
ll dp[110];
int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> h(n);
  for (int i = 0; i < n; i++)
    cin >> h.at(i);
  fill(dp, dp + n, 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] + abs(h.at(i) - h.at(i + j)));
    }
  }
  cout << dp[n - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
#define fi first
#define se second
set<string> c;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const ll inf = 100000000000000000;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll c, ll d) { return c / gcd(c, d) * d; }
map<ll, ll> mp;
ll dp[200000];
int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> h(n);
  for (int i = 0; i < n; i++)
    cin >> h.at(i);
  fill(dp, dp + n, 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] + abs(h.at(i) - h.at(i + j)));
    }
  }
  cout << dp[n - 1] << endl;
}
Problem p03161 (C++)

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

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

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

  cout << minCost.back();
  return 0;
}


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

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

  vector<int> minCost(n);
  minCost[0] = 0;
  minCost[1] = abs(height[0] - height[1]);
  for (int i = 2; i < n; i++) {
    minCost[i] = abs(height[i] - height[i - 1]) + minCost[i - 1];
    for (int j = i - 2; j >= i - k && j >= 0; j--)
      minCost[i] = min(minCost[i], abs(height[i] - height[j]) + minCost[j]);
  }

  cout << minCost.back();
  return 0;
}
Problem p03161 (C++)

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

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

  vector<int> dp(n, INF);
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= min(n - 1, i + k); j++) {
      dp[j] = min(dp[j], dp[i] + abs(h[j] - h[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> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  const int INF = 1e9 + 10;

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

Buggy submission (status: Runtime Error):
// Author - Sumit

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

#define ll long long
#define int ll
#define ps push
#define pb push_back
#define INF 1000000005
#define MOD 1000000007
#define mp make_pair
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repd(i, a, b) for (int i = a; i >= b; i--)
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define F first
#define S second
#define mii map<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vpii vector<pair<int, int>>
#define itr ::iterator it
#define WL(t) while (t--)
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define debug(x) cout << x << endl;
#define debug2(x, y) cout << x << " " << y << endl;
#define debug3(x, y, z) cout << x << " " << y << " " << z << endl;

void solve() {
  int n, k;
  cin >> n >> k;
  int a[n];
  rep(i, 0, n) cin >> a[i];
  int ar[n];
  // memset(ar,INF,sizeof(ar));
  ar[0] = 0;
  ar[1] = abs(a[1] - a[0]);

  rep(i, 2, n) {
    // ar[i] = min(ar[i-1]+abs(a[i]-a[i-1]),ar[i-2]+abs(a[i]-a[i-2]));
    ar[i] = INF;
    rep(j, 1, k + 1) {
      if ((i - j) >= 0) {
        // cout<<(i-j)<<" ";
        ar[i] = min(ar[i], ar[i - j] + abs(a[i] - a[i - j]));
      }
    }
  }
  // cout<<endl;
  // for(auto x:ar){
  //     cout<<x<<" ";
  // }cout<<endl;
  debug(ar[n - 1]);
}

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

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

  int t = 1;
  // cin>>t;
  WL(t) { solve(); }
}

Fixed submission:
// Author - Sumit

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

#define ll long long
#define int ll
#define ps push
#define pb push_back
#define INF 1000000005
#define MOD 1000000007
#define mp make_pair
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repd(i, a, b) for (int i = a; i >= b; i--)
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define F first
#define S second
#define mii map<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vpii vector<pair<int, int>>
#define itr ::iterator it
#define WL(t) while (t--)
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define debug(x) cout << x << endl;
#define debug2(x, y) cout << x << " " << y << endl;
#define debug3(x, y, z) cout << x << " " << y << " " << z << endl;

void solve() {
  int n, k;
  cin >> n >> k;
  int a[n];
  rep(i, 0, n) cin >> a[i];
  int ar[n];
  // memset(ar,INF,sizeof(ar));
  ar[0] = 0;
  ar[1] = abs(a[1] - a[0]);

  rep(i, 2, n) {
    // ar[i] = min(ar[i-1]+abs(a[i]-a[i-1]),ar[i-2]+abs(a[i]-a[i-2]));
    ar[i] = INF;
    rep(j, 1, k + 1) {
      if ((i - j) >= 0) {
        // cout<<(i-j)<<" ";
        ar[i] = min(ar[i], ar[i - j] + abs(a[i] - a[i - j]));
      }
    }
  }
  // cout<<endl;
  // for(auto x:ar){
  //     cout<<x<<" ";
  // }cout<<endl;
  debug(ar[n - 1]);
}

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int t = 1;
  // cin>>t;
  WL(t) { solve(); }
}
Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
/*राधे कृष्णा*/ /*राधे कृष्णा*/ /*राधे कृष्णा*/ /*राधे कृष्णा*/
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <string>
#include <vector>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
// #define test ll t;cin >> t;while(t--)
#define ll long long int
#define sll stack<ll>
#define qll queue<ll>
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpi vector<pair<int, int>>
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define ln "\n"
#define f0(i, n) for (i = 0; i < n; i++)
#define f1(i, n) for (i = 1; i < n; i++)
#define fab(i, a, b) for (i = a; i < b; i++)
// typical dp problem
void solve() {
  ll n, k, i, j;
  cin >> n >> k;
  ll h[n + 1];
  for (i = 1; i <= n; i++)
    cin >> h[i];
  ll dp[n + 1];
  dp[0] = 0;
  dp[1] = 0;
  dp[2] = abs(h[2] - h[1]);
  for (i = 3; i <= n; i++) {
    ll x = 1e9 + 100;
    for (j = 1; j < i; j++) {
      if (i - j > k)
        continue;
      x = min(x, (dp[j] + abs(h[i] - h[j])));
    }
    dp[i] = x;
  }
  // for(i=1;i<=n;i++)
  cout << dp[n]; //<< "   ";
}
int main() {
  fastio
  /*ll t;
  cin >> t;
  while(t--){*/
  solve();
  //}
  return 0;
}

Fixed submission:
/*राधे कृष्णा*/ /*राधे कृष्णा*/ /*राधे कृष्णा*/ /*राधे कृष्णा*/
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <string>
#include <vector>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
// #define test ll t;cin >> t;while(t--)
#define ll long long int
#define sll stack<ll>
#define qll queue<ll>
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpi vector<pair<int, int>>
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define ln "\n"
#define f0(i, n) for (i = 0; i < n; i++)
#define f1(i, n) for (i = 1; i < n; i++)
#define fab(i, a, b) for (i = a; i < b; i++)
// typical dp problem
void solve() {
  ll n, k, i, j;
  cin >> n >> k;
  ll h[n + 1];
  for (i = 1; i <= n; i++)
    cin >> h[i];
  ll dp[n + 1];
  dp[0] = 0;
  dp[1] = 0;
  dp[2] = abs(h[2] - h[1]);
  for (i = 3; i <= n; i++) {
    ll x = 1e9 + 100;
    j = max(1LL, i - k);
    for (; j < i; j++) {
      // if(i-j >k) {j=j+(i-j) ;continue;}
      x = min(x, (dp[j] + abs(h[i] - h[j])));
    }
    dp[i] = x;
  }
  // for(i=1;i<=n;i++)
  cout << dp[n]; //<< "   ";
}
int main() {
  fastio
  /*ll t;
  cin >> t;
  while(t--){*/
  solve();
  //}
  return 0;
}
Problem p03161 (C++)

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

void cc_env() {
#ifndef ONLINE_JUDGE
  freopen("input", "r", stdin);
  freopen("output", "w", stdout);
  //
#endif
}

void self_min(int &a, int b) { a = min(a, b); }

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

  vector<int> 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)
        self_min(dp[j], dp[i] + abs(height[j] - height[i]));
    }
  }

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


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

void cc_env() {
#ifndef ONLINE_JUDGE
  freopen("input", "r", stdin);
  freopen("output", "w", stdout);
  //
#endif
}

void self_min(int &a, int b) { a = min(a, b); }

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

  vector<int> 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)
        self_min(dp[j], dp[i] + abs(height[j] - height[i]));
    }
  }

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

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

ll n, k;
vector<ll> arr, dp;

void init() {
  for (ll i = 0; i < n; i++)
    dp[i] = -1;
}

ll solve(ll i) {
  if (dp[i] != -1)
    return dp[i];

  if (i <= 0)
    return 0;

  else {
    ll z = 1000000000;
    for (ll j = 1; j <= k; j++) {
      z = min(z, solve(i - j) + abs(arr[i] - arr[i - j]));
    }
    dp[i] = z;
    return z;
  }
}

int main() {
  cin >> n >> k;
  arr.resize(n);
  dp.resize(n);
  init();
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  cout << solve(n - 1) << "\n";
  return 0;
}

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

ll n, k;
vector<ll> arr, dp;

void init() {
  for (ll i = 0; i < n; i++)
    dp[i] = -1;
}

ll solve(ll i) {
  if (dp[i] != -1)
    return dp[i];

  if (i <= 0)
    return 0;

  else {
    ll z = 1000000000;
    for (ll j = 1; j <= k; j++) {
      if (i >= j)
        z = min(z, solve(i - j) + abs(arr[i] - arr[i - j]));
    }
    dp[i] = z;
    return z;
  }
}

int main() {
  cin >> n >> k;
  arr.resize(n);
  dp.resize(n);
  init();
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  cout << solve(n - 1) << "\n";
  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 N;
long long h[110000];

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

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

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

  dp[0] = 0;

  for (int i = 0; i < N - 1; i++) {
    for (int j = 1; j <= K; j++) {

      chmin(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
    }
  }
  cout << dp[N - 1] << endl;
}


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 N;
long long h[110000];

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

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

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

  dp[0] = 0;

  for (int i = 0; i < N - 1; i++) {
    for (int j = 1; j <= K; j++) {

      chmin(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
    }
  }
  cout << dp[N - 1] << endl;
}
Problem p03161 (C++)

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

typedef long long ll;
typedef unsigned long long ull;

int main() {
  int N, K;
  cin >> N >> K;
  int h[10000];
  ll dp[10000];
  for (int i = 0; i < N; i++)
    cin >> h[i];
  dp[0] = 0;
  for (int i = 1; i < N; i++) {
    dp[i] = INT64_MAX;
    for (int j = 1; (i - j) >= 0 && j <= K; j++) {
      dp[i] = min(dp[i], dp[i - j] + abs(h[i - j] - h[i]));
    }
  }

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

  return 0;
}

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

typedef long long ll;
typedef unsigned long long ull;

int main() {
  int N, K;
  cin >> N >> K;
  int h[100000];
  ll dp[100000];
  for (int i = 0; i < N; i++)
    cin >> h[i];
  dp[0] = 0;
  for (int i = 1; i < N; i++) {
    dp[i] = INT64_MAX;
    for (int j = 1; (i - j) >= 0 && j <= K; j++) {
      dp[i] = min(dp[i], dp[i - j] + 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>

#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define mem(x) memset(x, 0, sizeof(x));
#define what_is(x) cout << #x << " is " << x << flush << endl;
#define rep(i, a, b) for (int i = a; i < b; i++)

using namespace std;
const int maxn = 100010;
int n, k;
int a[maxn];
int dp[maxn];
void solve() {
  cin >> n >> k;
  rep(i, 0, n) cin >> a[i];
  dp[1] = abs(a[1] - a[0]);
  rep(i, 2, n) {
    dp[i] = INT_MAX;
    for (int j = i - 1; j >= 0 && i - j <= k; j--)
      dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j]));
  }
  cout << dp[n - 1] << endl;
}

signed main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int t = 1;
  // cin>>t;

  while (t--)
    solve();

  return 0;
}

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

#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define mem(x) memset(x, 0, sizeof(x));
#define what_is(x) cout << #x << " is " << x << flush << endl;
#define rep(i, a, b) for (int i = a; i < b; i++)

using namespace std;
const int maxn = 100010;
int n, k;
int a[maxn];
int dp[maxn];
void solve() {
  cin >> n >> k;
  rep(i, 0, n) cin >> a[i];
  dp[1] = abs(a[1] - a[0]);
  rep(i, 2, n) {
    dp[i] = INT_MAX;
    for (int j = i - 1; j >= 0 && i - j <= k; j--)
      dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j]));
  }
  cout << dp[n - 1] << endl;
}

signed main() {

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

  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 ll long long
#define FOR(i, x, n) for (ll i = x; i <= n; ++i)
#define REP(i, x) for (ll i = 0; i < x; ++i)
#define FORD(i, x, n) for (ll i = x; i >= n; --i)

const int INF = 1e9;

ll n;
ll h[2 * (10 ^ 5)];

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

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

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

const int INF = 1e9;

ll n;
vector<ll> h(2000000);

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
const ll mod = 1e9 + 7;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
vector<ll> v;
int main() {
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  vector<ll> dp(n, 1e9);
  for (ll i = 0; i < n; i++) {
    ll s;
    cin >> s;
    v.push_back(s);
  }
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (ll i = 0; i < n; i++) {
    if (i == 0) {
      dp[i] = 0;
    } else if (i == 1) {
      dp[i] = abs(v[i] - v[i - 1]);
    } else {
      for (ll j = 1; j <= k; j++) {
        dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j]));
      }
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
const ll mod = 1e9 + 7;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
vector<ll> v;
int main() {
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  vector<ll> dp(n, 1e9);
  for (ll i = 0; i < n; i++) {
    ll s;
    cin >> s;
    v.push_back(s);
  }
  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (ll i = 0; i < n; i++) {
    if (i == 0) {
      dp[i] = 0;
    } else if (i == 1) {
      dp[i] = abs(v[i] - v[i - 1]);
    } else {
      for (ll j = 1; j <= k; j++) {
        if (i - j < 0)
          break;
        dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[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;
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(x) (x).begin(), (x).end()    // 昇順
#define RALL(x) (x).rbegin(), (x).rend() // 降順
#define SZ(x) ((int)(x).size)
const long long mod = 1e9 + 7;
typedef priority_queue<int> PQ;
typedef vector<long long> Vl;
typedef vector<int> VI; // VI a(n);
typedef vector<string> VS;
typedef vector<VS> VSS;
typedef vector<VI> VVI; // VVI a(n,vector<int>(m)) n * m
typedef vector<Vl> VVl;
typedef pair<int, int> PII;
typedef vector<pair<int, int>> PS;
typedef long long ll; // ll とdoubleは違う
// cout << << endl;
// cin >> ;

int main() {
  ll x = 0, g = 0, n, k, w, l, ans, ptr = 0;
  string s, s1, s2;
  cin >> n >> k;
  VI h(n);
  VI dp(n, INF);
  dp[0] = 0;
  rep(i, n) { cin >> h[i]; }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
      if (abs(i - j) <= k) {
        dp[i] = min(dp[j] + abs(h[i] - h[j]), dp[i]);
      } else
        continue;
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(x) (x).begin(), (x).end()    // 昇順
#define RALL(x) (x).rbegin(), (x).rend() // 降順
#define SZ(x) ((int)(x).size)
const long long mod = 1e9 + 7;
typedef priority_queue<int> PQ;
typedef vector<long long> Vl;
typedef vector<int> VI; // VI a(n);
typedef vector<string> VS;
typedef vector<VS> VSS;
typedef vector<VI> VVI; // VVI a(n,vector<int>(m)) n * m
typedef vector<Vl> VVl;
typedef pair<int, int> PII;
typedef vector<pair<int, int>> PS;
typedef long long ll; // ll とdoubleは違う
// cout << << endl;
// cin >> ;

int main() {
  ll x = 0, g = 0, n, k, w, l, ans, ptr = 0;
  string s, s1, s2;
  cin >> n >> k;
  VI h(n);
  VI dp(n, INF);
  dp[0] = 0;
  rep(i, n) { cin >> h[i]; }
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < k; j++) {
      if (i + j + 1 < n)
        dp[i + j + 1] = min(dp[i] + abs(h[i] - h[i + j + 1]), dp[i + j + 1]);
      else
        break;
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define vi vector<int>
#define vl vector<long>
#define vll vector<long long int>
#define vC vector<char>
#define FastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

/*ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}*/
int main() {
  int n, i, j, k;
  cin >> n >> k;
  vi a(n), m(n);
  for (i = 0; i < n; i++) {
    cin >> a[i];
    m[i] = INT_MAX;
  }
  m[0] = 0;
  m[1] = abs(a[1] - a[0]);
  for (i = 2; i < n; i++) {
    j = 1;
    while (j <= k || i - j > 0) {
      m[i] = min((abs(a[i - j] - a[i]) + m[i - j]), m[i]);
      j++;
    }
  }
  cout << m[n - 1];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define vi vector<int>
#define vl vector<long>
#define vll vector<long long int>
#define vC vector<char>
#define FastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

/*ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}*/
int main() {
  int n, i, j, k;
  cin >> n >> k;
  vi a(n), m(n);
  for (i = 0; i < n; i++) {
    cin >> a[i];
    m[i] = INT_MAX;
  }
  m[0] = 0;
  m[1] = abs(a[1] - a[0]);
  for (i = 2; i < n; i++) {
    j = 1;
    while (j <= k && i - j >= 0) {
      m[i] = min((abs(a[i - j] - a[i]) + m[i - j]), m[i]);
      j++;
    }
  }
  cout << m[n - 1];
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define IFOR(i, m, n) for (int i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
const int MOD = 1000000007;
/* テンプレートここまで */

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

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

  vector<int> h(N);

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

  vector<ll> dp(N, INF);
  dp[0] = 0;

  REP(i, N - 1) {
    FOR(j, 1, K + 1) { chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); }
  }

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


Fixed submission:
#include <bits/stdc++.h>
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define IFOR(i, m, n) for (int i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
const int MOD = 1000000007;
/* テンプレートここまで */

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

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

  vector<int> h(N);

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

  vector<ll> dp(N, INF);
  dp[0] = 0;

  REP(i, N - 1) {
    FOR(j, 1, K + 1) {
      if (i + j >= N)
        continue;
      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 <algorithm>
#include <cstdio>
using namespace std;
int n, m, i, j, k, a, b, c, dp[500000], cc[500000];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &cc[i]);
  for (int i = 2; i <= n; i++)
    dp[i] = 9999999999;
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= j + k; j++)
      dp[j] = min(dp[j], dp[i] + abs(cc[i] - cc[j]));
  }
  printf("%d\n", dp[n]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
int n, m, i, j, k, a, b, c, dp[500000], cc[500000];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &cc[i]);
  for (int i = 2; i <= n; i++)
    dp[i] = 9999999999;
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= i + k; j++)
      dp[j] = min(dp[j], dp[i] + abs(cc[i] - cc[j]));
  }
  printf("%d\n", dp[n]);
  return 0;
}
Problem p03161 (C++)

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int a[n], dp[n];
  for (int i = 0; i < n; i++)
    dp[i] = 1000000000;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  if (n > 2)
    dp[0] = 0;
  else {
    cout << abs(a[1] - a[0]);
    return 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(a[j] - a[i]));
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 5;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  for (int &x : h)
    cin >> x;
  vector<int> dp(n, INF);
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j <= i + k; i++) {
      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;
using ll = long long;
const int INF = 1e9 + 5;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  for (int &x : h)
    cin >> 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]));
      }
    }
  }
  cout << dp[n - 1];

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

int main() {
  int n, K;
  cin >> n >> K;
  vector<int> ht(n + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> ht[i];

  vector<int> dp(n + 1, INT_MAX);
  dp[0] = 0;
  dp[1] = 0;
  for (int i = 1; i <= K; i++)
    dp[i] = abs(ht[i] - ht[1]);
  for (int i = 2; i <= n; i++) {
    for (int k = 1; k <= K; k++)
      dp[i] = min(abs(ht[i] - ht[i - k]) + dp[i - k], dp[i]);
  }
  cout << dp[n];
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

int main() {
  int n, K;
  cin >> n >> K;
  vector<int> ht(n + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> ht[i];

  vector<int> dp(n + 1, INT_MAX);
  dp[0] = 0;
  dp[1] = 0;
  for (int i = 1; i <= K; i++)
    dp[i] = abs(ht[i] - ht[1]);
  for (int i = 2; i <= n; i++) {
    for (int k = 1; k <= K && i - k >= 1; k++)
      dp[i] = min(abs(ht[i] - ht[i - k]) + dp[i - k], dp[i]);
  }
  cout << dp[n];
}
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<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pll> vpll;

#define test(t)                                                                \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define forn(i, n) for (i = 0; i < n; i++)
#define rforn(i, n) for (i = n - 1; i >= 0; i--)
#define all(v) (v).begin(), (v).end()
const int MOD = 1e9 + 7, MAX = 1e5 + 5;
ll powN(ll a, ll p) {
  if (p == 0)
    return 1;
  ll z = powN(a, p / 2);
  z = (z * z) % MOD;
  if (p % 2)
    z = (z * a) % MOD;
  return z;
}
vector<bool> is_prime(MAX + 1, true);
void Sieve() {
  is_prime[0] = is_prime[1] = false;
  int i, j;
  for (i = 2; i * i <= MAX; i++) {
    if (is_prime[i]) {
      for (j = i * i; j <= MAX; j += i)
        is_prime[j] = false;
    }
  }
}
ll modInv(ll a) { return powN(a, MOD - 2); }

int main() {
  int n, k;
  cin >> n >> k;
  vi h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  vi dp(n, INT_MAX);
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; 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;
}

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

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pll> vpll;

#define test(t)                                                                \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define forn(i, n) for (i = 0; i < n; i++)
#define rforn(i, n) for (i = n - 1; i >= 0; i--)
#define all(v) (v).begin(), (v).end()
const int MOD = 1e9 + 7, MAX = 1e5 + 5;
ll powN(ll a, ll p) {
  if (p == 0)
    return 1;
  ll z = powN(a, p / 2);
  z = (z * z) % MOD;
  if (p % 2)
    z = (z * a) % MOD;
  return z;
}
vector<bool> is_prime(MAX + 1, true);
void Sieve() {
  is_prime[0] = is_prime[1] = false;
  int i, j;
  for (i = 2; i * i <= MAX; i++) {
    if (is_prime[i]) {
      for (j = i * i; j <= MAX; j += i)
        is_prime[j] = false;
    }
  }
}
ll modInv(ll a) { return powN(a, MOD - 2); }

int main() {
  int n, k;
  cin >> n >> k;
  vi h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  vi dp(n, INT_MAX);
  dp[0] = 0;
  for (int i = 1; i < n; i++) {
    for (int j = 1; j <= min(i, k); j++) {
      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: Runtime Error):
#include <algorithm>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <set>
#include <string>
#include <vector>

template <typename T> void vc(T t) { std::cout << t << " "; }

template <typename T, typename... Args> void vc(T t, Args... args) {
  // recursive variadic function
  std::cout << t << " ";
  vc(args...);
}

const int N = 100000;
std::vector<int> dp(N + 1, -1);
int dpf(const std::vector<int> &h, int r, int n, int k) {
  if (dp[r] != -1) {
    return dp[r];
  }
  if (r == n) {
    return 0;
  }
  // if (r == n - 1) {
  //   dp[r] = std::abs(h[r] - h[n]);
  //   return dp[r];
  // }
  dp[r] = 0x3fffffff;
  for (int i = 1; i <= k; ++i) {
    dp[r] = std::min(dp[r], std::abs(h[r] - h[r + i]) + dpf(h, r + i, n, k));
  }
  // vc(" +", r, dp[r], "\n");
  return dp[r];
}

void once() {
  int n, k;
  std::cin >> n >> k;
  std::vector<int> h(n + 1);
  for (int i = 1; i <= n; ++i) {
    std::cin >> h[i];
  }
  std::cout << dpf(h, 1, n, k) << std::endl;
}

int main() {
  int n = 1;
  std::ios::sync_with_stdio(false);
  // std::cin >> n;
  while (n--) {
    once();
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <set>
#include <string>
#include <vector>

template <typename T> void vc(T t) { std::cout << t << " "; }

template <typename T, typename... Args> void vc(T t, Args... args) {
  // recursive variadic function
  std::cout << t << " ";
  vc(args...);
}

const int N = 100000;
std::vector<int> dp(N + 1, -1);
int dpf(const std::vector<int> &h, int r, int n, int k) {
  if (dp[r] != -1) {
    return dp[r];
  }
  if (r == n) {
    return 0;
  }
  // if (r == n - 1) {
  //   dp[r] = std::abs(h[r] - h[n]);
  //   return dp[r];
  // }
  dp[r] = 0x3fffffff;
  for (int i = 1; i <= k && r + i <= n; ++i) {
    dp[r] = std::min(dp[r], std::abs(h[r] - h[r + i]) + dpf(h, r + i, n, k));
  }
  // vc(" +", r, dp[r], "\n");
  return dp[r];
}

void once() {
  int n, k;
  std::cin >> n >> k;
  std::vector<int> h(n + 1);
  for (int i = 1; i <= n; ++i) {
    std::cin >> h[i];
  }
  std::cout << dpf(h, 1, n, k) << std::endl;
}

int main() {
  int n = 1;
  std::ios::sync_with_stdio(false);
  // std::cin >> n;
  while (n--) {
    once();
  }
}
Problem p03161 (C++)

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

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

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

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

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

using namespace std;

int main() {
  int n;
  cin >> n;
  int j;
  cin >> j;
  vector<int> vec;
  int dp[n + 20];
  for (int i = 0; i < n; i++) {
    int h;
    cin >> h;
    vec.push_back(h);
    dp[i] = 2000000000;
  }
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int k = 1; k <= j; k++)
      if (abs(vec[i] - vec[i + k]) + dp[i] < dp[i + k])
        dp[i + k] = abs(vec[i] - vec[i + k]) + dp[i];
  }
  printf("%d\n", dp[n - 1]);
}

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

using namespace std;

int main() {
  int n;
  cin >> n;
  int j;
  cin >> j;
  vector<int> vec;
  int dp[n + 300];
  for (int i = 0; i < n; i++) {
    int h;
    cin >> h;
    vec.push_back(h);
    dp[i] = 2000000000;
  }
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int k = 1; k <= j; k++)
      if (abs(vec[i] - vec[i + k]) + dp[i] < dp[i + k])
        dp[i + k] = abs(vec[i] - vec[i + k]) + dp[i];
  }
  printf("%d\n", dp[n - 1]);
}
Problem p03161 (C++)

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

#define int int64_t

int MOD = 1000000007;

int minCost(int *A, int N, int K, int *dp) {
  if (N == 0)
    return 0;

  if (N == 1)
    return abs(A[1] - A[0]);

  if (dp[N] != -1)
    return dp[N];

  int ans = INT_MAX;
  for (int i = 1; i <= K; i++)
    ans = min(minCost(A, N - i, K, dp) + abs(A[N] - A[N - i]), ans);

  return dp[N] = ans;
}

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

  int N, K;
  cin >> N >> K;

  int *A = new int[N]();
  for (int i = 0; i < N; i++)
    cin >> A[i];

  int *dp = new int[N]();
  for (int i = 0; i < N; i++)
    dp[i] = -1;

  int ans = minCost(A, N - 1, K, dp);

  cout << ans;

  delete[] dp;
  delete[] A;

  return 0;
}


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

#define int int64_t

int MOD = 1000000007;

int minCost(int *A, int N, int K, int *dp) {
  if (N < 0)
    return INT_MAX;

  if (N == 0)
    return 0;

  if (N == 1)
    return abs(A[1] - A[0]);

  if (dp[N] != -1)
    return dp[N];

  int ans = INT_MAX;
  for (int i = 1; i <= K; i++)
    ans = min(minCost(A, N - i, K, dp) + abs(A[N] - A[N - i]), ans);

  return dp[N] = ans;
}

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

  int N, K;
  cin >> N >> K;

  int *A = new int[N]();
  for (int i = 0; i < N; i++)
    cin >> A[i];

  int *dp = new int[N]();
  for (int i = 0; i < N; i++)
    dp[i] = -1;

  int ans = minCost(A, N - 1, K, dp);

  cout << ans;

  delete[] dp;
  delete[] A;

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int maxN = 100200;
int mem[maxN];
int nums[maxN];
int solve(int i, int n, int k) {
  if (i == n - 1) {
    return 0;
  }
  int res = 2000000000;
  for (int j = i + 1; j <= i + k; j++) {
    if (j < n) {
      res = min(res, abs(nums[i] - nums[j]) + solve(j, n, k));
    }
  }
  return mem[i] = res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> nums[i];
  }
  memset(mem, -1, sizeof(mem));
  cout << solve(0, n, k) << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxN = 100200;
int mem[maxN];
int nums[maxN];
int solve(int i, int n, int k) {
  if (i == n - 1) {
    return 0;
  }
  if (mem[i] != -1) {
    return mem[i];
  }
  int res = 2000000000;
  for (int j = i + 1; j <= i + k; j++) {
    if (j < n) {
      res = min(res, abs(nums[i] - nums[j]) + solve(j, n, k));
    }
  }
  return mem[i] = res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> nums[i];
  }
  memset(mem, -1, sizeof(mem));
  cout << solve(0, n, k) << "\n";
}
Problem p03161 (C++)

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

int main() {
  int n;
  cin >> n;
  int ar[n];
  int dp[n];
  int k;
  cin >> k;
  for (int i = 0; i < n; i++)
    cin >> ar[i];
  dp[0] = 0;
  for (int i = 1; i <= k; i++) {
    int mini = 100000007;
    for (int j = 1; j <= i; j++)
      mini = min(mini, (dp[i - j] + abs(ar[i] - ar[i - j])));
    dp[i] = mini;
  }
  for (int i = k + 1; i < n; i++) {
    int mini = 10000007;
    for (int j = 1; j <= k; j++)
      mini = min(mini, (dp[i - j] + abs(ar[i] - ar[i - j])));
    dp[i] = mini;
  }

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


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

int main() {
  int n;
  cin >> n;
  int ar[n];
  int dp[n];
  int k;
  cin >> k;
  for (int i = 0; i < n; i++)
    cin >> ar[i];
  dp[0] = 0;
  for (int i = 1; i < n; i++) {
    int mini = 1000000007;
    if (i - k < 0) {
      for (int j = 0; j < i; j++)
        mini = min(mini, (dp[j] + abs(ar[j] - ar[i])));
      dp[i] = mini;
    } else {
      for (int j = i - k; j < i; j++)
        mini = min(mini, (dp[j] + abs(ar[j] - ar[i])));
      dp[i] = mini;
    }
  }

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

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

int n, k, v[100010], dp[100010];

int recur(int pos) {
  if (pos == n - 1)
    return 0;
  if (pos >= n)
    return INF;

  if (dp[pos] != -1)
    return dp[pos];

  int ret = INF;
  for (int i = 1; i <= k; ++i)
    ret = min(ret, recur(pos + i) + abs(v[pos] - v[pos + i]));

  return ret;
}

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

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

  memset(dp, -1, sizeof dp);
  cout << recur(0) << endl;
  return 0;
}

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

int n, k, v[100010], dp[100010];

int recur(int pos) {
  if (pos == n - 1)
    return 0;
  if (pos >= n)
    return INF;

  if (dp[pos] != -1)
    return dp[pos];

  int ret = INF;
  for (int i = 1; i <= k; ++i)
    ret = min(ret, recur(pos + i) + abs(v[pos] - v[pos + i]));

  dp[pos] = ret;
  return ret;
}

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

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

  memset(dp, -1, sizeof dp);
  cout << recur(0) << endl;
  return 0;
}