Problem p03161 (C++)

Buggy submission (status: Runtime Error):
// motatoes cp snippet
#include <bits/stdc++.h>

using namespace std;

#define endl "\n"
#define ll long long

#define forr(a, b, v) for (int v = a; v <= b; v++)
#define ford(a, b, v) for (int v = a; v >= b; v--)

// https://www.topcoder.com/community/competitive-programming/tutorials/power-up-c-with-the-standard-template-library-part-1/#map
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef pair<int, int> ii;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (typeof((c)).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())

string to_string(string s) { 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;
}

// get default value from map type
template <class K, class V> V get(map<K, V> m, K k, V def) {
  if (m.find(k) != m.end()) {
    return m[k];
  } else {
    return def;
  }
}

// <geometry>
const double PI = acos(-1.0);

double toDegreeFromMinutes(double minutes) { return (minutes / 60); }

double toRadians(double degree) { return (degree * PI / 180.0); }

double toDegree(double radian) {
  if (radian < 0)
    radian += 2 * PI;
  return (radian * 180 / PI);
}

double fixAngle(double A) { return A > 1 ? 1 : (A < -1 ? -1 : A); }

// sin(A)/a = sin(B)/b = sin(C)/c
double getSide_a_bAB(double b, double A, double B) {
  return (sin(A) * b) / sin(B);
}

double getAngle_A_abB(double a, double b, double B) {
  return asin(fixAngle((a * sin(B)) / b));
}

// a^2 = b^2 + c^2 - 2*b*c*cos(A)
double getAngle_A_abc(double a, double b, double c) {
  return acos(fixAngle((b * b * c * c - a * a) / (2 * b * c)));
}
// </geometry>

#define INF 1e9 + 5

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  int k;

  cin >> n >> k;

  int h[n + k + 5];

  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  vector<int> dp(n + k, INF);
  dp[1] = 0;

  for (int i = 1; i < n + 1; i++) {
    for (int kk = 1; kk <= k; kk++) {
      // cout << dp[i] << " " << abs(h[i] - h[i+kk]) << endl;
      dp[i + kk] = min(dp[i + kk], dp[i] + abs(h[i] - h[i + kk]));
    }
  }
  // cout << to_string(dp) << endl;
  cout << dp[n];

  return 0;
}

Fixed submission:
// motatoes cp snippet
#include <bits/stdc++.h>

using namespace std;

#define endl "\n"
#define ll long long

#define forr(a, b, v) for (int v = a; v <= b; v++)
#define ford(a, b, v) for (int v = a; v >= b; v--)

// https://www.topcoder.com/community/competitive-programming/tutorials/power-up-c-with-the-standard-template-library-part-1/#map
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef pair<int, int> ii;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (typeof((c)).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())

string to_string(string s) { 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;
}

// get default value from map type
template <class K, class V> V get(map<K, V> m, K k, V def) {
  if (m.find(k) != m.end()) {
    return m[k];
  } else {
    return def;
  }
}

// <geometry>
const double PI = acos(-1.0);

double toDegreeFromMinutes(double minutes) { return (minutes / 60); }

double toRadians(double degree) { return (degree * PI / 180.0); }

double toDegree(double radian) {
  if (radian < 0)
    radian += 2 * PI;
  return (radian * 180 / PI);
}

double fixAngle(double A) { return A > 1 ? 1 : (A < -1 ? -1 : A); }

// sin(A)/a = sin(B)/b = sin(C)/c
double getSide_a_bAB(double b, double A, double B) {
  return (sin(A) * b) / sin(B);
}

double getAngle_A_abB(double a, double b, double B) {
  return asin(fixAngle((a * sin(B)) / b));
}

// a^2 = b^2 + c^2 - 2*b*c*cos(A)
double getAngle_A_abc(double a, double b, double c) {
  return acos(fixAngle((b * b * c * c - a * a) / (2 * b * c)));
}
// </geometry>

#define INF 1e9 + 5

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  int k;

  cin >> n >> k;

  int h[n + k + 5];

  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  vector<ll> dp(n + k + 5, INF);
  dp[1] = 0;

  for (int i = 1; i < n + 1; i++) {
    for (int kk = 1; kk <= k; kk++) {
      // cout << dp[i] << " " << abs(h[i] - h[i+kk]) << endl;
      dp[i + kk] = min(dp[i + kk], dp[i] + abs(h[i] - h[i + kk]));
    }
  }
  // cout << to_string(dp) << endl;
  cout << dp[n];

  return 0;
}