結果

問題 No.3051 Make All Divisible
ユーザー rin204
提出日時 2025-02-02 15:36:48
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,215 bytes
コンパイル時間 229 ms
コンパイル使用メモリ 82,600 KB
実行使用メモリ 77,296 KB
最終ジャッジ日時 2025-02-08 15:04:18
合計ジャッジ時間 12,789 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21 WA * 9 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    def ok(x):
        tot = 0
        ma = 0
        for a, c in zip(A, C):
            d = (x - c) // K * K + c
            d = min(d, a)
            tot += d
            ma = max(ma, d)
            if d < 0:
                return False

        return x <= tot // K

    rr = A[-K] - 1 + K

    inf = 1 << 62
    ans = inf
    for d in range(K):
        ma = (rr - d) // K * K + d
        if not ok(ma):
            continue

        r = ma // K
        l = -1
        while r - l > 1:
            mid = (l + r) // 2
            if ok(mid * K + d):
                r = mid
            else:
                l = mid

        r = r * K + d
        tot = 0
        mac = 0
        for a, c in zip(A, C):
            d = (r - c) // K * K + c
            d = min(d, a)
            tot += d
            if d == r:
                mac += 1

        tot //= K
        tmp = max(r, tot - mac)
        ans = min(ans, tmp)

    if ans == inf:
        print(-1)
    else:
        print(ans)


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