Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define sorted(a_1) sort(a_1.begin(), a_1.end())
#define rsorted(a_1) sort(a_1.rbegin(), a_1.rend())
#define t1(a_1) cout << a_1 << endl;
#define t2(a_1)                                                                \
  for (auto it_test : a_1)                                                     \
    cout << it_test << " ";
#define end cout << endl;

const int MAX = 1000001;
vector<long long> isprime(MAX, true);
vector<long long> prime;
vector<long long> SPF(MAX);
void Seive(int N) {
  isprime[0] = isprime[1] = false;

  for (long long int i = 2; i < N; i++) {
    if (isprime[i]) {
      prime.push_back(i);
      SPF[i] = i;
    }

    for (long long int j = 0;
         j < (int)prime.size() && i * prime[j] < N && prime[j] <= SPF[i]; j++) {
      isprime[i * prime[j]] = false;

      SPF[i * prime[j]] = prime[j];
    }
  }
}

ll expo(ll a, ll b, ll m) {
  if (b == 0)
    return 1;
  ll p = expo(a, b / 2, m) % m;
  p = (p * p) % m;

  return (b % 2 == 0) ? p : (a * p) % m;
}

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> arr(n);
  for (auto &i : arr)
    cin >> i;
  vector<int> dp(n, INT_MAX);
  dp[0] = 0;
  dp[1] = abs(arr[1] - arr[0]);
  for (int i = 2; i < n; i++) {
    for (int j = 1; j <= k; j++)
      dp[i] = min(dp[i], abs(arr[i] - arr[i - j]) + dp[i - j]);
  }
  cout << dp[n - 1] << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define sorted(a_1) sort(a_1.begin(), a_1.end())
#define rsorted(a_1) sort(a_1.rbegin(), a_1.rend())
#define t1(a_1) cout << a_1 << endl;
#define t2(a_1)                                                                \
  for (auto it_test : a_1)                                                     \
    cout << it_test << " ";
#define end cout << endl;

const int MAX = 1000001;
vector<long long> isprime(MAX, true);
vector<long long> prime;
vector<long long> SPF(MAX);
void Seive(int N) {
  isprime[0] = isprime[1] = false;

  for (long long int i = 2; i < N; i++) {
    if (isprime[i]) {
      prime.push_back(i);
      SPF[i] = i;
    }

    for (long long int j = 0;
         j < (int)prime.size() && i * prime[j] < N && prime[j] <= SPF[i]; j++) {
      isprime[i * prime[j]] = false;

      SPF[i * prime[j]] = prime[j];
    }
  }
}

ll expo(ll a, ll b, ll m) {
  if (b == 0)
    return 1;
  ll p = expo(a, b / 2, m) % m;
  p = (p * p) % m;

  return (b % 2 == 0) ? p : (a * p) % m;
}

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> arr(n);
  for (auto &i : arr)
    cin >> i;
  vector<int> dp(n, INT_MAX);
  dp[0] = 0;
  dp[1] = abs(arr[1] - arr[0]);
  for (int i = 2; i < n; i++) {
    for (int j = 1; j <= k && j <= i; j++)
      dp[i] = min(dp[i], abs(arr[i] - arr[i - j]) + dp[i - j]);
  }
  cout << dp[n - 1] << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}