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>
using namespace std;
#define int long long
#define rep(n) rep1(i,n)
#define rep1(i,n) rep2(i,0,n)
#define rep2(i,a,b) for(int i=a; i<b; ++i)
#define rep3(i,a,b) for(int i=a; i>=b; --i)
#define sz(a) ((int)a.size())
#define all(a) a.begin(),a.end()
#define pow2(x) (1ll<<(x))
#define pii pair<int,int>
#define pb push_back
#ifdef i_am_noob
#define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x){cerr << x << endl;}
template<typename T, typename ...S> void _do(T && x, S&&... y){cerr << x << ' ',_do(y...);}
#else
#define bug(...) 826
#endif
const int maxn=1000005;
const int mod=1000000007;
int n,k,a[maxn];
priority_queue<pii> pq;
vector<vector<int>> res;
signed main(){
ios_base::sync_with_stdio(0),cin.tie(0);
#ifdef i_am_noob
freopen("input1.txt","r",stdin);
freopen("output1.txt","w",stdout);
freopen("output2.txt","w",stderr);
#endif
cin >> n >> k;
rep(n) cin >> a[i];
int sum=0,m;
rep(n) sum+=a[i];
if(sum%k){
cout << "-1\n";
return 0;
}
m=sum/k;
rep(n) pq.push({a[i],i});
while(sz(pq)>=k){
if(pq.top().first>m){
cout << "-1\n";
return 0;
}
vector<int> vec;
rep(k){
auto [x,y]=pq.top();
pq.pop();
vec.pb(y);
}
if(pq.empty()){
res.pb({a[vec[0]]});
for(auto i: vec) res.back().pb(i+1);
break;
}
int val=min(a[vec.back()],m-pq.top().first);
res.pb({val});
for(auto i: vec) res.back().pb(i+1);
for(auto i: vec) a[i]-=val;
for(auto i: vec) if(a[i]) pq.push({a[i],i});
m-=val;
rep(n) bug(a[i]);
}
if(!pq.empty()){
cout << "-1\n";
return 0;
}
cout << sz(res) << "\n";
for(auto& vec1: res){
for(auto i: vec1) cout << i << ' ';
cout << "\n";
}
}
Compilation message (stderr)
nicegift.cpp: In function 'int main()':
nicegift.cpp:20:18: warning: statement has no effect [-Wunused-value]
20 | #define bug(...) 826
| ^~~
nicegift.cpp:69:16: note: in expansion of macro 'bug'
69 | rep(n) bug(a[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... |