#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 (int 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 (int 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 {
int gosh = sum / k - val;
q[san].push({gosh, i});
san++;
val = a[i] - gosh;
q[san].push({val, i});
}
}
// for (int i = 1; i <= k; ++i) {
// cout << i << " --- > \n";
// while (!q[i].empty()) {
// cout << q[i].front().ff << " " << q[i].front().ss << "\n";
// q[i].pop();
// }
// cout << "\n";
// }
// cout << "\n\n\n";
while (1) {
vector <ll> kk;
int oo = 0;
ll mn = 2e18;
for (int i = 1; i <= k; ++i) {
if (q[i].empty()) {
oo = 1;
break;
}
// cout << "men -> " << q[i].front().ff << "\n";
mn = min(q[i].front().ff, mn);
}
// cout << "jj --- > " << mn;
// dur;
if (oo) break;
kk.pb (mn);
for (int i = 1; i <= k; ++i) {
kk.pb (q[i].front().ss);
q[i].front().ff -= mn;
if (!q[i].front().ff) {
q[i].pop();
}
}
// cout << "menow\n";
// for (ll j : kk) {
// cout << j << " ";
// }
// dur1;
v.pb (kk);
}
cout << (int)v.sz << "\n";
for (int i = 0; i < (int)v.sz; ++i) {
for (ll j : v[i]) {
cout << j << " ";
}
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... |