Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

// c++ templte for
// help............................................................................................................................................
typedef long long ll;

// template<typename... T>
// void print1(T... t){
//((cout<<t<<" "),...);
//}

// template<typename... T>
// void print2(T... t){
// vector<vector<ll>> ans={t...};
// for(ll i=0;i<(ll)ans.size();i++){
// if(i!=0) cout<<"\t";
// else cout<<"\t";
// for(ll j=0;j<(ll)ans[i].size();j++){
// cout<<ans[i][j]<<" ";
//}
// if(i!=(ll)ans.size()-1)
// cout<<endl;
//}
//}
// #define deb(...) cout<<" [ "#__VA_ARGS__<<" : "; print1(__VA_ARGS__);
// cout<<"]"<<endl; #define vdeb(...) cout<<" [ "#__VA_ARGS__<<" : ";
// print2(__VA_ARGS__); cout<<"]"<<endl;

// for loop
#define fo(i, n) for (int i = 0; i < (ll)n; i++)
#define Fo(i, k, n) for (int i = k; i < n; i++)

// to print vector element
// template<typename T>
// void printV(T vec){
// fo(i,vec.size()) cout<<vec[i]<<" "<<flush;
// cout<<endl;
// }

#define modulo 1000000007
const int INF = 1e9 + 5;

// iterate map
// template<typename T>
// void itR(T map){
// for(auto itr=map.begin();itr!=map.end();itr++){
// deb(itr->first,itr->second);
//}
// cout<<endl;
//}

// c++ template for
// help...........................................................................................................................................

ll solve() {
  ll n, k;
  cin >> n >> k;

  vector<int> vec(n);
  fo(i, n) { cin >> vec[i]; }

  vector<int> dp(n + 1, INF); // dp[n] -- n pa jane ma kintna cost lagega
  dp[0] = 0;
  dp[1] = 0;

  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      if (i - j <= n and i - j - 1 < n)
        dp[i] = min(dp[i], dp[i - j] + abs(vec[i - 1] - vec[i - j - 1]));
    }
  }

  return dp[n];
}

int main() {
  // stringstream ss;
  // ss<<"akhil";
  // cout<<ss.str()<<endl;
  cout << setprecision(50);
  ios::sync_with_stdio(0);
  cin.tie(0);

  // TESTCASE KO DEKH KA BHAUT SARA CONDITION GENERATE MAT KRO
  // PAPER PD CONFIRM HO JAO THORA JADA TIME HI KYU NA LGA
  // greedy ma jadatr exceptional case check kr ke dekh lo

  // WHILE DOING THE QUESTION OF DYNAMIC PROGRAMMING GREEDY KA TRAH SOCHNA BAND
  // KRO DP M DATA KA BHI USE KRNA PDTA HA BE BHULO MT DP BHI WO LEETCODE WALE
  // MATRIX KA QUESTION KI TARAH HA CONSTRAIN WILL BE THE DIMENSION OF DP DP MA
  // STATE PTA KRO AUR PHIR FUNCTION GENERATE KRO OBJECT KO CHOOSE KIA YA NAE
  // KIA AGAR KIA TO KYA HOGA AUR NAE KIA TO KYA HOGA KYA KYA CHEJ IMPORTANT HA
  // WO STATE BN JAGA AUR HUM LOG STATE KO CHANGE KR SKTA HA JYSA KNPSACK 2 WALE
  // PROBLEM MA KIA THA

  // ll testCase;
  // cin>>testCase;
  // for(ll tt=0;tt<testCase;tt++){
  // solve();
  cout << solve() << endl;
  //}
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

// c++ templte for
// help............................................................................................................................................
typedef long long ll;

// template<typename... T>
// void print1(T... t){
//((cout<<t<<" "),...);
//}

// template<typename... T>
// void print2(T... t){
// vector<vector<ll>> ans={t...};
// for(ll i=0;i<(ll)ans.size();i++){
// if(i!=0) cout<<"\t";
// else cout<<"\t";
// for(ll j=0;j<(ll)ans[i].size();j++){
// cout<<ans[i][j]<<" ";
//}
// if(i!=(ll)ans.size()-1)
// cout<<endl;
//}
//}
// #define deb(...) cout<<" [ "#__VA_ARGS__<<" : "; print1(__VA_ARGS__);
// cout<<"]"<<endl; #define vdeb(...) cout<<" [ "#__VA_ARGS__<<" : ";
// print2(__VA_ARGS__); cout<<"]"<<endl;

// for loop
#define fo(i, n) for (int i = 0; i < (ll)n; i++)
#define Fo(i, k, n) for (int i = k; i < n; i++)

// to print vector element
// template<typename T>
// void printV(T vec){
// fo(i,vec.size()) cout<<vec[i]<<" "<<flush;
// cout<<endl;
// }

#define modulo 1000000007
const int INF = 1e9 + 5;

// iterate map
// template<typename T>
// void itR(T map){
// for(auto itr=map.begin();itr!=map.end();itr++){
// deb(itr->first,itr->second);
//}
// cout<<endl;
//}

// c++ template for
// help...........................................................................................................................................

ll solve() {
  ll n, k;
  cin >> n >> k;

  vector<int> vec(n);
  fo(i, n) { cin >> vec[i]; }

  vector<int> dp(n + 1, INF); // dp[n] -- n pa jane ma kintna cost lagega
  dp[0] = 0;
  dp[1] = 0;

  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      if (i - j - 1 >= 0)
        dp[i] = min(dp[i], dp[i - j] + abs(vec[i - 1] - vec[i - j - 1]));
    }
  }

  return dp[n];
}

int main() {
  // stringstream ss;
  // ss<<"akhil";
  // cout<<ss.str()<<endl;
  cout << setprecision(50);
  ios::sync_with_stdio(0);
  cin.tie(0);

  // TESTCASE KO DEKH KA BHAUT SARA CONDITION GENERATE MAT KRO
  // PAPER PD CONFIRM HO JAO THORA JADA TIME HI KYU NA LGA
  // greedy ma jadatr exceptional case check kr ke dekh lo

  // WHILE DOING THE QUESTION OF DYNAMIC PROGRAMMING GREEDY KA TRAH SOCHNA BAND
  // KRO DP M DATA KA BHI USE KRNA PDTA HA BE BHULO MT DP BHI WO LEETCODE WALE
  // MATRIX KA QUESTION KI TARAH HA CONSTRAIN WILL BE THE DIMENSION OF DP DP MA
  // STATE PTA KRO AUR PHIR FUNCTION GENERATE KRO OBJECT KO CHOOSE KIA YA NAE
  // KIA AGAR KIA TO KYA HOGA AUR NAE KIA TO KYA HOGA KYA KYA CHEJ IMPORTANT HA
  // WO STATE BN JAGA AUR HUM LOG STATE KO CHANGE KR SKTA HA JYSA KNPSACK 2 WALE
  // PROBLEM MA KIA THA

  // ll testCase;
  // cin>>testCase;
  // for(ll tt=0;tt<testCase;tt++){
  // solve();
  cout << solve() << endl;
  //}
  return 0;
}