#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define tr(i, c) for(auto i = c.begin(); i != c.end(); ++i)
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int N = 1e6+1;
const ll INF = 1e18+1;
ll v[N];
int main(){
int n, k;
scanf("%d%d", &n, &k);
ll sm=0, mx=0;
for(int i = 1; i <= n; ++i)
scanf("%lld", v+i), umax(mx, v[i]), sm+=v[i];
if(sm%k!=0 or mx>sm/k){
puts("-1");
return 0;
}
ll lim=sm/k, now=0;
int pk=1;
vector<deque<pli>> box(k+1);
for(int i = 1; i <= n; ++i){
if(lim==now)
box[++pk].pb({v[i], i}), now=v[i];
else if(now+v[i]<=lim)
box[pk].pb({v[i], i}), now+=v[i];
else
box[pk].pb({lim-now, i}), now=v[i]-lim+now, box[++pk].pb({now, i});
}
assert(pk==k);
vector<vector<ll>> ans;
while(1){
ll mn=INF;
bool did=0;
for(int i = 1; i <= pk; ++i){
if(box[i].empty()){
did=1;
break;
}
umin(mn, box[i][0].ff);
}
if(did)
break;
ans.pb({mn});
for(int i = 1; i <= pk; ++i){
box[i][0].ff-=mn;
ans.back().pb(box[i][0].ss);
if(box[i][0].ff==0)
box[i].pop_front();
}
}
printf("%d\n", (int)ans.size());
for(auto i : ans){
tr(ii, i)
printf("%lld ", *ii);
puts("");
}
return 0;
}
Compilation message (stderr)
nicegift.cpp: In function 'int main()':
nicegift.cpp:21:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf("%d%d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~
nicegift.cpp:24:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | scanf("%lld", v+i), umax(mx, v[i]), sm+=v[i];
| ~~~~~^~~~~~~~~~~~~
# | 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... |