-
Notifications
You must be signed in to change notification settings - Fork 3
/
CCC07S5.java
60 lines (60 loc) · 1.93 KB
/
CCC07S5.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
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
* CCC '07 S5 - Bowling for Numbers
* Question type: Dynamic Programming
* 5/5 on DMOJ
* @author Milliken high school -> http://mmhs.ca/ccc/index.htm
*/
public class CCC07S5 {
static StringTokenizer st;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int N, K, W, ans;
static int [] balls;
static int [] sum;
static int [][] dp;
public static void main(String[] args) throws IOException {
int cases = Integer.parseInt(br.readLine());
for (int i = 0; i < cases; i++) {
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
W = Integer.parseInt(st.nextToken());
dp = new int[K+1][N+1];
balls = new int[N+1];
sum = new int[N+1];
for (int j = 1; j <= N; j++) {
balls[j] = Integer.parseInt(br.readLine());
}
for (int j = 1; j <= N-W+1; j++) {
for (int k = j; k < j+W; k++) {
sum[j]+=balls[k];
}
}
solve();
System.out.println(dp[K][1]);
}
}
static void solve(){
for (int i = 0; i < N+1; i++) {
dp[0][i] = 0;
}
for (int i = 1; i < K+1; i++) {
for (int j = 1; j < N+1; j++) {
dp[i][j] = -1;
}
}
for (int i = 1; i <= K; i++) {
for (int j = N; j >= 1; j--) {
if (j>=N-W+1){
dp[i][j] = sum[j];
}
else {
dp[i][j] = Math.max(dp[i-1][j+W] + sum[j], dp[i][j+1]);
}
}
}
}
}