結果

問題 No.158 奇妙なお使い
ユーザー lam6er
提出日時 2025-03-31 17:25:25
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,755 bytes
コンパイル時間 464 ms
コンパイル使用メモリ 82,844 KB
実行使用メモリ 76,512 KB
最終ジャッジ日時 2025-03-31 17:26:16
合計ジャッジ時間 2,831 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 21 WA * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

def readints():
    return list(map(int, input().split()))

class Transaction:
    def __init__(self, D, coins, loss):
        self.D = D
        self.coins = coins  # (B1000, B100, B1)
        self.loss = loss

def can_pay(x, a1000, a100, a1):
    max_1000 = min(a1000, x // 1000)
    for u1000 in range(max_1000, -1, -1):
        rem = x - u1000 * 1000
        if rem < 0:
            continue
        max_100 = min(a100, rem // 100)
        for u100 in range(max_100, -1, -1):
            rem1 = rem - u100 * 100
            if rem1 < 0:
                continue
            if rem1 <= a1:
                return True
    return False

def find_payment(x, a1000, a100, a1):
    max_1000 = min(a1000, x // 1000)
    for u1000 in range(max_1000, -1, -1):
        rem = x - u1000 * 1000
        if rem < 0:
            continue
        max_100 = min(a100, rem // 100)
        for u100 in range(max_100, -1, -1):
            rem1 = rem - u100 * 100
            if rem1 < 0:
                continue
            if rem1 <= a1:
                return (u1000, u100, rem1)
    return None

def simulate(initial_coins, first, second):
    current_1000, current_100, current_1 = initial_coins
    count = 0
    while True:
        executed = False
        for trans in [first, second]:
            if can_pay(trans.D, current_1000, current_100, current_1):
                payment = find_payment(trans.D, current_1000, current_100, current_1)
                if payment is None:
                    continue  # Shouldn't happen as can_pay was True
                u1000, u100, u1 = payment
                # Deduct the coins used
                current_1000 -= u1000
                current_100 -= u100
                current_1 -= u1
                # Add the coins received
                b1000, b100, b1 = trans.coins
                current_1000 += b1000
                current_100 += b100
                current_1 += b1
                count += 1
                executed = True
                break
        if not executed:
            break
    return count

# Read input
A = readints()
Db = int(input())
B_coins = readints()
Dc = int(input())
C_coins = readints()

# Calculate loss for B and C transactions
loss_B = Db - (B_coins[0] * 1000 + B_coins[1] * 100 + B_coins[2])
loss_C = Dc - (C_coins[0] * 1000 + C_coins[1] * 100 + C_coins[2])

# Create Transaction objects
trans_B = Transaction(Db, B_coins, loss_B)
trans_C = Transaction(Dc, C_coins, loss_C)

# Get initial coins
initial_coins = (A[0], A[1], A[2])

# Simulate both orders and take the maximum
order1 = (trans_B, trans_C)
order2 = (trans_C, trans_B)
count1 = simulate(initial_coins, order1[0], order1[1])
count2 = simulate(initial_coins, order2[0], order2[1])

print(max(count1, count2))
0