結果

問題 No.2006 Decrease All to Zero
ユーザー gew1fw
提出日時 2025-06-12 16:49:02
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,406 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 76,544 KB
最終ジャッジ日時 2025-06-12 16:50:05
合計ジャッジ時間 12,218 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 6 WA * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr += 1
    X = list(map(int, input[ptr:ptr+N]))
    ptr += N
    A = list(map(int, input[ptr:ptr+N]))
    ptr += N

    # Check feasibility
    total = sum(A)
    possible = True
    for i in range(N):
        if A[i] > 1 + (total - A[i]):
            possible = False
            break
    if not possible:
        print(-1)
        return

    # Initialize
    current = None
    min_x = float('inf')
    min_idx = -1
    for i in range(N):
        if A[i] > 0 and X[i] < min_x:
            min_x = X[i]
            min_idx = i
    if min_idx == -1:
        print(0)
        return
    current = min_idx
    total_distance = 0
    A[current] -= 1

    while True:
        found = False
        min_dist = float('inf')
        next_button = -1
        for j in range(N):
            if j == current:
                continue
            if A[j] > 0:
                dist = abs(X[j] - X[current])
                if dist < min_dist:
                    min_dist = dist
                    next_button = j
        if next_button == -1:
            break
        total_distance += min_dist
        current = next_button
        A[current] -= 1

    # Check if all A are zero
    if all(a == 0 for a in A):
        print(total_distance)
    else:
        print(-1)

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