Problem p03161 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double dbl;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<long long> vl;
typedef vector<pair<int, int>> vii;
#define mp make_pair
#define pb push_back
#define invec(a, n)                                                            \
  for (ll i = 0; i < n; i++) {                                                 \
    cin >> a[i];                                                               \
  }
#define rep(n) for (ll i = 0; i < n; i++)
#define fr(i, j, k) for (ll i = j; i < k; i++)
#define FR(i, j, k) for (ll i = j; i >= 0; i--)
#define MOD 1000000007
#define all(v) v.begin(), v.end()
#define INF 1000000
#define xx first
#define yy second
#define pl cout << endl;
template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p) {
  return os << "(" << p.first << "," << p.second << ")";
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  typename vector<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "}";
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "[";
  typename set<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}
template <typename F, typename S>
ostream &operator<<(ostream &os, const map<F, S> &v) {
  os << "[";
  typename map<F, S>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}
#define deb(x) cout << #x << " = " << x << endl;

#define er(args...)                                                            \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

int main() {
  ll n, k;
  cin >> n >> k;
  vl v(n);
  invec(v, n);
  vl dp(n);
  fill(all(dp), LLONG_MAX);
  dp[0] = 0;
  dp[1] = abs(v[1] - v[0]);

  for (ll i = 2; i < n; i++) {
    for (ll j = 1; j <= k; j++) {
      if (i - j < 0) {
        break;
      }
      dp[i] = min(dp[i], abs(v[i] - v[i - k]) + dp[i - k]);
    }
    // dp[i]=min(abs(v[i]-v[i-2])+dp[i-2],abs(v[i]-v[i-1])+dp[i-1]);
  }
  cout << dp[n - 1] << endl;
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double dbl;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<long long> vl;
typedef vector<pair<int, int>> vii;
#define mp make_pair
#define pb push_back
#define invec(a, n)                                                            \
  for (ll i = 0; i < n; i++) {                                                 \
    cin >> a[i];                                                               \
  }
#define rep(n) for (ll i = 0; i < n; i++)
#define fr(i, j, k) for (ll i = j; i < k; i++)
#define FR(i, j, k) for (ll i = j; i >= 0; i--)
#define MOD 1000000007
#define all(v) v.begin(), v.end()
#define INF 1000000
#define xx first
#define yy second
#define pl cout << endl;
template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p) {
  return os << "(" << p.first << "," << p.second << ")";
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  typename vector<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "}";
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "[";
  typename set<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}
template <typename F, typename S>
ostream &operator<<(ostream &os, const map<F, S> &v) {
  os << "[";
  typename map<F, S>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}
#define deb(x) cout << #x << " = " << x << endl;

#define er(args...)                                                            \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

int main() {
  ll n, k;
  cin >> n >> k;
  vl v(n);
  invec(v, n);
  vl dp(n);
  fill(all(dp), LLONG_MAX);
  dp[0] = 0;
  dp[1] = abs(v[1] - v[0]);

  for (ll i = 2; i < n; i++) {
    for (ll j = 1; j <= k; j++) {
      if (i - j < 0) {
        break;
      }
      dp[i] = min(dp[i], abs(v[i] - v[i - j]) + dp[i - j]);
    }
    // dp[i]=min(abs(v[i]-v[i-2])+dp[i-2],abs(v[i]-v[i-1])+dp[i-1]);
  }
  cout << dp[n - 1] << endl;
}