-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrog2.java
More file actions
68 lines (47 loc) · 1.29 KB
/
frog2.java
File metadata and controls
68 lines (47 loc) · 1.29 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
//problem link :- https://atcoder.jp/contests/dp/tasks/dp_b
import java.util.*;
import java.io.*;
class Main{
public static void main(String []args) throws Exception{
//System.out.println("Hello World");
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
int N,k;
String s;
s=br.readLine();
String ss[]=s.split(" ");
N=Integer.parseInt(ss[0]);
k=Integer.parseInt(ss[1]);
int arr[]=new int[N];
s=br.readLine();
ss=s.split(" ");
for(int i=0;i<N;i++)
{
arr[i]=Integer.parseInt(ss[i]);
}
if(N==2)
{
System.out.println(Math.abs(arr[0]-arr[1]));
}
else{
long dp[]=new long[N];
dp[0]=0l;
dp[1]=Math.abs(arr[1]-arr[0])*1l;
long temp=10000000009l;
for (int i=2;i<N;i++)
{
temp=1000000009l;
for(int j=1;j<=k;j++)
{
if(i-j<0)
{
continue;
}
long var=(dp[i-j]+Math.abs(arr[i]-arr[i-j]))*1l;
temp=Math.min(var,temp);
}
dp[i]=temp;
}
System.out.println(dp[N-1]);
}
}
}