Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define db double
#define pb push_back
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define all(a) a.begin(), a.end()
using namespace std;

ll mod = 1e9 + 7;
const ll M = 500006;
ll f[M], inv[M];
ll expo(ll base, ll exponent) {
  ll ans = 1;
  while (exponent != 0) {
    if (exponent & 1)
      ans = (1LL * ans * base) % mod;
    base = (1LL * base * base) % mod;
    exponent >>= 1;
  }
  return ans % mod;
}
void compute() {
  f[0] = 1;
  rep(i, 1, M - 1) { f[i] = (1LL * i * f[i - 1]) % mod; }
  inv[M - 1] = expo(f[M - 1], mod - 2);
  for (ll i = M - 2; i >= 0; i--) {
    inv[i] = (1LL * (i + 1) * inv[i + 1]) % mod;
  }
}
ll C(ll n, ll r) {
  return (1LL * ((1LL * f[n] * inv[r]) % mod) * inv[n - r]) % mod;
}

vector<bool> prime(1000002, true);
void Sieve() {

  for (int p = 2; p * p <= 1000001; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == true) {
      for (int i = p * p; i <= 1000001; i += p)
        prime[i] = false;
    }
  }
}

ll n, k;
char matrix[2000][2000];

string func(ll i, ll j) {
  string ans;
  ll x = i, y = j;
  while (x < n || y < n) {
    if (x == n - 1) {
      ans.pb(matrix[x][y + 1]);
      y++;
      continue;
    }
    if (y == n - 1) {
      ans.pb(matrix[x + 1][y]);
      x++;
      continue;
    }
    if (matrix[x + 1][y] < matrix[x][y + 1]) {
      ans.pb(matrix[x + 1][y]);
      x++;
    } else if (matrix[x + 1][y] > matrix[x][y + 1]) {
      ans.pb(matrix[x][y + 1]);
      y++;
    } else {
      ans.pb(matrix[x + 1][y]);
      string t = min(func(x + 1, y), func(x, y + 1));
      ans += t;
      return ans;
    }
  }
  return ans;
}

int main() {
  // #ifndef ONLINE_JUDGE
  //    // for getting input from input.txt
  // freopen("alchemy_input.txt", "r", stdin);
  //    // for writing output to output.txt
  // freopen("output.txt", "w", stdout);
  //    #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // compute();
  ll tests = 1, t = 1;
  // cin>>tests;
  while (tests--) {
    ll i, j, n, k;
    cin >> n >> k;
    vector<ll> h(n);
    rep(i, 0, n - 1) cin >> h[i];
    ll dp[n];
    dp[0] = 0;
    dp[1] = abs(h[1] - h[0]);
    for (i = 2; i < n; i++) {
      dp[i] = (dp[i - 1] + abs(h[i] - h[i - 1]));
      for (j = 2; j <= k; j++) {
        dp[i] = min(dp[i], dp[i - j] + abs(h[i - j] - h[i]));
      }
    }
    cout << dp[n - 1];
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define db double
#define pb push_back
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define all(a) a.begin(), a.end()
using namespace std;

ll mod = 1e9 + 7;
const ll M = 500006;
ll f[M], inv[M];
ll expo(ll base, ll exponent) {
  ll ans = 1;
  while (exponent != 0) {
    if (exponent & 1)
      ans = (1LL * ans * base) % mod;
    base = (1LL * base * base) % mod;
    exponent >>= 1;
  }
  return ans % mod;
}
void compute() {
  f[0] = 1;
  rep(i, 1, M - 1) { f[i] = (1LL * i * f[i - 1]) % mod; }
  inv[M - 1] = expo(f[M - 1], mod - 2);
  for (ll i = M - 2; i >= 0; i--) {
    inv[i] = (1LL * (i + 1) * inv[i + 1]) % mod;
  }
}
ll C(ll n, ll r) {
  return (1LL * ((1LL * f[n] * inv[r]) % mod) * inv[n - r]) % mod;
}

vector<bool> prime(1000002, true);
void Sieve() {

  for (int p = 2; p * p <= 1000001; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == true) {
      for (int i = p * p; i <= 1000001; i += p)
        prime[i] = false;
    }
  }
}

ll n, k;
char matrix[2000][2000];

string func(ll i, ll j) {
  string ans;
  ll x = i, y = j;
  while (x < n || y < n) {
    if (x == n - 1) {
      ans.pb(matrix[x][y + 1]);
      y++;
      continue;
    }
    if (y == n - 1) {
      ans.pb(matrix[x + 1][y]);
      x++;
      continue;
    }
    if (matrix[x + 1][y] < matrix[x][y + 1]) {
      ans.pb(matrix[x + 1][y]);
      x++;
    } else if (matrix[x + 1][y] > matrix[x][y + 1]) {
      ans.pb(matrix[x][y + 1]);
      y++;
    } else {
      ans.pb(matrix[x + 1][y]);
      string t = min(func(x + 1, y), func(x, y + 1));
      ans += t;
      return ans;
    }
  }
  return ans;
}

int main() {
  // #ifndef ONLINE_JUDGE
  //    // for getting input from input.txt
  // freopen("alchemy_input.txt", "r", stdin);
  //    // for writing output to output.txt
  // freopen("output.txt", "w", stdout);
  //    #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // compute();
  ll tests = 1, t = 1;
  // cin>>tests;
  while (tests--) {
    ll i, j, n, k;
    cin >> n >> k;
    vector<ll> h(n);
    rep(i, 0, n - 1) cin >> h[i];
    ll dp[n];
    dp[0] = 0;
    dp[1] = abs(h[1] - h[0]);
    for (i = 2; i < n; i++) {
      dp[i] = (dp[i - 1] + abs(h[i] - h[i - 1]));
      for (j = 2; j <= k; j++) {
        if (i - j < 0)
          break;
        dp[i] = min(dp[i], dp[i - j] + abs(h[i - j] - h[i]));
      }
    }
    cout << dp[n - 1];
  }

  return 0;
}