結果

問題 No.968 引き算をして門松列(その3)
ユーザー lam6er
提出日時 2025-04-16 15:44:20
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,624 bytes
コンパイル時間 320 ms
コンパイル使用メモリ 81,744 KB
実行使用メモリ 67,236 KB
最終ジャッジ日時 2025-04-16 15:47:18
合計ジャッジ時間 3,785 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other TLE * 1 -- * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def is_kadomatsu(a, b, c):
    if a == b or b == c or a == c:
        return False
    sorted_vals = sorted([a, b, c])
    second = sorted_vals[1]
    return second == a or second == c

def compute_min_cost(A, B, C, X, Y, Z):
    min_cost = float('inf')
    
    # Check original
    if is_kadomatsu(A, B, C):
        return 0
    
    # Structure 1: b' > a' > c'
    # Try various possibilities, e.g., m and k
    # We can try m from 0 to min(B, C)
    for m in range(0, min(B, C) + 1):
        for k in range(0, min(A, B - m) + 1):
            if m < (C - A + k) + 1:
                continue
            n_min = max(0, (A - B + m) + 1)
            n_max = min(A - k - 1, C - m - 1)
            if n_min > n_max:
                continue
            n = n_min
            a_new = A - k - n
            b_new = B - k - m
            c_new = C - m - n
            if a_new <= 0 or b_new <= 0 or c_new <= 0:
                continue
            if not (b_new > a_new and a_new > c_new):
                continue
            cost = k * X + m * Y + n * Z
            if cost < min_cost:
                min_cost = cost
    
    # Structure 2: b' > c' > a'
    for m in range(0, min(B, C) + 1):
        for k in range(0, min(A, B - m) + 1):
            if m < (C - A + k):
                continue
            n_min = max(0, (A - B + m) + 1)
            n_max = min(A - k - 1, C - m - 1)
            if n_min > n_max:
                continue
            n = n_min
            a_new = A - k - n
            b_new = B - k - m
            c_new = C - m - n
            if a_new <= 0 or b_new <= 0 or c_new <= 0:
                continue
            if not (b_new > c_new and c_new > a_new):
                continue
            cost = k * X + m * Y + n * Z
            if cost < min_cost:
                min_cost = cost
    
    # Structure 3: a' > c' > b'
    for k in range(0, min(A, B) + 1):
        for n in range(0, min(A - k, C) + 1):
            m_min = max(0, (C - A + k) + 1)
            m_max = min(B - k - 1, C - n - 1)
            if m_min > m_max:
                continue
            m = m_min
            a_new = A - k - n
            b_new = B - k - m
            c_new = C - m - n
            if a_new <= 0 or b_new <= 0 or c_new <= 0:
                continue
            if not (a_new > c_new and c_new > b_new):
                continue
            cost = k * X + m * Y + n * Z
            if cost < min_cost:
                min_cost = cost
    
    # Structure 4: c' > a' > b'
    for n in range(0, min(A, C) + 1):
        for m in range(0, min(C - n, B) + 1):
            k_min = max(0, (A - B + m) + 1)
            k_max = min(A - n - 1, B - m - 1)
            if k_min > k_max:
                continue
            k = k_min
            a_new = A - k - n
            b_new = B - k - m
            c_new = C - m - n
            if a_new <= 0 or b_new <= 0 or c_new <= 0:
                continue
            if not (c_new > a_new and a_new > b_new):
                continue
            cost = k * X + m * Y + n * Z
            if cost < min_cost:
                min_cost = cost
    
    return min_cost if min_cost != float('inf') else -1

def main():
    input = sys.stdin.read().split()
    idx = 0
    T = int(input[idx])
    idx +=1
    for _ in range(T):
        A = int(input[idx])
        B = int(input[idx+1])
        C = int(input[idx+2])
        X = int(input[idx+3])
        Y = int(input[idx+4])
        Z = int(input[idx+5])
        idx +=6
        res = compute_min_cost(A, B, C, X, Y, Z)
        print(res if res != float('inf') else -1)

if __name__ == '__main__':
    main()
0