#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define all(x) x.begin(), x.end()
#define int long long
#define pq priority_queue
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pp pop_back
#define F first
#define S second
using namespace std;
void solve () {
int n, k; cin >> n >> k;
pq<pair<int, int>> q;
vector<int> a(n+1);
int sum = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
q.push({a[i], i});
}
if (sum % k) {cout << "-1"; return ;}
if (q.top().F > sum / k) {cout << "-1"; return ;}
vector<vector<int>> ans;
while (sum) {
vector<int> v;
int s = INT_MAX;
for (int i = 1; i <= k; i++) {
v.pb(q.top().S);
s = q.top().F;
q.pop();
}
if (!q.empty()) s = min(s, (sum / k) - q.top().F);
sum -= k * s;
vector<int> t; t.pb(s);
for (auto i: v) {
t.pb(i);
a[i] -= s;
if (a[i]) q.push({a[i], i});
}
ans.pb(t);
}
cout << ans.size() << '\n';
for (auto v: ans) {
for (auto i: v) cout << i << ' ';
cout << '\n';
}
}
signed main() {IOS solve(); return 0;}
# | 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... |