Language: C++

#include#include<bits/stdc++.h>
#include<ctype.h>
# define pb push_back
#define fst first
#define sec second
#define For(i,a,b) for(int i=a;i<b;i++)
#define ll long long int
#define ull unsigned long long int
#define mod 1000000007
#define fo(i,n) for(ll i=0;i<n;i++)
#define endl "\n"
#define rev(i,n) for(ll i=n-1;i>=0;i--)
#define fo1(i,n) for(ll i=1;i<=n;i++)
#define boolsize 1000001
#define pi pair<ll,ll>
#define vi vector<ll>
#define vii vector<pi>
using namespace std;
template<typename T>
void showvector(vector <T> v)
{
    for(T x:v)
        cout<<x<<" ";
    cout<<endl;

}
template<typename T>
void showvector1(vector <T> v)
{
    ll n=v.size();
    fo1(i,n-1)
        cout<<v[i]<<endl;

}
template<typename T>
void showset(set <T> s)
{
    for(T x: s)
        cout<<x<<" ";
    cout<<endl;
}
template<class T>
void showvectorpair(vector<T> v)
{
    for(auto it=v.begin();it!=v.end();it++)
        cout<<it->first<<" "<<it->second<<endl;
    cout<<endl;
}
template<typename T,typename P>
void showmap(map <T,P> m)
{
    for(auto it=m.begin();it!=m.end();it++)
        cout<<it->first<<" "<<it->second<<endl;
    cout<<endl;
}
template<typename T>
bool comp(T a,T b)
{
    return (a>b);
}
template<class T>
bool comppair(T a,T b)
{
    if(a.first==b.first)
        return(a.second>b.second);
    return (a.first>b.first);
}
bool sameparity(ll a,ll b)
{
    return (a%2==b%2);
}
bool difparity(ll a,ll b)
{
    return !(a%2==b%2);
}
bool isprime(ll x)
{   if(x<=1)
     return false;
    for(ll i=2;i<=sqrt(x);i++)
    {
        if(x%i==0)
            return false;
    }
    return true;
}
bool iseven(ll x)
{
    return !(x%2);
}
bool isodd(ll x)
{
    return (x%2);
}
/// check for test case before submitting
void vfun()
{
    ll n,k;
    cin>>n;
    vector <ll> v(n);
    fo(i,n)
    cin>>v[i];

}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("inputh.txt", "r", stdin);
    freopen("outputh.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0); ///cant use scanf, printf
    cin.tie(0);cout.tie(0); /// no longer auto flush cout before each cin, remove for interactive
    //cout << fixed << setprecision(11); /// no scientific output
    ll test=1;
    //cin>>test;
    while(test--)
    {
            ll n,k;
            cin>>n>>k;
            vi dp(n,INT_MAX),v(n);
            fo(i,n)
                cin>>v[i];
            dp[0]=0;
            fo(i,n)
            {
                ll j=i-1;
                while(j>=0 && j>=i-k)
                {
                    dp[i]=min(dp[i],dp[j]+abs(v[i]-v[j]));
                    j--;
                }
            }
           // showvector(dp);
            cout<<dp[n-1];

//         if(vfun())
//            cout<<"YES\n";
//         else
//            cout<<"NO\n";
    }
}
///before sub
/// check for value of zero and single input in array
///loop vars,1LL in mult, equal, one, bounds, int v ll, endl, finish taking inputs
/// check whether test cases are given or not





Compiler output:
a.cc:1:9: error: #include expects "FILENAME" or <FILENAME>
    1 | #include#include<bits/stdc++.h>
      |         ^
a.cc:20:6: error: variable or field 'showvector' declared void
   20 | void showvector(vector <T> v)
      |      ^~~~~~~~~~
a.cc:20:17: error: 'vector' was not declared in this scope
   20 | void showvector(vector <T> v)
      |                 ^~~~~~
a.cc:3:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
    2 | #include<ctype.h>
  +++ |+#include <vector>
    3 | # define pb push_back
a.cc:20:26: error: expected primary-expression before '>' token
   20 | void showvector(vector <T> v)
      |                          ^
a.cc:20:28: error: 'v' was not declared in this scope
   20 | void showvector(vector <T> v)
      |                            ^
a.cc:28:6: error: variable or field 'showvector1' declared void
   28 | void showvector1(vector <T> v)
      |      ^~~~~~~~~~~
a.cc:28:18: error: 'vector' was not declared in this scope
   28 | void showvector1(vector <T> v)
      |                  ^~~~~~
a.cc:28:18: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
a.cc:28:27: error: expected primary-expression before '>' token
   28 | void showvector1(vector <T> v)
      |                           ^
a.cc:28:29: error: 'v' was not declared in this scope
   28 | void showvector1(vector <T> v)
      |                             ^
a.cc:36:6: error: variable or field 'showset' declared void
   36 | void showset(set <T> s)
      |      ^~~~~~~
a.cc:36:14: error: 'set' was not declared in this scope
   36 | void showset(set <T> s)
      |              ^~~
a.cc:3:1: note: 'std::set' is defined in header '<set>'; this is probably fixable by adding '#include <set>'
    2 | #include<ctype.h>
  +++ |+#include <set>
    3 | # define pb push_back
a.cc:36:20: error: expected primary-expression before '>' token
   36 | void showset(set <T> s)
      |                    ^
a.cc:36:22: error: 's' was not declared in this scope
   36 | void showset(set <T> s)
      |                      ^
a.cc:43:6: error: variable or field 'showvectorpair' declared void
   43 | void showvectorpair(vector<T> v)
      |      ^~~~~~~~~~~~~~
a.cc:43:21: error: 'vector' was not declared in this scope
   43 | void showvectorpair(vector<T> v)
      |                     ^~~~~~
a.cc:43:21: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
a.cc:43:29: error: expected primary-expression before '>' token
   43 | void showvectorpair(vector<T> v)
      |                             ^
a.cc:43:31: error: 'v' was not declared in this scope
   43 | void showvectorpair(vector<T> v)
      |                               ^
a.cc:50:6: error: variable or field 'showmap' declared void
   50 | void showmap(map <T,P> m)
      |      ^~~~~~~
a.cc:50:14: error: 'map' was not declared in this scope
   50 | void showmap(map <T,P> m)
      |              ^~~
a.cc:3:1: note: 'std::map' is defined in header '<map>'; this is probably fixable by adding '#include <map>'
    2 | #include<ctype.h>
  +++ |+#include <map>
    3 | # define pb push_back
a.cc:50:20: error: expected primary-expression before ',' token
   50 | void showmap(map <T,P> m)
      |                    ^
a.cc:50:22: error: expected primary-expression before '>' token
   50 | void showmap(map <T,P> m)
      |                      ^
a.cc:50:24: error: 'm' was not declared in this scope
   50 | void showmap(map <T,P> m)
      |                        ^
a.cc: In function 'bool isprime(long long int)':
a.cc:79:19: error: 'sqrt' was not declared in this scope
   79 |     for(ll i=2;i<=sqrt(x);i++)
      |                   ^~~~
a.cc: In function 'void vfun()':
a.cc:98:5: error: 'cin' was not declared in this scope
   98 |     cin>>n;
      |     ^~~
a.cc:3:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    2 | #include<ctype.h>
  +++ |+#include <iostream>
    3 | # define pb push_back
a.cc:99:5: error: 'vector' was not declared in this scope
   99 |     vector <ll> v(n);
      |     ^~~~~~
a.cc:99:5: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
a.cc:7:12: error: expected primary-expression before 'long'
    7 | #define ll long long int
      |            ^~~~
a.cc:99:13: note: in expansion of macro 'll'
   99 |     vector <ll> v(n);
      |             ^~
a.cc:101:10: error: 'v' was not declared in this scope
  101 |     cin>>v[i];
      |          ^
a.cc: In function 'int main()':
a.cc:108:32: error: 'stdin' was not declared in this scope
  108 |     freopen("inputh.txt", "r", stdin);
      |                                ^~~~~
a.cc:3:1: note: 'stdin' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
    2 | #include<ctype.h>
  +++ |+#include <cstdio>
    3 | # define pb push_back
a.cc:108:5: error: 'freopen' was not declared in this scope
  108 |     freopen("inputh.txt", "r", stdin);
      |     ^~~~~~~
a.cc:109:33: error: 'stdout' was not declared in this scope
  109 |     freopen("outputh.txt", "w", stdout);
      |                                 ^~~~~~
a.cc:109:33: note: 'stdout' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
a.cc:111:5: error: 'ios_base' has not been declared
  111 |     ios_base::sync_with_stdio(0); ///cant use scanf, printf
      |     ^~~~~~~~
a.cc:112:5: error: 'cin' was not declared in this scope
  112 |     cin.tie(0);cout.tie(0); /// no longer auto flush cout before each cin, remove for interactive
      |     ^~~
a.cc:112:5: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:112:16: error: 'cout' was not declared in this scope
  112 |     cin.tie(0);cout.tie(0); /// no longer auto flush cout before each cin, remove for interactive
      |                ^~~~
a.cc:112:16: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:16:12: error: 'vector' was not declared in this scope
   16 | #define vi vector<ll>
      |            ^~~~~~
a.cc:120:13: note: in expansion of macro 'vi'
  120 |             vi dp(n,INT_MAX),v(n);
      |             ^~
a.cc:16:12: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
   16 | #define vi vector<ll>
      |            ^~~~~~
a.cc:120:13: note: in expansion of macro 'vi'
  120 |             vi dp(n,INT_MAX),v(n);
      |             ^~
a.cc:7:12: error: expected primary-expression before 'long'
    7 | #define ll long long int
      |            ^~~~
a.cc:16:19: note: in expansion of macro 'll'
   16 | #define vi vector<ll>
      |                   ^~
a.cc:120:13: note: in expansion of macro 'vi'
  120 |             vi dp(n,INT_MAX),v(n);
      |             ^~
a.cc:122:22: error: 'v' was not declared in this scope
  122 |                 cin>>v[i];
      |                      ^
a.cc:123:13: error: 'dp' was not declared in this scope
  123 |             dp[0]=0;
      |             ^~
a.cc:129:47: error: 'v' was not declared in this scope
  129 |                     dp[i]=min(dp[i],dp[j]+abs(v[i]-v[j]));
      |                                               ^
a.cc:129:43: error: 'abs' was not declared in this scope
  129 |                     dp[i]=min(dp[i],dp[j]+abs(v[i]-v[j]));
      |                                           ^~~
a.cc:129:27: error: 'min' was not declared in this scope; did you mean 'main'?
  129 |                     dp[i]=min(dp[i],dp[j]+abs(v[i]-v[j]));
      |                           ^~~
      |                           main