// correct #include #include #include #include #include void solve(int n, int k, std::vector a) { if (std::accumulate(a.begin(), a.end(), 0LL) % k != 0) { std::cout << -1 << '\n'; return; } std::priority_queue pq; const long long threshold = k * (k - 1) / 2; 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::max(); int ans = inf; int t = std::accumulate(a.begin(), a.end(), 0LL) / k; for (;; --t) { long long e = pq.top(); if (e <= t) ans = t; 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 a(n); for (int i = 0; i < n; ++i) { std::cin >> a[i]; } solve(n, k, a); } }