-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBowlingForNumbers.java
71 lines (61 loc) · 1.87 KB
/
BowlingForNumbers.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
61
62
63
64
65
66
67
68
69
70
71
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Class9;
import java.util.*;
/**
*
* @author ericawang
*/
public class BowlingForNumbers {
static int [] in;
static int [][]dp;
public static int sum ( int begin, int end){
int sum = 0;
for(int i = begin; i<=end; i++){
sum +=in[i];
}
return sum;
}
public static void print2D (int [][]arr){
for(int i = 0; i<arr.length;i++){
for(int u = 0; u<arr[0].length;u++){
System.out.print(arr[i][u]+" ");
}
System.out.println();
}
}
public static void main (String [] args){
Scanner input = new Scanner(System.in);
int t = input.nextInt();
for(int p = 0; p<t; p++){
int n = input.nextInt(); //pins
int k = input.nextInt(); //balls
int w = input.nextInt(); //width 3
in = new int [n];
for(int i = 0; i<n; i++){
in[i]=input.nextInt();
}
dp = new int [k][n];
for(int i = 0; i<n; i++){
if(i<w){
dp[0][i]=sum(0,i);
}else{
dp[0][i]=Math.max(sum(i-w+1,i), dp[0][i-1]);
}
}
for(int N = 1; N<k; N++){
for(int i = 0; i<n; i++){
if(i<(w*(N+1))){
dp[N][i]=sum(0,i);
}else{
dp[N][i]=Math.max(sum(i-w+1,i)+dp[N-1][i-w], dp[N][i-1]);
}
}
}
System.out.println(dp[k-1][n-1]);
}
}
}