# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1134587 | Halym2007 | Gift (IZhO18_nicegift) | C++17 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define sz size()
#define ff first
#define ss second
#define pb push_back
#define pii pair <ll, ll>
#define dur exit(0)
#define dur1 return(0)
const int N = 2e6 + 5;
ll n, k, a[N];
vector <vector <ll>> v;
int main () {
// freopen ("input.txt", "r", stdin);
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> k;
ll sum = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
sum += a[i];
}
if (sum % k != 0) {
return cout << "-1", 0;
}
for (ll i = 1; i <= n; ++i) {
if (sum / k < a[i]) {
return cout << "-1", 0;
}
queue <pii> q[k + 1];
int san = 1, val = 0;
for (ll i = 1; i <= n; ++i) {
if (val + a[i] <= sum / k) {
q[san].push ({a[i], i});
val += a[i];
if (val == sum / k) {
san++;
val = 0;
}
}
else {
ll gosh = sum / k - val;
q[san].push({gosh, i});
san++;
val = a[i] - gosh;
q[san].push({val, i});
}
}
while (1) {
vector <ll> kk;
ll oo = 0;
ll mn = 2e18;
for (ll i = 1; i <= k; ++i) {
if (q[i].empty()) {
oo++;
continue;
}
mn = min(q[i].front().ff, mn);
}
if (oo) {
assert (oo == k);
break;
}
kk.pb (mn);
for (ll i = 1; i <= k; ++i) {
kk.pb (q[i].front().ss);
q[i].front().ff -= mn;
if (!q[i].front().ff) {
q[i].pop();
}
}
v.pb (kk);
}
assert ((ll)v.sz <= n);
cout << (ll)v.sz << "\n";
for (int i = 0; i < (ll)v.sz; ++i) {
for (ll j : v[i]) {
cout << j << " ";
}
cout << "\n";
}
}