結果

問題 No.968 引き算をして門松列(その3)
ユーザー gew1fw
提出日時 2025-06-12 13:25:18
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,429 bytes
コンパイル時間 184 ms
コンパイル使用メモリ 82,668 KB
実行使用メモリ 83,856 KB
最終ジャッジ日時 2025-06-12 13:31:26
合計ジャッジ時間 3,121 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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
    if b > a and b > c:
        second = max(a, c)
        return second != b
    elif b < a and b < c:
        second = min(a, c)
        return second != b
    else:
        sorted_vals = sorted([a, b, c])
        if sorted_vals[1] == a or sorted_vals[1] == c:
            return True
        return False

def compute_min_cost(A, B, C, X, Y, Z):
    min_cost = float('inf')
    
    # Check if initial condition is already valid
    if is_kadomatsu(A, B, C):
        return 0
    
    # Case 1: B' is the smallest
    # Candidate: a + b = B-1, c=0
    if B >= 1:
        a_plus_b = B - 1
        a_min = max(0, (B - C + 1))
        a_max = min(A - 2, B - 1)
        if a_min <= a_max and a_plus_b >= a_min and a_plus_b >= a_min:
            a_candidates = []
            if X <= Y:
                a = min(a_max, a_plus_b)
                a_candidates.append(a)
                if a > a_min:
                    a_candidates.append(a - 1)
            else:
                a = max(a_min, 0)
                a_candidates.append(a)
                if a < a_max:
                    a_candidates.append(a + 1)
            for a in a_candidates:
                b = a_plus_b - a
                if b < 0:
                    continue
                new_A = A - a
                new_C = C - b
                new_B = 1
                if new_A > new_B and new_C > new_B and new_A != new_C and new_A > 0 and new_C > 0:
                    cost = X * a + Y * b
                    if cost < min_cost:
                        min_cost = cost
    
    # Case 2: B' is the largest
    # Candidate 2a: Use operation2 (Y) k times
    k = max(0, C - A + 1)
    if (B - A) > k and k >= 0:
        new_B = B - k
        new_A = A
        new_C = C - k
        if new_B > 0 and new_A > 0 and new_C > 0 and new_A < new_B and new_C < new_B and new_A != new_C:
            cost = Y * k
            if cost < min_cost:
                min_cost = cost
    
    # Candidate 2b: Use operation1 (X) k times
    k = max(0, A - C + 1)
    if (B - C) > k and k >= 0:
        new_B = B - k
        new_A = A - k
        new_C = C
        if new_B > 0 and new_A > 0 and new_C > 0 and new_A < new_B and new_C < new_B and new_A != new_C:
            cost = X * k
            if cost < min_cost:
                min_cost = cost
    
    # Candidate 2c: Use operation3 (Z) k times
    k = max(A - B + 1, C - B + 1, 0)
    if k < min(A, C) and A != C:
        new_B = B
        new_A = A - k
        new_C = C - k
        if new_A > 0 and new_C > 0 and new_A < new_B and new_C < new_B:
            cost = Z * k
            if cost < min_cost:
                min_cost = cost
    
    # Candidate 2d: Try k=0 for operation2
    new_B = B
    new_A = A
    new_C = C
    if new_A < new_B and new_C < new_B and new_A != new_C:
        cost = 0
        if cost < min_cost:
            min_cost = cost
    
    # Candidate 2e: Try k=0 for operation1
    # Same as 2d
    
    # Candidate 2f: Try k=0 for operation3
    # Same as 2d
    
    # Other possible candidates with single operations
    # Try using operation2 once
    new_B = B - 1
    new_A = A
    new_C = C - 1
    if new_B > 0 and new_A > 0 and new_C > 0:
        if is_kadomatsu(new_A, new_B, new_C):
            cost = Y * 1
            if cost < min_cost:
                min_cost = cost
    
    # Try using operation1 once
    new_B = B - 1
    new_A = A - 1
    new_C = C
    if new_B > 0 and new_A > 0 and new_C > 0:
        if is_kadomatsu(new_A, new_B, new_C):
            cost = X * 1
            if cost < min_cost:
                min_cost = cost
    
    # Try using operation3 once
    new_B = B
    new_A = A - 1
    new_C = C - 1
    if new_B > 0 and new_A > 0 and new_C > 0:
        if is_kadomatsu(new_A, new_B, new_C):
            cost = Z * 1
            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 __name__ == "__main__":
    main()
0