Problem p03161 (C++)

Buggy submission (status: Time Limit Exceeded):


//   /$$$$           /$$                 /$$$$  /$$$$$$            /$$
//  | $$_/          | $$                |_  $$ /$$__  $$          | $$
//  | $$    /$$$$$$$| $$$$$$$   /$$$$$$   | $$|__/  \ $$ /$$$$$$$ | $$   /$$
//  | $$   /$$_____/| $$__  $$ |____  $$  | $$  /$$$$$$/| $$__  $$| $$  /$$/
//  | $$  |  $$$$$$ | $$  \ $$  /$$$$$$$  | $$ /$$____/ | $$  \ $$| $$$$$$/
//  | $$   \____  $$| $$  | $$ /$$__  $$  | $$| $$      | $$  | $$| $$_  $$
//  | $$$$ /$$$$$$$/| $$  | $$|  $$$$$$$ /$$$$| $$$$$$$$| $$  | $$| $$ \  $$
//  |____/|_______/ |__/  |__/ \_______/|____/|________/|__/  |__/|__/  \__/
//
//
//

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define ull unsigned long long int
#define po2(z) 1 << (ll)z
#define fr(i, x, n) for (ll i = (ll)(x); i < (ll)(n); ++i)
#define rf(i, x, n) for (ll i = (ll)(x); i >= (ll)(n); --i)
#define modinv(x) powah(x, mod - 2)
#define ncr(n, r)                                                              \
  (((fact[n] * modinv(fact[n - r])) % mod * modinv(fact[r])) % mod)
#define factorial                                                              \
  fact.pb(1);                                                                  \
  sidha(i, 1, MAX) fact.pb((fact[i - 1] * i) % mod)
#define tej                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define mod (ll)1000000007
#define MAX 100005
#define Vii vector<ll>
#define pb push_back
#define pii pair<ll, ll>
#define ff first
#define ss second
#define vpii vector<pii>
#define endl '\n'

//--------------------------------------------------//
void solve() {
  ll n, k;
  cin >> n >> k;
  vector<ll> h(n);

  for (ll i = 0; i < n; i++) {
    cin >> h[i];
  }
  ll dp[n];

  for (ll i = 0; i < n; i++)
    dp[i] = 1e10;

  dp[0] = 0;

  for (ll i = 0; i < n; i++) {
    for (ll j = 1; j <= min(i + k, n - 1); j++) {

      dp[j] = min(dp[j], dp[i] + abs(h[j] - h[i]));
    }
  }
  cout << dp[n - 1];
}

//--------------------------------------------------//

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

  int tc = 1;
  //	cin >> tc;
  for (int t = 1; t <= tc; t++) {
    // cout << "Case #" << t  << ": ";
    solve();
  }
}


Fixed submission:


//   /$$$$           /$$                 /$$$$  /$$$$$$            /$$
//  | $$_/          | $$                |_  $$ /$$__  $$          | $$
//  | $$    /$$$$$$$| $$$$$$$   /$$$$$$   | $$|__/  \ $$ /$$$$$$$ | $$   /$$
//  | $$   /$$_____/| $$__  $$ |____  $$  | $$  /$$$$$$/| $$__  $$| $$  /$$/
//  | $$  |  $$$$$$ | $$  \ $$  /$$$$$$$  | $$ /$$____/ | $$  \ $$| $$$$$$/
//  | $$   \____  $$| $$  | $$ /$$__  $$  | $$| $$      | $$  | $$| $$_  $$
//  | $$$$ /$$$$$$$/| $$  | $$|  $$$$$$$ /$$$$| $$$$$$$$| $$  | $$| $$ \  $$
//  |____/|_______/ |__/  |__/ \_______/|____/|________/|__/  |__/|__/  \__/
//
//
//

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define ull unsigned long long int
#define po2(z) 1 << (ll)z
#define fr(i, x, n) for (ll i = (ll)(x); i < (ll)(n); ++i)
#define rf(i, x, n) for (ll i = (ll)(x); i >= (ll)(n); --i)
#define modinv(x) powah(x, mod - 2)
#define ncr(n, r)                                                              \
  (((fact[n] * modinv(fact[n - r])) % mod * modinv(fact[r])) % mod)
#define factorial                                                              \
  fact.pb(1);                                                                  \
  sidha(i, 1, MAX) fact.pb((fact[i - 1] * i) % mod)
#define tej                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define mod (ll)1000000007
#define MAX 100005
#define Vii vector<ll>
#define pb push_back
#define pii pair<ll, ll>
#define ff first
#define ss second
#define vpii vector<pii>
#define endl '\n'

//--------------------------------------------------//
void solve() {
  ll n, k;
  cin >> n >> k;
  vector<ll> h(n);

  for (ll i = 0; i < n; i++) {
    cin >> h[i];
  }
  ll dp[n];

  for (ll i = 0; i < n; i++)
    dp[i] = 1e10;

  dp[0] = 0;

  for (ll i = 0; i < n; i++) {
    for (ll j = i + 1; j <= i + k; j++) {
      if (j < n) {
        dp[j] = min(dp[j], dp[i] + abs(h[j] - h[i]));
      }
    }
  }
  cout << dp[n - 1];
}

//--------------------------------------------------//

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

  int tc = 1;
  //	cin >> tc;
  for (int t = 1; t <= tc; t++) {
    // cout << "Case #" << t  << ": ";
    solve();
  }
}