結果
問題 | No.3051 Make All Divisible |
ユーザー |
![]() |
提出日時 | 2025-03-08 00:21:50 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 214 ms / 2,000 ms |
コード長 | 1,044 bytes |
コンパイル時間 | 6,069 ms |
コンパイル使用メモリ | 332,940 KB |
実行使用メモリ | 8,608 KB |
最終ジャッジ日時 | 2025-03-08 00:21:59 |
合計ジャッジ時間 | 8,898 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 31 |
ソースコード
#include <atcoder/all>#include <bits/stdc++.h>#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)using namespace atcoder;using namespace std;typedef long long ll;void solve() {int n, k;cin >> n >> k;vector<ll> a(n);rep(i, 0, n) cin >> a[i];{ll sm = 0;rep(i, 0, n) { sm += a[i]; }if (sm % k != 0) {cout << "-1\n";return;}}priority_queue<int> pq;ll sm = 0;rep(i, 0, n) {a[i] = min(a[i], k * k + a[i] % k);pq.push(a[i]);sm += a[i];}ll ans = 1e9;while (1) {if ((ll)pq.top() <= sm / k) {ans = min(ans, sm / k);}int tp = pq.top();pq.pop();if (tp < k)break;sm -= k;pq.push(tp - k);}if (ans == 1e9)ans = -1;cout << ans << '\n';}int main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int t;cin >> t;while (t--) {solve();}}