-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
35 lines (28 loc) Β· 1.09 KB
/
Main.java
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
package DP.P15678;
import java.io.*;
import java.util.*;
public class Main {
static int N, D, K;
static long ans = Long.MIN_VALUE;
static long[] dp;
static Deque<Integer> dq;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/DP/P15678/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
D = Integer.parseInt(st.nextToken());
dp = new long[N];
dq = new LinkedList<>();
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
K = Integer.parseInt(st.nextToken());
while (!dq.isEmpty() && i - dq.peekFirst() > D) dq.removeFirst();
dp[i] = Math.max(dq.isEmpty() ? K : dp[dq.peekFirst()] + K, K);
ans = Math.max(ans, dp[i]);
while (!dq.isEmpty() && dp[dq.peekLast()] < dp[i]) dq.removeLast();
dq.add(i);
}
System.out.println(ans);
}
}