結果
| 問題 |
No.2390 Udon Coupon (Hard)
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2023-07-24 13:35:34 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,070 bytes |
| コンパイル時間 | 3,186 ms |
| コンパイル使用メモリ | 90,796 KB |
| 実行使用メモリ | 370,748 KB |
| 最終ジャッジ日時 | 2024-10-01 08:44:16 |
| 合計ジャッジ時間 | 11,421 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 WA * 19 TLE * 2 -- * 9 |
ソースコード
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class Main {
static Coupon[] coupons = new Coupon[3];
static ArrayList<HashMap<Long, Long>> dp = new ArrayList<>();
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
long n = sc.nextLong();
for (int i = 0; i < 3; i++) {
coupons[i] = new Coupon(sc.nextInt(), sc.nextInt());
dp.add(new HashMap<>());
}
Arrays.sort(coupons);
long lcm = 1;
for (Coupon x : coupons) {
lcm = getLCM(x.count, lcm);
}
long max = 0;
for (Coupon x : coupons) {
max = Math.max(max, lcm / x.count * x.value);
}
long ans = n / lcm * max;
n %= lcm;
System.out.println(ans + dfw(2, n));
}
static long getLCM(long x, long y) {
return x / getGCD(x, y) * y;
}
static long getGCD(long x, long y) {
if (y == 0) {
return x;
} else {
return getGCD(y, x % y);
}
}
static long dfw(int idx, long v) {
if (v < 0) {
return Long.MIN_VALUE;
}
if (idx == 0) {
return v / coupons[0].count * coupons[0].value;
}
if (!dp.get(idx).containsKey(v)) {
dp.get(idx).put(v, Math.max(dfw(idx - 1, v), dfw(idx, v - coupons[idx].count) + coupons[idx].value));
}
return dp.get(idx).get(v);
}
static class Coupon implements Comparable<Coupon> {
int count;
int value;
public Coupon(int count, int value) {
this.count = count;
this.value = value;
}
public int compareTo(Coupon another) {
return count - another.count;
}
}
}
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