Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
// بسم الله الرحمن الرحيم
// اللہ کے نام سے شروع جو بڑا مہربان اور نہایت رحم والا ہے
// Allah in the name of The Most Affectionate, the Merciful
// अल्लाह के नाम से जो दयालु और कृपाशील है
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;
#define fastio                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0)
#define ll long long
#define ull unsigned ll
#define pb push_back

#define endl '\n'
#define mod 1000000007

#define FOR(i, k, n) for (int i = k; i < n; i++)
#define ROF(i, k, n) for (int i = k; i >= n; i--)
#define MEMSET(t) memset(t, 0, sizeof(t));
#define time clock_t tStart
#define Start clock()
#define Time                                                                   \
  printf("Time taken: %.3fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC)

ll minC(int ar[], int n, int k, ll dp[], int i) {
  if (i >= n)
    return 0;
  if (i == n - 1)
    return 0;
  if (dp[i] != -1)
    return dp[i];

  int t = n;
  ll mini = INT_MAX;
  for (int j = i + 1; j <= i + k && j < n; j++) {
    mini = min(abs(ar[i] - ar[j]) + minC(ar, n, k, dp, j), mini);
    //  cout<<mini<<" "<<i<<" "<< j<<endl;
  }
  dp[n] = mini;
  return dp[n];
  // return 0;
}

int main() {
  fastio;
  int t = 1;
  while (t--) {
    int n, k;
    cin >> n >> k;

    int ar[n];
    ll dp[n + 1];
    FOR(i, 0, n) cin >> ar[i];
    memset(dp, -1, sizeof(dp));
    cout << minC(ar, n, k, dp, 0);
  }
  return 0;
}

Fixed submission:
// بسم الله الرحمن الرحيم
// اللہ کے نام سے شروع جو بڑا مہربان اور نہایت رحم والا ہے
// Allah in the name of The Most Affectionate, the Merciful
// अल्लाह के नाम से जो दयालु और कृपाशील है
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;
#define fastio                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0)
#define ll long long
#define ull unsigned ll
#define pb push_back

#define endl '\n'
#define mod 1000000007

#define FOR(i, k, n) for (int i = k; i < n; i++)
#define ROF(i, k, n) for (int i = k; i >= n; i--)
#define MEMSET(t) memset(t, 0, sizeof(t));
#define time clock_t tStart
#define Start clock()
#define Time                                                                   \
  printf("Time taken: %.3fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC)

ll minC(int ar[], int n, int k, ll dp[], int i) {
  if (i >= n)
    return 0;
  if (i == n - 1)
    return 0;
  if (dp[i] != -1)
    return dp[i];

  int t = n;
  ll mini = INT_MAX;
  for (int j = i + 1; j <= i + k && j < n; j++) {
    mini = min(abs(ar[i] - ar[j]) + minC(ar, n, k, dp, j), mini);
    //  cout<<mini<<" "<<i<<" "<< j<<endl;
  }
  dp[i] = mini;
  return dp[i];
  // return 0;
}

int main() {
  fastio;
  int t = 1;
  while (t--) {
    int n, k;
    cin >> n >> k;

    int ar[n];
    ll dp[n + 1];
    FOR(i, 0, n) cin >> ar[i];
    memset(dp, -1, sizeof(dp));
    cout << minC(ar, n, k, dp, 0);
  }
  return 0;
}