This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//I wrote this code 4 u today
#include <bits/stdc++.h>
const int MAX_MEM = 5e8;
size_t mpos = 0;
char mem[MAX_MEM];
void *operator new(size_t n) {
return (void *) (mem + (mpos += n) - n);
}
void operator delete(void *) noexcept {}
#define vc vector
#define nd node*
#define pnd pair<nd, nd>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef vc<pll> vpll;
typedef vc<vll> vvll;
typedef vc<vpll> vvpll;
template<const ll MOD>
struct mod_mul : std::multiplies<const ll> {
ll operator()(const ll a, const ll b) {
return (a * b) % MOD;
}
};
template<typename T>
inline void sort(T &a) {
sort(a.begin(), a.end());
}
template<typename T>
inline void unique(T &a) {
a.resize(unique(a.begin(), a.end()) - a.begin());
}
template<typename T>
inline void reverse(T &a) {
reverse(a.begin(), a.end());
}
const ll INF = 9023372036854775808ll;
const ll MOD = 1000000007ll;
#define int ll
int32_t main() {
cin.tie(nullptr)->ios_base::sync_with_stdio(false);
int n, k;
cin >> n >> k;
vc<ll> a(n);
ll sm = 0, mx = 0;
for (auto &i : a) cin >> i, sm += i, mx = max(mx, i);
if (sm % k || mx > sm / k) return cout << "-1\n", 0;
set<pair<int, int>> alive;
for (int i = 0; i < n; ++i) if (a[i]) alive.emplace(a[i], i);
vc<vc<int>> ans;
while (!alive.empty()) {
vc<int> inds(k);
for (int i = k - 1; i >= 0; --i) inds[i] = alive.rbegin()->second, alive.erase(prev(alive.end()));
if (!a[inds.back()]) break;
int d = min(sm / k - (alive.empty() ? 0 : alive.rbegin()->first), a[inds[0]]);
ans.push_back({d});
for (auto i : inds) ans.back().push_back(i + 1), a[i] -= d, alive.emplace(a[i], i);
sm -= k * d;
}
cout << ans.size() << '\n';
for (auto v : ans) {
for (auto &i : v) cout << i << ' ';
cout << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |