結果

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

ソースコード

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])
    mid = sorted_vals[1]
    return mid == A or mid == C

def compute_case1(A, B, C, X, Y, Z):
    min_cost = float('inf')
    max_z = min(A-1, C-1)
    for z in [0, max_z]:
        max_y_upper = B - A + z - 1
        max_y = min(max_y_upper, C-1 - z, B-1)
        max_y = min(max_y, B - A + z -1)
        max_y = max(max_y, 0)
        if max_y < 0:
            continue
        for y in [0, max_y]:
            if y < 0:
                continue
            x_upper1 = A - 1 - z
            x_upper2 = B - 1 - y
            x_upper3 = A - C + y - 1
            x_max = min(x_upper1, x_upper2, x_upper3)
            if x_max < 0:
                continue
            for x in [0, x_max]:
                a = A - x - z
                b = B - x - y
                c = C - y - z
                if a <= 0 or b <= 0 or c <= 0:
                    continue
                if c < a and a < b:
                    cost = X * x + Y * y + Z * z
                    if cost < min_cost:
                        min_cost = cost
    return min_cost

def compute_case2(A, B, C, X, Y, Z):
    min_cost = float('inf')
    max_z = min(A-1, B-1)
    for z in [0, max_z]:
        max_y_upper = C - A + z - 1
        max_y = min(max_y_upper, B-1 - z, C-1)
        max_y = min(max_y, C - A + z -1)
        max_y = max(max_y, 0)
        if max_y < 0:
            continue
        for y in [0, max_y]:
            if y < 0:
                continue
            x_upper1 = A - 1 - z
            x_upper2 = C - 1 - y
            x_upper3 = A - B + y - 1
            x_max = min(x_upper1, x_upper2, x_upper3)
            if x_max < 0:
                continue
            for x in [0, x_max]:
                a = A - x - z
                b = B - x - y
                c = C - y - z
                if a <= 0 or b <= 0 or c <= 0:
                    continue
                if b < a and a < c:
                    cost = X * x + Y * y + Z * z
                    if cost < min_cost:
                        min_cost = cost
    return min_cost

def compute_case3(A, B, C, X, Y, Z):
    min_cost = float('inf')
    max_x = min(C-1, B-1)
    for x in [0, max_x]:
        max_z_upper = A - B + x - 1
        max_z = min(max_z_upper, B-1 - x, A-1)
        max_z = min(max_z, A - B + x -1)
        max_z = max(max_z, 0)
        if max_z < 0:
            continue
        for z in [0, max_z]:
            if z < 0:
                continue
            y_upper1 = C - 1 - x
            y_upper2 = B - 1 - z
            y_upper3 = C - A + z - 1
            y_max = min(y_upper1, y_upper2, y_upper3)
            if y_max < 0:
                continue
            for y in [0, y_max]:
                a = A - x - z
                b = B - x - y
                c = C - y - z
                if a <= 0 or b <= 0 or c <= 0:
                    continue
                if b < c and c < a:
                    cost = X * x + Y * y + Z * z
                    if cost < min_cost:
                        min_cost = cost
    return min_cost

def compute_case4(A, B, C, X, Y, Z):
    min_cost = float('inf')
    max_x = min(C-1, A-1)
    for x in [0, max_x]:
        max_z_upper = B - C + x - 1
        max_z = min(max_z_upper, A-1 - x, B-1)
        max_z = min(max_z, B - C + x -1)
        max_z = max(max_z, 0)
        if max_z < 0:
            continue
        for z in [0, max_z]:
            if z < 0:
                continue
            y_upper1 = C - 1 - x
            y_upper2 = B - 1 - z
            y_upper3 = C - A + z - 1
            y_max = min(y_upper1, y_upper2, y_upper3)
            if y_max < 0:
                continue
            for y in [0, y_max]:
                a = A - x - z
                b = B - x - y
                c = C - y - z
                if a <= 0 or b <= 0 or c <= 0:
                    continue
                if a < c and c < b:
                    cost = X * x + Y * y + Z * z
                    if cost < min_cost:
                        min_cost = cost
    return min_cost

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')
        
        cost1 = compute_case1(A, B, C, X, Y, Z)
        if cost1 < min_cost:
            min_cost = cost1
        
        cost2 = compute_case2(A, B, C, X, Y, Z)
        if cost2 < min_cost:
            min_cost = cost2
        
        cost3 = compute_case3(A, B, C, X, Y, Z)
        if cost3 < min_cost:
            min_cost = cost3
        
        cost4 = compute_case4(A, B, C, X, Y, Z)
        if cost4 < min_cost:
            min_cost = cost4
        
        if min_cost == float('inf'):
            print(-1)
        else:
            print(min_cost)

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