結果
| 問題 |
No.1858 Gorgeous Knapsack
|
| ユーザー |
tenten
|
| 提出日時 | 2022-02-28 11:09:43 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 946 ms / 2,000 ms |
| コード長 | 2,555 bytes |
| コンパイル時間 | 1,849 ms |
| コンパイル使用メモリ | 77,704 KB |
| 実行使用メモリ | 250,032 KB |
| 最終ジャッジ日時 | 2024-07-06 13:34:33 |
| 合計ジャッジ時間 | 12,225 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 37 |
ソースコード
import java.io.*;
import java.util.*;
public class Main {
static Jewel[] jewels;
static long[][] dp;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int m = sc.nextInt();
jewels = new Jewel[n];
for (int i = 0; i < n; i++) {
jewels[i] = new Jewel(sc.nextInt(), sc.nextInt());
}
Arrays.sort(jewels);
dp = new long[n][m + 1];
for (long[] arr : dp) {
Arrays.fill(arr, -1);
}
long ans = 0;
for (int i = 0; i < n; i++) {
ans = Math.max(ans, (dfw(i - 1, m - jewels[i].weight) + jewels[i].value) * jewels[i].value);
}
System.out.println(ans);
}
static long dfw(int idx, int w) {
if (w < 0) {
return Integer.MIN_VALUE;
}
if (idx < 0) {
return 0;
}
if (dp[idx][w] < 0) {
dp[idx][w] = Math.max(dfw(idx - 1, w), dfw(idx - 1, w - jewels[idx].weight) + jewels[idx].value);
}
return dp[idx][w];
}
static boolean getEnable(int x, int y) {
if (x == y) {
return true;
}
if (x > y) {
return false;
}
for (int i = 2; i <= Math.sqrt(y); i++) {
if (y % i > 0) {
continue;
}
if (getEnable(x, i + y / i)) {
return true;
}
}
return false;
}
static class Jewel implements Comparable<Jewel> {
int value;
int weight;
public Jewel(int value, int weight) {
this.value = value;
this.weight = weight;
}
public int compareTo(Jewel another) {
return another.value - value;
}
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
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 String next() throws Exception {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten