This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
#define boost ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
const int inf = INT_MAX;
const int mod = 998244353;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
const int N = 1e6+5;
const int M = 1e2+1;
ll a[N],pos[N];
int main(){
	boost;
	int n,k;
	cin>>n>>k;
	ll sum=0,mx=0;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum+=a[i];
		mx=max(mx,a[i]);	
	}
	ll part=sum/k;
	if(sum%k||mx>part){
		cout<<"-1";
		return 0;
	}
	sum=0;
	vector< vector<pli> >q;
	vector<pli>v;
	for(int i=1;i<=n;i++){
		sum+=a[i];
		if(sum>=part){
			v.pb(mp(a[i]-(sum-part),i));
			q.pb(v);
			v.clear();
			sum-=part;
			if(sum)v.pb(mp(sum,i));
		}
		else v.pb(mp(a[i],i));
	}
	vector<ll>ans;	
	while(1){
		ll mn=1e18;
		for(int i=0;i<k;i++){
			mn=min(mn,q[i][pos[i]].x);
		}
		//cout<<mn<<" ";
		ans.pb(mn);
		bool w=0;
		for(int i=0;i<k;i++){
			//cout<<q[i][pos[i]].y<<" ";
			ans.pb(q[i][pos[i]].y);
			q[i][pos[i]].x-=mn;
			if(pos[i]<sz(q[i])&&!q[i][pos[i]].x){
				pos[i]++;
				if(pos[i]<sz(q[i]))w++;
			}
		}
		if(!w)break;
	}
	cout<<sz(ans)/(k+1)<<endl;
	for(int i=0;i<sz(ans);i++){
		cout<<ans[i]<<" ";
		if((i+1)%(k+1)==0)cout<<endl;
	}
}
Compilation message (stderr)
nicegift.cpp: In function 'int main()':
nicegift.cpp:74:25: warning: use of an operand of type 'bool' in 'operator++' is deprecated [-Wdeprecated]
     if(pos[i]<sz(q[i]))w++;
                         ^~| # | 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... |