Problem p03161 (C++)

Buggy submission (status: Runtime Error):

//_Hala Madrid_//

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

typedef long long ll;
typedef double db;
typedef long double ldb;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define FOR(i, start, end) for (ll i = start; i <= end; i++)
#define FORD(i, start, end) for (ll i = start; i >= end; i--)
#define br cout << "\n"
#define test(t)                                                                \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define rz return 0
#define all(s) s.begin(), s.end()
#define sz size()
#define pb push_back
#define F first
#define S second

const int mod = 1e9 + 7;

void solve() {
  bool flag = 0, flag1 = 0;
  int m, n, k, l, r, sum = 0, ans = 0, pos, pos1, c1 = 0, c2 = 0, c3 = 0,
                     c4 = 0, x, y, z, cnt = 0, mx = 0, mn = INT_MAX;
  string s, t, s1, s2;
  string str = "";
  vi v;
  cin >> n >> m;
  int h[n];
  vector<ll> dp(n + 5, INT_MAX);
  FOR(i, 0, n - 1)
  cin >> h[i];
  dp[0] = 0;
  FOR(i, 0, n - 1) {
    FOR(k, 1, m) { dp[i + k] = min(dp[i + k], dp[i] + abs(h[i + k] - h[i])); }
  }
  cout << dp[n - 1];
}
int main() {
  fio bool notc = 0;
  int t = 1;
  if (notc) {
    cin >> t;
  }
  while (t--) {

#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    solve();
  }
  rz;
}

Fixed submission:

//_Hala Madrid_//

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

typedef long long ll;
typedef double db;
typedef long double ldb;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define FOR(i, start, end) for (ll i = start; i <= end; i++)
#define FORD(i, start, end) for (ll i = start; i >= end; i--)
#define br cout << "\n"
#define test(t)                                                                \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define rz return 0
#define all(s) s.begin(), s.end()
#define sz size()
#define pb push_back
#define F first
#define S second

const int mod = 1e9 + 7;

void solve() {
  bool flag = 0, flag1 = 0;
  int m, n, k, l, r, sum = 0, ans = 0, pos, pos1, c1 = 0, c2 = 0, c3 = 0,
                     c4 = 0, x, y, z, cnt = 0, mx = 0, mn = INT_MAX;
  string s, t, s1, s2;
  string str = "";
  vi v;
  cin >> n >> m;
  int h[n];
  vector<ll> dp(n + m + 5, INT_MAX);
  FOR(i, 0, n - 1)
  cin >> h[i];
  dp[0] = 0;
  FOR(i, 0, n - 1) {
    FOR(k, 1, m) { dp[i + k] = min(dp[i + k], dp[i] + abs(h[i + k] - h[i])); }
  }
  cout << dp[n - 1];
}
int main() {
  fio bool notc = 0;
  int t = 1;
  if (notc) {
    cin >> t;
  }
  while (t--) {

#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    solve();
  }
  rz;
}