Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define rep(i, a, b) for (i = a; i < b; i++)
#define pb push_back
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define CLR(a) memset((a), 0, sizeof(a))
#define mp make_pair
#define hell 1000000007

using namespace std;
// int g[105][105];
// vector < int > wt(100004);
// vector < int > a;
// vector < int > v(30500);
// int cst[102][100005];
// int ansr[100005];
// ll int sum,x=0,c=0,n,m,flag=0,a1,a2,flag1=0,flag2=0;
// vector < vector < int > > adj(5000, vector < int > (5000));
// int  dp[1000006];
//  int level[500];

void solve() {
  int i, j = 0, r = 200, p, q, k = 0, ans = 0, d, h, flag = 0, n, m = 0, a, b,
         id, c, h1, h2, a1, b1;
  cin >> n >> k;
  vector<int> v(n), dp(n, hell);
  rep(i, 0, n) { cin >> v[i]; }
  dp[0] = 0;
  dp[1] = abs(v[1] - v[0]);
  rep(i, 2, n) {
    rep(j, 1, k + 1) { dp[i] = min(dp[i], dp[i - j] + abs(v[i - j] - v[i])); }
  }
  cout << dp[n - 1];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}

/*

void bfs( int i , int j)
{
    vis[i][j]= true;
  queue < int > que;
  que.push(k);
  while(!que.empty())
  {
     int x= que.front();
     que.pop();
      for ( int i=1;i<=n;i++)
      {
          if((!g[1][n] and g[x][i] ) or (g[1][n] and ! g[x][i]))
          {
            if(!vis[i])
            {
                que.push(i);
                level[i]=level[x]+1;
                vis[i]= true;
            }
          }
      }
  }
}

ll int por(ll int a,ll int b)
  {
     ll int k=1;
     while(b>0)
     {
         if(b%2) k=k*a,k%=hell;
         b/=2;
         a*=a; a%=hell;
     }
     return k;
  } */


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define rep(i, a, b) for (i = a; i < b; i++)
#define pb push_back
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define CLR(a) memset((a), 0, sizeof(a))
#define mp make_pair
#define hell 1000000007

using namespace std;
// int g[105][105];
// vector < int > wt(100004);
// vector < int > a;
// vector < int > v(30500);
// int cst[102][100005];
// int ansr[100005];
// ll int sum,x=0,c=0,n,m,flag=0,a1,a2,flag1=0,flag2=0;
// vector < vector < int > > adj(5000, vector < int > (5000));
// int  dp[1000006];
//  int level[500];

void solve() {
  int i, j = 0, r = 200, p, q, k = 0, ans = 0, d, h, flag = 0, n, m = 0, a, b,
         id, c, h1, h2, a1, b1;
  cin >> n >> k;
  vector<int> v(n), dp(n, hell);
  rep(i, 0, n) { cin >> v[i]; }
  dp[0] = 0;
  dp[1] = abs(v[1] - v[0]);
  rep(i, 2, n) {
    rep(j, 1, k + 1) {
      if (i - j >= 0)
        dp[i] = min(dp[i], dp[i - j] + abs(v[i - j] - v[i]));
    }
  }
  cout << dp[n - 1];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}

/*

void bfs( int i , int j)
{
    vis[i][j]= true;
  queue < int > que;
  que.push(k);
  while(!que.empty())
  {
     int x= que.front();
     que.pop();
      for ( int i=1;i<=n;i++)
      {
          if((!g[1][n] and g[x][i] ) or (g[1][n] and ! g[x][i]))
          {
            if(!vis[i])
            {
                que.push(i);
                level[i]=level[x]+1;
                vis[i]= true;
            }
          }
      }
  }
}

ll int por(ll int a,ll int b)
  {
     ll int k=1;
     while(b>0)
     {
         if(b%2) k=k*a,k%=hell;
         b/=2;
         a*=a; a%=hell;
     }
     return k;
  } */