結果

問題 No.968 引き算をして門松列(その3)
ユーザー gew1fw
提出日時 2025-06-12 15:02:15
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 5,055 bytes
コンパイル時間 284 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 64,896 KB
最終ジャッジ日時 2025-06-12 15:02:32
合計ジャッジ時間 3,834 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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])
    middle = sorted_vals[1]
    return middle == A or middle == C

def try_op1(A, B, C, X):
    min_cost = float('inf')
    max_k = min(A-1, B-1)
    for k in range(0, max_k + 1):
        a = A - k
        b = B - k
        c = C
        if a <= 0 or b <= 0 or c <= 0:
            continue
        if a == b or b == c or a == c:
            continue
        if (b - a) * (c - a) < 0:
            cost = k * X
            if cost < min_cost:
                min_cost = cost
    return min_cost if min_cost != float('inf') else None

def try_op2(A, B, C, Y):
    min_cost = float('inf')
    max_k = min(B-1, C-1)
    for k in range(0, max_k + 1):
        a = A
        b = B - k
        c = C - k
        if a <= 0 or b <= 0 or c <= 0:
            continue
        if a == b or b == c or a == c:
            continue
        if (b - a) * (c - a) < 0:
            cost = k * Y
            if cost < min_cost:
                min_cost = cost
    return min_cost if min_cost != float('inf') else None

def try_op3(A, B, C, Z):
    min_cost = float('inf')
    max_k = min(A-1, C-1)
    for k in range(0, max_k + 1):
        a = A - k
        b = B
        c = C - k
        if a <= 0 or b <= 0 or c <= 0:
            continue
        if a == b or b == c or a == c:
            continue
        if (a - c) * (b - c) < 0:
            cost = k * Z
            if cost < min_cost:
                min_cost = cost
    return min_cost if min_cost != float('inf') else None

def try_op1_op3(A, B, C, X, Z):
    min_cost = float('inf')
    max_k1 = A - 1
    max_k3 = C - 1
    for k1 in range(0, max_k1 + 1):
        for k3 in range(0, max_k3 + 1):
            a = A - k1 - k3
            b = B - k1
            c = C - k3
            if a <= 0 or b <= 0 or c <= 0:
                continue
            if a == b or b == c or a == c:
                continue
            sorted_vals = sorted([a, b, c])
            middle = sorted_vals[1]
            if middle == a or middle == c:
                cost = k1 * X + k3 * Z
                if cost < min_cost:
                    min_cost = cost
    return min_cost if min_cost != float('inf') else None

def try_op2_op3(A, B, C, Y, Z):
    min_cost = float('inf')
    max_k2 = min(B-1, C-1)
    max_k3 = C - 1
    for k2 in range(0, max_k2 + 1):
        for k3 in range(0, max_k3 + 1):
            a = A - k3
            b = B - k2
            c = C - k2 - k3
            if a <= 0 or b <= 0 or c <= 0:
                continue
            if a == b or b == c or a == c:
                continue
            sorted_vals = sorted([a, b, c])
            middle = sorted_vals[1]
            if middle == a or middle == c:
                cost = k2 * Y + k3 * Z
                if cost < min_cost:
                    min_cost = cost
    return min_cost if min_cost != float('inf') else None

def try_op1_op2(A, B, C, X, Y):
    min_cost = float('inf')
    max_k1 = min(A-1, B-1)
    max_k2 = B - 1
    for k1 in range(0, max_k1 + 1):
        for k2 in range(0, max_k2 + 1):
            a = A - k1
            b = B - k1 - k2
            c = C - k2
            if a <= 0 or b <= 0 or c <= 0:
                continue
            if a == b or b == c or a == c:
                continue
            sorted_vals = sorted([a, b, c])
            middle = sorted_vals[1]
            if middle == a or middle == c:
                cost = k1 * X + k2 * Y
                if cost < min_cost:
                    min_cost = cost
    return min_cost if min_cost != float('inf') else None

def solve():
    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
        
        if is_kadomatsu(A, B, C):
            print(0)
            continue
        
        min_cost = float('inf')
        
        cost_op1 = try_op1(A, B, C, X)
        if cost_op1 is not None:
            min_cost = min(min_cost, cost_op1)
        
        cost_op2 = try_op2(A, B, C, Y)
        if cost_op2 is not None:
            min_cost = min(min_cost, cost_op2)
        
        cost_op3 = try_op3(A, B, C, Z)
        if cost_op3 is not None:
            min_cost = min(min_cost, cost_op3)
        
        cost_op1_op3 = try_op1_op3(A, B, C, X, Z)
        if cost_op1_op3 is not None:
            min_cost = min(min_cost, cost_op1_op3)
        
        cost_op2_op3 = try_op2_op3(A, B, C, Y, Z)
        if cost_op2_op3 is not None:
            min_cost = min(min_cost, cost_op2_op3)
        
        cost_op1_op2 = try_op1_op2(A, B, C, X, Y)
        if cost_op1_op2 is not None:
            min_cost = min(min_cost, cost_op1_op2)
        
        if min_cost != float('inf'):
            print(min_cost)
        else:
            print(-1)

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