結果

問題 No.3051 Make All Divisible
ユーザー suisen
提出日時 2025-01-18 16:05:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,260 bytes
コンパイル時間 613 ms
コンパイル使用メモリ 80,720 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2025-02-08 15:03:31
合計ジャッジ時間 2,175 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14 WA * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

// WA

#include <iostream>
#include <limits>
#include <numeric>
#include <queue>
#include <vector>


void solve(int n, int k, std::vector<long long> a) {
    if (std::accumulate(a.begin(), a.end(), 0LL) % k != 0) {
        std::cout << -1 << '\n';
        return;
    }

    std::priority_queue<long long> pq;

    const long long threshold = k * (k - 1) / 2 - 1;
    for (auto &e : a) {
        const long long q = (std::max(0LL, e - threshold) + k - 1) / k;
        e -= q * k;
        pq.push(e);
    }

    constexpr int inf = std::numeric_limits<int>::max();

    int ans = inf;

    int r = std::accumulate(a.begin(), a.end(), 0LL) / k;
    for (;; --r) {
        long long e = pq.top();
        if (e <= r) ans = std::min(ans, r);
        pq.pop();
        e -= k;
        if (e < 0) break;
        pq.push(e);
    }

    if (ans == inf) {
        std::cout << -1 << '\n';
    } else {
        std::cout << ans << '\n';
    }
}


int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;

    while (t--) {
        int n, k;
        std::cin >> n >> k;

        std::vector<long long> a(n);
        for (int i = 0; i < n; ++i) {
            std::cin >> a[i];
        }

        solve(n, k, a);
    }
}
0