Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define mod 1000000007
#define inf 1e18
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
using namespace std;
bool sieve[100007];
void sieve_make() {
  memset(sieve, true, sizeof(sieve));
  sieve[0] = sieve[1] = false;
  for (int i = 2; i * i <= 100006; i++) {
    if (sieve[i]) {
      for (int j = 2 * i; j <= 100006; j += i)
        sieve[j] = false;
    }
  }
}

int max(int a, int b) {
  if (a > b)
    return a;
  return b;
}
int min(int a, int b) {
  if (a < b)
    return a;
  return b;
}
int modexp(int a, int b, int c) {
  if (a == 0)
    return 0;
  if (b == 0)
    return 1;

  int ans;
  if (b % 2 == 0) {
    int small = modexp(a, b / 2, c);
    ans = (small * small) % c;
  } else {
    int small = modexp(a, b - 1, c);
    ans = (a % c);
    ans = (small * ans) % c;
  }
  return (ans + c) % c;
}

string rev(string s) {
  reverse(s.begin(), s.end());
  return s;
}
bool sign(int x) {
  if (x > 0)
    return true;
  return false;
}

int base3(string b) {
  int ans = 0, c = 0;
  for (int i = b.length() - 1; i >= 0; i--) {
    ans += (b[i] - '0') * pow(3, c++);
  }
  return ans;
}

int32_t main() {
  ios::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif

  int n, k;
  cin >> n >> k;
  int a[n];

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

  int dp[n];

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

  cout << dp[n - 1];
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define mod 1000000007
#define inf 1e18
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
using namespace std;
bool sieve[100007];
void sieve_make() {
  memset(sieve, true, sizeof(sieve));
  sieve[0] = sieve[1] = false;
  for (int i = 2; i * i <= 100006; i++) {
    if (sieve[i]) {
      for (int j = 2 * i; j <= 100006; j += i)
        sieve[j] = false;
    }
  }
}

int max(int a, int b) {
  if (a > b)
    return a;
  return b;
}
int min(int a, int b) {
  if (a < b)
    return a;
  return b;
}
int modexp(int a, int b, int c) {
  if (a == 0)
    return 0;
  if (b == 0)
    return 1;

  int ans;
  if (b % 2 == 0) {
    int small = modexp(a, b / 2, c);
    ans = (small * small) % c;
  } else {
    int small = modexp(a, b - 1, c);
    ans = (a % c);
    ans = (small * ans) % c;
  }
  return (ans + c) % c;
}

string rev(string s) {
  reverse(s.begin(), s.end());
  return s;
}
bool sign(int x) {
  if (x > 0)
    return true;
  return false;
}

int base3(string b) {
  int ans = 0, c = 0;
  for (int i = b.length() - 1; i >= 0; i--) {
    ans += (b[i] - '0') * pow(3, c++);
  }
  return ans;
}

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

  int n, k;
  cin >> n >> k;
  int a[n];

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

  int dp[n];

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

  cout << dp[n - 1];
}