Problem p03161 (C++)

Buggy submission (status: Runtime Error):
// aarsh01rsh@gmail.com
// Aarsh Sharma
#include <bits/stdc++.h>
using namespace std;

string to_string(string s) { return '"' + s + '"'; }
string to_string(char ch) {
  string s(1, ch);
  return '\'' + s + '\'';
}
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifndef ONLINE_JUDGE
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define f(i, x, n) for (int i = x; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define fast_io()                                                              \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

typedef long long int ll;
#define int ll
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> matrix;
typedef vector<ll> vll;

const ll mod = 1e9 + 7;
const ll inf = LLONG_MAX;
const ll N = 1e5 + 10;

int32_t main() {
  fast_io();

  int n, k;
  cin >> n >> k;
  vll a(n);
  f(i, 0, n) cin >> a[i];
  if (n == 2) {
    cout << abs(a[n - 1] - a[0]);
    return 0;
  }
  vll dp(n);
  dp[0] = 0;
  // dp[1] = abs(a[1] - a[0]);
  // f (i, 2, k) {
  //   int mini = inf;
  //   f (j, 1, i+1) {
  //     if (abs(a[i]-a[i-j]) < mini) {
  //       mini = abs(a[i]-a[i-j]);
  //       dp[i] = dp[i-j] + abs(a[i]-a[i-j]);
  //     }
  //   }
  //   dp[i] = mini;
  // }
  f(i, 1, n) {
    int mini = inf;
    for (int j = 1; j <= k; j++) {
      if (abs(a[i] - a[i - j]) + dp[i - j] < mini) {
        mini = abs(a[i] - a[i - j]) + dp[i - j];
      }
    }
    dp[i] = mini;
  }
  debug(dp);
  cout << dp[n - 1];

  return 0;
}

Fixed submission:
// aarsh01rsh@gmail.com
// Aarsh Sharma
#include <bits/stdc++.h>
using namespace std;

string to_string(string s) { return '"' + s + '"'; }
string to_string(char ch) {
  string s(1, ch);
  return '\'' + s + '\'';
}
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifndef ONLINE_JUDGE
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define f(i, x, n) for (int i = x; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define fast_io()                                                              \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

typedef long long int ll;
#define int ll
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> matrix;
typedef vector<ll> vll;

const ll mod = 1e9 + 7;
const ll inf = LLONG_MAX;
const ll N = 1e5 + 10;

int32_t main() {
  fast_io();

  int n, k;
  cin >> n >> k;
  vll a(n);
  f(i, 0, n) cin >> a[i];
  if (n == 2) {
    cout << abs(a[n - 1] - a[0]);
    return 0;
  }
  vll dp(n);
  dp[0] = 0;
  // dp[1] = abs(a[1] - a[0]);
  // f (i, 2, k) {
  //   int mini = inf;
  //   f (j, 1, i+1) {
  //     if (abs(a[i]-a[i-j]) < mini) {
  //       mini = abs(a[i]-a[i-j]);
  //       dp[i] = dp[i-j] + abs(a[i]-a[i-j]);
  //     }
  //   }
  //   dp[i] = mini;
  // }
  f(i, 1, n) {
    int mini = inf;
    for (int j = 1; j <= min(i, k); j++) {
      if (abs(a[i] - a[i - j]) + dp[i - j] < mini) {
        mini = abs(a[i] - a[i - j]) + dp[i - j];
      }
    }
    dp[i] = mini;
  }
  debug(dp);
  cout << dp[n - 1];

  return 0;
}