Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i, n) for (long long i = 0; i < n; ++i)
#define REPR(i, n) for (long long i = n; i >= 0; --i)
#define REPI(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define REPIR(itr, v) for (auto itr = v.rbegin(); itr != v.rend(); ++itr)
#define FOR(i, a, b) for (long long i = a; i < b; ++i)
#define SORT(v, n) sort(v, v + n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(), v.end()
#define llong long long
#define INF 999999999
#define SUR 1000000007
#define pb push_back
#define pf push_front
#define MP make_pair
#define SV(v)                                                                  \
  {                                                                            \
    for (long long sitr = 0; sitr < v.size(); ++sitr) {                        \
      cin >> v[sitr];                                                          \
    }                                                                          \
  }

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

using namespace std;

typedef pair<int, int> pii;

int main() {

  int n, K;
  cin >> n >> K;

  vector<llong> h(n);
  REP(i, n) { cin >> h[i]; }

  llong dp[100001] = {};
  REP(i, 100001) { dp[i] = INF; }

  dp[0] = 0;
  REP(i, n) {
    FOR(k, 1, K + 1)
    dp[i + k] = min(dp[i + k], dp[i] + abs(h[i] - h[i + k]));
  }

  cout << dp[n - 1] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define REP(i, n) for (long long i = 0; i < n; ++i)
#define REPR(i, n) for (long long i = n; i >= 0; --i)
#define REPI(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define REPIR(itr, v) for (auto itr = v.rbegin(); itr != v.rend(); ++itr)
#define FOR(i, a, b) for (long long i = a; i < b; ++i)
#define SORT(v, n) sort(v, v + n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(), v.end()
#define llong long long
#define INF 999999999
#define SUR 1000000007
#define pb push_back
#define pf push_front
#define MP make_pair
#define SV(v)                                                                  \
  {                                                                            \
    for (long long sitr = 0; sitr < v.size(); ++sitr) {                        \
      cin >> v[sitr];                                                          \
    }                                                                          \
  }

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

using namespace std;

typedef pair<int, int> pii;

int main() {

  int n, K;
  cin >> n >> K;

  vector<llong> h(n);
  REP(i, n) { cin >> h[i]; }

  llong dp[100001] = {};
  REP(i, 100001) { dp[i] = INF; }

  dp[0] = 0;
  REP(i, n) {
    FOR(k, 1, K + 1)
    if (i + k < n)
      dp[i + k] = min(dp[i + k], dp[i] + abs(h[i] - h[i + k]));
  }

  cout << dp[n - 1] << endl;
  return 0;
}