結果

問題 No.2390 Udon Coupon (Hard)
ユーザー lam6er
提出日時 2025-03-20 20:20:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 1,521 bytes
コンパイル時間 199 ms
コンパイル使用メモリ 82,380 KB
実行使用メモリ 67,368 KB
最終ジャッジ日時 2025-03-20 20:22:26
合計ジャッジ時間 4,605 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 47
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
coupons = []
for _ in range(3):
    a, b = map(int, input().split())
    coupons.append((a, b))

# Custom comparator to sort coupons by efficiency (B_i/A_i) in descending order.
# If efficiency is equal, prefer the one with smaller A_i to potentially use coupons more effectively.
def compare(x, y):
    a1, b1 = x
    a2, b2 = y
    val1 = b1 * a2
    val2 = b2 * a1
    if val1 != val2:
        return val2 - val1  # Higher efficiency first
    else:
        return a1 - a2  # Lower A_i first if efficiency is the same

from functools import cmp_to_key
coupons_sorted = sorted(coupons, key=cmp_to_key(compare))
X, Y, Z = coupons_sorted  # Unpack sorted coupons into X (most efficient), Y, Z (least efficient)

max_total = 0
max_reduce = 2000  # Number of previous candidates to check for each coupon type

# Iterate possible usages for X (most efficient)
m1_max = n // X[0]
start_m1 = max(m1_max - max_reduce, 0)
for m1 in range(start_m1, m1_max + 1):
    remaining = n - m1 * X[0]
    if remaining < 0:
        continue
    current_x = m1 * X[1]
    
    # Now handle Y (second most efficient)
    m2_max = remaining // Y[0]
    start_m2 = max(m2_max - max_reduce, 0)
    for m2 in range(start_m2, m2_max + 1):
        rem_after_y = remaining - m2 * Y[0]
        if rem_after_y < 0:
            continue
        # Handle Z (least efficient)
        m3 = rem_after_y // Z[0]
        total = current_x + m2 * Y[1] + m3 * Z[1]
        if total > max_total:
            max_total = total

print(max_total)
0