結果

問題 No.3051 Make All Divisible
ユーザー 👑 rin204
提出日時 2025-02-03 23:24:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 756 bytes
コンパイル時間 316 ms
コンパイル使用メモリ 82,652 KB
実行使用メモリ 77,064 KB
最終ジャッジ日時 2025-06-20 02:21:23
合計ジャッジ時間 3,483 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


def solve():
    n, K = map(int, input().split())
    A = list(map(int, input().split()))
    A.sort()
    C = [a % K for a in A]
    if sum(C) % K != 0:
        print(-1)
        return

    queue = deque()
    pos = [[] for _ in range(K)]
    for i, c in enumerate(C):
        pos[c].append(i)

    for row in pos:
        queue.extend(row)

    tot = sum(C) // K
    ma = max(C)
    cnt = K * K + 10

    while ma > tot and cnt > 0 and queue:
        i = queue.popleft()
        if A[i] == C[i]:
            continue
        cnt -= 1
        C[i] += K
        tot += 1
        ma = C[i]
        queue.append(i)

    if ma <= tot:
        print(tot)
    else:
        print(-1)


for _ in range(int(input())):
    solve()
0