結果
| 問題 | No.1858 Gorgeous Knapsack |
| ユーザー |
tenten
|
| 提出日時 | 2023-01-17 13:13:56 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 1,514 ms / 2,000 ms |
| コード長 | 2,896 bytes |
| 記録 | |
| コンパイル時間 | 2,588 ms |
| コンパイル使用メモリ | 91,780 KB |
| 実行使用メモリ | 450,632 KB |
| 最終ジャッジ日時 | 2025-01-02 11:49:06 |
| 合計ジャッジ時間 | 16,432 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 37 |
ソースコード
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class Main {
static long[][][] dp;
static Juel[] juels;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int m = sc.nextInt();
juels = new Juel[n];
for (int i = 0; i < n; i++) {
juels[i] = new Juel(sc.nextInt(), sc.nextInt());
}
Arrays.sort(juels);
dp = new long[2][n][m + 1];
for (long[][] arr1 : dp) {
for (long[] arr2 : arr1) {
Arrays.fill(arr2, -1);
}
}
System.out.println(dfw(0, n - 1, m));
}
static long dfw(int type, int idx, int w) {
if (w < 0) {
return Integer.MIN_VALUE;
}
if (idx < 0) {
return 0;
}
if (dp[type][idx][w] < 0) {
dp[type][idx][w] = dfw(type, idx - 1, w);
if (type == 0) {
dp[type][idx][w] = Math.max(dp[type][idx][w], (dfw(1, idx - 1, w - juels[idx].weight) + juels[idx].value) * juels[idx].value);
} else {
dp[type][idx][w] = Math.max(dp[type][idx][w], (dfw(1, idx - 1, w - juels[idx].weight) + juels[idx].value));
}
}
return dp[type][idx][w];
}
static class Juel implements Comparable<Juel> {
int weight;
int value;
public Juel(int value, int weight) {
this.weight = weight;
this.value = value;
}
public int compareTo(Juel another) {
return another.value - value;
}
}
}
class Utilities {
static String arrayToLineString(Object[] arr) {
return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
}
static String arrayToLineString(int[] arr) {
return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
public Scanner() throws Exception {
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public double nextDouble() throws Exception {
return Double.parseDouble(next());
}
public int[] nextIntArray() throws Exception {
return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
}
public String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten