Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<ll>

#define sz(x) ((int)(x).size())
#define pll pair<long long, long long>
#define mp make_pair
#define F first
#define S second
#define T                                                                      \
  int tt;                                                                      \
  cin >> tt;                                                                   \
  while (tt--)
#define fastio ios_base::sync_with_stdio(false);

#define endl '\n'
#define all(a) (a).begin(), (a).end()
#define display(x)                                                             \
  for (auto i : x)                                                             \
    cout << i << " ";                                                          \
  cout << '\n';
#define input(x)                                                               \
  for (int i = 0; i < n; i++)                                                  \
    cin >> x[i];
#define make(x, y) fill(all(x), y)
#define FO(i, startLoop, n) for (int i = startLoop; i < n; i++)

#define mod 1000000007
#define Y cout << "YES" << endl;
#define N cout << "NO" << endl;
#define ITFO(myiterator, mymap)                                                \
  for (auto myiterator = mymap.begin(); myiterator != mymap.end(); ++myiterator)
using namespace std;

int main() {
  fastio cin.tie(NULL);
  cout.tie(0);

  int n, k;
  cin >> n >> k;
  vi a(n, 0), dp(n, INT_MAX);
  input(a);
  dp[0] = 0;
  if (k >= n) {
    ll ans = INT_MAX;
    for (int i = n - 2; i >= 0; ++i)
      ans = min(ans, abs(a[i] - a[n - 1]));
    cout << ans << endl;
    return 0;
  }

  for (int i = 0; i < k; ++i)
    dp[i] = abs(a[i] - a[0]);

  for (int i = k; i < n; ++i) {
    ll cnt = k, mm = INT_MAX, j = i - 1;
    while (cnt-- && j >= 0) {
      mm = min(mm, abs(a[i] - a[j]) + dp[j]);
      j--;
    }
    dp[i] = mm;
  }
  cout << dp[n - 1];
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<ll>

#define sz(x) ((int)(x).size())
#define pll pair<long long, long long>
#define mp make_pair
#define F first
#define S second
#define T                                                                      \
  int tt;                                                                      \
  cin >> tt;                                                                   \
  while (tt--)
#define fastio ios_base::sync_with_stdio(false);

#define endl '\n'
#define all(a) (a).begin(), (a).end()
#define display(x)                                                             \
  for (auto i : x)                                                             \
    cout << i << " ";                                                          \
  cout << '\n';
#define input(x)                                                               \
  for (int i = 0; i < n; i++)                                                  \
    cin >> x[i];
#define make(x, y) fill(all(x), y)
#define FO(i, startLoop, n) for (int i = startLoop; i < n; i++)

#define mod 1000000007
#define Y cout << "YES" << endl;
#define N cout << "NO" << endl;
#define ITFO(myiterator, mymap)                                                \
  for (auto myiterator = mymap.begin(); myiterator != mymap.end(); ++myiterator)
using namespace std;

int main() {
  fastio cin.tie(NULL);
  cout.tie(0);

  int n, k;
  cin >> n >> k;
  vi a(n, 0), dp(n, INT_MAX);
  input(a);
  dp[0] = 0;
  if (k >= n) {
    ll ans = INT_MAX;
    for (int i = n - 2; i >= 0; --i)
      ans = min(ans, abs(a[i] - a[n - 1]));
    cout << ans << endl;
    return 0;
  }

  for (int i = 0; i < k; ++i)
    dp[i] = abs(a[i] - a[0]);

  for (int i = k; i < n; ++i) {
    ll cnt = k, mm = INT_MAX, j = i - 1;
    while (cnt-- && j >= 0) {
      mm = min(mm, abs(a[i] - a[j]) + dp[j]);
      j--;
    }
    dp[i] = mm;
  }
  cout << dp[n - 1];
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}