Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
#define watch5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define watch4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl;
#define watch3(a, b, c)                                                        \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << endl;
#define printclock                                                             \
  cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\n";
#define sharingan cerr << "\n-------------------------------------------\n\n";
#define watch2(a, b)                                                           \
  cerr << #a << ": " << a << " | " << #b << ": " << b << endl;
#define Yup                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define watch(a) cerr << #a << ": " << a << endl;
#define __gcd(a, b) __algo_gcd(a, b)
#define PI 3.14159265358979323846
#define mod 1000000007
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define endl "\n"
#define int long long

int k;
int dp[100001];

int go(int i, vector<int> &v) {
  int n = v.size();
  if (i == n - 1)
    return 0;
  int ans = LLONG_MAX;
  for (int j = i + 1; j <= n - 1 && j <= i + k; j++) {
    ans = min(ans, go(j, v) + abs(v[i] - v[j]));
  }
  return dp[i] = ans;
}

int32_t main() {
  Yup;
  int n;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  memset(dp, -1, sizeof(dp));
  cout << go(0, v);
}

Fixed submission:
#define watch5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define watch4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl;
#define watch3(a, b, c)                                                        \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << endl;
#define printclock                                                             \
  cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\n";
#define sharingan cerr << "\n-------------------------------------------\n\n";
#define watch2(a, b)                                                           \
  cerr << #a << ": " << a << " | " << #b << ": " << b << endl;
#define Yup                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define watch(a) cerr << #a << ": " << a << endl;
#define __gcd(a, b) __algo_gcd(a, b)
#define PI 3.14159265358979323846
#define mod 1000000007
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define endl "\n"
#define int long long

int k;
int dp[100001];

int go(int i, vector<int> &v) {
  int n = v.size();
  if (i == n - 1)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  int ans = LLONG_MAX;
  for (int j = i + 1; j <= n - 1 && j <= i + k; j++) {
    ans = min(ans, go(j, v) + abs(v[i] - v[j]));
  }
  return dp[i] = ans;
}

int32_t main() {
  Yup;
  int n;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  memset(dp, -1, sizeof(dp));
  cout << go(0, v);
}