FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java-Competitive-Programming/Algorithms/Knapsack.java at threads · joney000/Java-Competitive-Programming · GitHub
joney000
/
Java-Competitive-Programming
Public
Notifications
You must be signed in to change notification settings
Fork
30
Star
124
Code
Issues
0
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java-Competitive-Programming
/
Algorithms
/
Knapsack.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
59 lines (44 loc) · 1.34 KB
Breadcrumbs
Java-Competitive-Programming
/
Algorithms
/
Knapsack.java
Copy path
File metadata and controls
executable file
·
59 lines (44 loc) · 1.34 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/* package joney_000 */
import
java
.
util
.*;
import
java
.
lang
.*;
import
java
.
io
.*;
import
java
.
math
.*;
class
Knapsack
{
public
static
long
ans
=
0
;
public
static
void
main
(
String
[]
args
)
throws
Exception
{
BufferedReader
br
=
new
BufferedReader
(
new
InputStreamReader
(
System
.
in
));
BufferedWriter
out
=
new
BufferedWriter
(
new
OutputStreamWriter
(
System
.
out
));
ans
=
0
;
int
tests
=
Integer
.
parseInt
(
br
.
readLine
());
for
(
int
t
=
0
;
t
<
tests
;
t
++){
int
n
=
Integer
.
parseInt
(
br
.
readLine
());
int
W
=
Integer
.
parseInt
(
br
.
readLine
());
int
val
[]=
new
int
[
n
];
String
[]
s
=
br
.
readLine
().
split
(
" "
);
for
(
int
i
=
0
;
i
<
n
;
i
++){
val
[
i
]=
Integer
.
parseInt
(
s
[
i
]);
}
int
wt
[]=
new
int
[
n
];
s
=
br
.
readLine
().
split
(
" "
);
for
(
int
i
=
0
;
i
<
n
;
i
++){
wt
[
i
]=
Integer
.
parseInt
(
s
[
i
]);
}
int
[][]
dp
=
new
int
[
n
+
1
][
W
+
1
];
for
(
int
i
=
0
;
i
<=
n
;
i
++)
dp
[
i
][
0
]=
0
;
for
(
int
i
=
0
;
i
<=
W
;
i
++)
dp
[
0
][
i
]=
0
;
for
(
int
i
=
1
;
i
<=
n
;
i
++){
for
(
int
j
=
1
;
j
<=
W
;
j
++){
if
(
wt
[
i
-
1
]<=
j
){
dp
[
i
][
j
]=
Math
.
max
(
dp
[
i
-
1
][
j
],
val
[
i
-
1
]+
dp
[
i
-
1
][
j
-
wt
[
i
-
1
]]);
}
else
{
dp
[
i
][
j
]=
dp
[
i
-
1
][
j
];
}
}
}
ans
=
dp
[
n
][
W
];
out
.
write
(
""
+
ans
+
"
\n
"
);
out
.
flush
();
}
}
}
Back
|
FazBrowse Home
|
New Git URL