Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define vi vector<ll>
#define vb vector<bool>
#define pii pair<ll, ll>
#define mod 1000000007
#define pll pair<ll, ll>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define per1(i, n) for (ll i = n; i > 0; i--)
#define prll(a, n)                                                             \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << a[i] << " ";                                                       \
  }
#define ain(a)                                                                 \
  for (auto &i : a)                                                            \
  cin >> i
#define mms(a) memset(a, 0, sizeof(a))
// cout << fixed << setprecision(0) << pi <<" "<<npi<<endl;

ll max_ll(ll a, ll b, ll c) { return max(a, max(b, c)); }

ll min_ll(ll a, ll b, ll c) { return min(a, min(b, c)); }

bool sortBySec(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.second == b.second)
    return a.first < b.first;
  return (a.second < b.second);
}

long long binexpmod(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) {
      res = res * a % mod;
    }
    a = a * a % mod;
    b >>= 1;
  }
  return (res);
}

ll lcm(ll a, ll b) {
  ll x = __gcd(a, b);
  a /= x;
  b /= x;
  ll sum = a * b * x;
  return sum;
}
void solve() {
  ll n, k, res = 0;
  cin >> n >> k;
  vi a(n);
  ain(a);
  ll dp1[n];
  rep(i, n) dp1[i] = 1000000000;
  rep(i, k) { dp1[i] = abs(a[i] - a[0]); }
  for (ll i = 2; i < n; i++) {
    rep1(j, k) { dp1[i] = min(dp1[i], dp1[i - j] + abs(a[i] - a[i - j])); }
  }
  cout << dp1[n - 1] << endl;
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define vi vector<ll>
#define vb vector<bool>
#define pii pair<ll, ll>
#define mod 1000000007
#define pll pair<ll, ll>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define per1(i, n) for (ll i = n; i > 0; i--)
#define prll(a, n)                                                             \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << a[i] << " ";                                                       \
  }
#define ain(a)                                                                 \
  for (auto &i : a)                                                            \
  cin >> i
#define mms(a) memset(a, 0, sizeof(a))
// cout << fixed << setprecision(0) << pi <<" "<<npi<<endl;

ll max_ll(ll a, ll b, ll c) { return max(a, max(b, c)); }

ll min_ll(ll a, ll b, ll c) { return min(a, min(b, c)); }

bool sortBySec(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.second == b.second)
    return a.first < b.first;
  return (a.second < b.second);
}

long long binexpmod(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) {
      res = res * a % mod;
    }
    a = a * a % mod;
    b >>= 1;
  }
  return (res);
}

ll lcm(ll a, ll b) {
  ll x = __gcd(a, b);
  a /= x;
  b /= x;
  ll sum = a * b * x;
  return sum;
}
void solve() {
  ll n, k, res = 0;
  cin >> n >> k;
  vi a(n);
  ain(a);
  ll dp1[n];
  rep(i, n) dp1[i] = 1000000000;
  dp1[0] = 0;
  for (ll i = 1; i < n; i++) {
    rep1(j, min(k, i)) {
      dp1[i] = min(dp1[i], dp1[i - j] + abs(a[i] - a[i - j]));
    }
  }
  cout << dp1[n - 1] << endl;
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}