結果

問題 No.968 引き算をして門松列(その3)
ユーザー lam6er
提出日時 2025-04-16 16:24:00
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 6,737 bytes
コンパイル時間 347 ms
コンパイル使用メモリ 81,628 KB
実行使用メモリ 83,552 KB
最終ジャッジ日時 2025-04-16 16:24:42
合計ジャッジ時間 2,359 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 1 WA * 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):
    if is_kadomatsu(A, B, C):
        return 0
    min_cost = float('inf')
    
    # Case 1: B is max, A is second, C is min (B > A > C)
    # Try using operation Y (B and C)
    lower = max(C - A + 1, 0)
    upper = min(B - A - 1, C - 1, B - 1)
    if lower <= upper:
        k = lower
        cost = Y * k
        new_B = B - k
        new_A = A
        new_C = C - k
        if new_B > new_A and new_A > new_C and new_B > 0 and new_C > 0 and new_A > 0:
            if is_kadomatsu(new_A, new_B, new_C):
                min_cost = min(min_cost, cost)
    # Try using operation Z (A and C)
    if A > C:
        lower_k = max(A - B + 1, 0)
        upper_k = min(A - 1, C - 1)
        if lower_k <= upper_k:
            k = lower_k
            new_A_val = A - k
            new_C_val = C - k
            new_B_val = B
            if new_B_val > new_A_val and new_A_val > new_C_val and new_A_val > 0 and new_C_val > 0 and new_B_val > 0:
                if is_kadomatsu(new_A_val, new_B_val, new_C_val):
                    cost = Z * k
                    min_cost = min(min_cost, cost)
    # Case 2: B is max, C is second, A is min (B > C > A)
    # Try using operation Y (B and C)
    lower = max(A - C + 1, 0)
    upper = min(B - C - 1, A - 1, B - 1)
    if lower <= upper:
        k = lower
        cost = Y * k
        new_B = B - k
        new_C_val = C - k
        new_A_val = A
        if new_B > new_C_val and new_C_val > new_A_val and new_B > 0 and new_A_val > 0 and new_C_val > 0:
            if is_kadomatsu(new_A_val, new_B, new_C_val):
                min_cost = min(min_cost, cost)
    # Try using operation X (A and B)
    if C > A:
        lower_k = max(C - B + 1, 0)
        upper_k = min(C - 1, A - 1)
        if lower_k <= upper_k:
            k = lower_k
            new_A_val = A - k
            new_B_val = B - k
            new_C_val = C
            if new_B_val > new_C_val and new_C_val > new_A_val and new_A_val > 0 and new_B_val > 0 and new_C_val > 0:
                if is_kadomatsu(new_A_val, new_B_val, new_C_val):
                    cost = X * k
                    min_cost = min(min_cost, cost)
    # Case 3: B is min, A is max, C is second (A > C > B)
    # Try using operation X (A and B)
    lower = max(B - C + 1, 0)
    upper = min(A - C - 1, B - 1, A - 1)
    if lower <= upper:
        k = lower
        cost = X * k
        new_A_val = A - k
        new_B_val = B - k
        new_C_val = C
        if new_A_val > new_C_val and new_C_val > new_B_val and new_A_val > 0 and new_B_val > 0 and new_C_val > 0:
            if is_kadomatsu(new_A_val, new_B_val, new_C_val):
                min_cost = min(min_cost, cost)
    # Try using operation Z (A and C)
    if C > B:
        lower_k = max(C - A + 1, 0)
        upper_k = min(C - 1, B - 1)
        if lower_k <= upper_k:
            k = lower_k
            new_A_val = A - k
            new_C_val = C - k
            new_B_val = B
            if new_A_val > new_C_val and new_C_val > new_B_val and new_A_val > 0 and new_B_val > 0 and new_C_val > 0:
                if is_kadomatsu(new_A_val, new_B_val, new_C_val):
                    cost = Z * k
                    min_cost = min(min_cost, cost)
    # Case 4: B is min, C is max, A is second (C > A > B)
    # Try using operation Y (B and C)
    lower = max(B - A + 1, 0)
    upper = min(C - A - 1, B - 1, C - 1)
    if lower <= upper:
        k = lower
        cost = Y * k
        new_B_val = B - k
        new_C_val = C - k
        new_A_val = A
        if new_C_val > new_A_val and new_A_val > new_B_val and new_C_val > 0 and new_A_val > 0 and new_B_val > 0:
            if is_kadomatsu(new_A_val, new_B_val, new_C_val):
                min_cost = min(min_cost, cost)
    # Try using operation Z (A and C)
    if A > B:
        lower_k = max(A - C + 1, 0)
        upper_k = min(A - 1, B - 1)
        if lower_k <= upper_k:
            k = lower_k
            new_A_val = A - k
            new_C_val = C - k
            new_B_val = B
            if new_C_val > new_A_val and new_A_val > new_B_val and new_C_val > 0 and new_A_val > 0 and new_B_val > 0:
                if is_kadomatsu(new_A_val, new_B_val, new_C_val):
                    cost = Z * k
                    min_cost = min(min_cost, cost)
    # Case 5: A is max, C is second, B is mid (A > C > B)
    # Try using operation Z (A and C)
    lower_k = 0
    upper_k = min(A - 1, C - 1)
    possible = False
    if A > C and C > B:
        possible = True
    else:
        diff_A_C = A - C
        diff_C_B = C - B
        if diff_A_C >= 0 and diff_C_B >= 0:
            upper_k = min(upper_k, diff_A_C, diff_C_B)
            possible = True
    if possible:
        for k in [0, 1]:
            if k > upper_k:
                continue
            new_A_val = A - k
            new_C_val = C - k
            new_B_val = B
            if new_A_val > new_C_val and new_C_val > new_B_val and new_A_val > 0 and new_C_val > 0 and new_B_val > 0:
                if is_kadomatsu(new_A_val, new_B_val, new_C_val):
                    cost = Z * k
                    min_cost = min(min_cost, cost)
    # Case 6: C is max, A is second, B is mid (C > A > B)
    # Try using operation Z (A and C)
    lower_k = 0
    upper_k = min(C - 1, A - 1)
    possible = False
    if C > A and A > B:
        possible = True
    else:
        diff_C_A = C - A
        diff_A_B = A - B
        if diff_C_A >= 0 and diff_A_B >= 0:
            upper_k = min(upper_k, diff_C_A, diff_A_B)
            possible = True
    if possible:
        for k in [0, 1]:
            if k > upper_k:
                continue
            new_A_val = A - k
            new_C_val = C - k
            new_B_val = B
            if new_C_val > new_A_val and new_A_val > new_B_val and new_C_val > 0 and new_A_val > 0 and new_B_val > 0:
                if is_kadomatsu(new_A_val, new_B_val, new_C_val):
                    cost = Z * k
                    min_cost = min(min_cost, cost)
    # Check other possible combinations
    # Additional cases can be added here if necessary
    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
        print(compute_min_cost(A, B, C, X, Y, Z))

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