Problem p03161 (C++)

Buggy submission (status: Runtime Error):
/*
...........__________
| ___  __
.............|       |
| |   /
.............|       |
| |  /
.............|       |                               | __                | /
_______
.............|_______|  _______            _______   |______   ________   /|  /
|               |/       |      |
.............| \               | |      | |          |      |          | / | /
|               |\       |
.............|  \       _______| |      | |_______   |      |   _______|   |/ |
| \      |
.............|   \      |      | |      |         |  |      |  |       |   | |
|  \     |
.........  __|    \__|  |______| |______| ________|  |      |_ |_______|   |
|__|           _|   \__ _|        .

*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long int
#define ld long double
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vvi vector<vector<int>>
#define pf push_front
#define popb pop_back
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define prec(x) fixed << setprecision(15) << x
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mset(a, b) memset(a, b, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
#define fo0(i, n) for (int i = 0, i##end = n; i < i##end; i++)
#define fo1(i, n) for (int i = 1, i##end = n; i <= i##end; i++)
#define fo(i, a, b) for (int i = a, i##end = b; i <= i##end; i++)
#define foe(i, x) for (__typeof((x).end()) i = (x).begin(); i != (x).end(); ++i)
#define fre(i, x)                                                              \
  for (__typeof((x).rend()) i = (x).rbegin(); i != (x).rend(); ++i)
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define inf 1e18
const int mod = 1e9 + 7;
const int N = 4e5 + 10;

void raushan_kumar() {
  int n, k;
  cin >> n >> k;
  int a[n + 1];
  fo1(i, n) { cin >> a[i]; }
  int dp[n + 1];
  for (int i = 1; i <= n; i++) {
    dp[i] = INT_MAX;
  }
  dp[1] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      dp[i + j] = min(dp[i + j], dp[i] + abs(a[i] - a[i + j]));
    }
  }
  cout << dp[n];
}
int32_t main() {

  fast;
  int t = 1;
  // cin>>t;
  while (t--) {
    raushan_kumar();
  }
}

Fixed submission:
/*
...........__________
| ___  __
.............|       |
| |   /
.............|       |
| |  /
.............|       |                               | __                | /
_______
.............|_______|  _______            _______   |______   ________   /|  /
|               |/       |      |
.............| \               | |      | |          |      |          | / | /
|               |\       |
.............|  \       _______| |      | |_______   |      |   _______|   |/ |
| \      |
.............|   \      |      | |      |         |  |      |  |       |   | |
|  \     |
.........  __|    \__|  |______| |______| ________|  |      |_ |_______|   |
|__|           _|   \__ _|        .

*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long int
#define ld long double
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vvi vector<vector<int>>
#define pf push_front
#define popb pop_back
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define prec(x) fixed << setprecision(15) << x
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mset(a, b) memset(a, b, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
#define fo0(i, n) for (int i = 0, i##end = n; i < i##end; i++)
#define fo1(i, n) for (int i = 1, i##end = n; i <= i##end; i++)
#define fo(i, a, b) for (int i = a, i##end = b; i <= i##end; i++)
#define foe(i, x) for (__typeof((x).end()) i = (x).begin(); i != (x).end(); ++i)
#define fre(i, x)                                                              \
  for (__typeof((x).rend()) i = (x).rbegin(); i != (x).rend(); ++i)
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define inf 1e18
const int mod = 1e9 + 7;
const int N = 4e5 + 10;

void raushan_kumar() {
  int n, k;
  cin >> n >> k;
  int a[n + 1];
  fo1(i, n) { cin >> a[i]; }
  int dp[n + 1];
  for (int i = 1; i <= n; i++) {
    dp[i] = INT_MAX;
  }
  dp[1] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= k and i + j <= n; j++) {
      dp[i + j] = min(dp[i + j], dp[i] + abs(a[i] - a[i + j]));
    }
  }
  cout << dp[n];
}
int32_t main() {

  fast;
  int t = 1;
  // cin>>t;
  while (t--) {
    raushan_kumar();
  }
}