-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_for_problem_B(Frog_2).cpp
More file actions
147 lines (105 loc) · 2.37 KB
/
code_for_problem_B(Frog_2).cpp
File metadata and controls
147 lines (105 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
ATCODER DP : PROBLEM B (Frog 2)
PROBLEM LINK : https://atcoder.jp/contests/dp/tasks/dp_b
*/
#include<algorithm>
#include<bits/stdc++.h>
#define ll long long int
#define dbl long double
#define loop(i,ed) for(ll i=0;i<ed;i++)
#define rloop(i,ed) for(ll i=ed-1;i>=0;i--)
#define aloop(it,a) for(auto it:a)
#define test(tc) ll tc;cin>>tc;while(tc--)
#define debo ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define vi vector<ll>
#define sz size()
#define bs binary_search
#define setBits __builtin_popcountll
#define mod 1000000007
#define yos cout<<"YES"<<endl
#define nos cout<<"NO"<<endl
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define F first
#define S second
#define vep vector<pair<ll,ll>>
#define all(v) v.begin(),v.end()
#define tos to_string
#define iss(v) is_sorted(all(v))
#define sp " "
#define mem(a,i) memset(a,i,sizeof(a))
using namespace std;
/*
" " =>32
A=>65
a=>97
*/
bool isPrime(ll n)
{
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
bool IsPalindrome( string str)
{
if (str.empty())
return false;
ll i = 0; // first characters
ll j = str.length() - 1; // last character
while (i < j)
{
if (str[i] != str[j])
{
return false;
}
i++;
j--;
}
return true;
}
// Binary Exponentiation fun to calculate pow in 0(logN)
ll binpow(ll a, ll b) {
if (b == 0)
return 1;
ll res = binpow(a, b / 2);
if (b % 2)
return (res * res * a);
return (res * res);
}
ll ar[100000+5];
ll n;
ll dp[100000+5];
ll k;
ll minCost(ll ind)
{
if(ind==(n-1))
return 0;
else if(dp[ind]!=-1) return dp[ind];
else
{
ll ans = INT_MAX;
for(ll i = 1;i<=k;i++)
{
if(ind+i<=n-1)
ans = min(ans,abs(ar[ind+i]-ar[ind]) + minCost(ind+i));
}
return dp[ind] = ans;
}
}
signed main(){
debo;
// test(tc){
cin >> n>>k;
for(ll i=0;i<n;i++){
cin >> ar[i];
dp[i]=-1;
}
cout << minCost(0);
// }
return 0;
}