#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
using pii=pair<int,int>;
const int lim=1e5+100;
struct op{
int val;
vector<int>inds;
};
signed main(){
int n,k;
cin>>n>>k;
int a[n],sm=0;
for(int i=0;i<n;i++)cin>>a[i],sm+=a[i];
int p[n];
iota(p,p+n,0);
sort(p,p+n,[&](int i,int j)->bool {
return a[i]>a[j];
});
if(sm%k){
cout<<-1;
return 0;
}
sm/=k;
if(sm<a[p[0]]){
cout<<-1;
return 0;
}
vector<op>ops;
int left=n;
while(left&&a[p[0]]!=a[p[left-1]]){
op todo;
int last=0;
for(;last<n;last++){
if(a[p[last]]==sm){
todo.inds.pb(p[last]);
}else break;
}
todo.val=min(a[p[left-1]],sm-a[p[last]]);
for(int i=left-1;todo.inds.size()<k;i--){
todo.inds.pb(p[i]);
}
sm-=todo.val;
for(int i:todo.inds){
a[i]-=todo.val;
}
ops.pb(todo);
while(left&&a[p[left-1]]==0)left--;
}
if(left){
int z=left/gcd(left,k);
int val=sm*k/left;
int i=0;
for(int t=0;t<z;t++){
op todo;
todo.val=val;
for(int j=0;j<k;j++,(i=((i+1)%left))){
todo.inds.pb(p[i]);
}
ops.pb(todo);
}
}
cout<<ops.size()<<'\n';
for(op&pp:ops){
cout<<pp.val<<' ';
for(int i:pp.inds){
cout<<i+1<<' ';
}
cout<<'\n';
}
}
Compilation message
nicegift.cpp: In function 'int main()':
nicegift.cpp:43:38: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
43 | for(int i=left-1;todo.inds.size()<k;i--){
| ~~~~~~~~~~~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
n=4 |
2 |
Correct |
1 ms |
504 KB |
n=3 |
3 |
Correct |
1 ms |
336 KB |
n=3 |
4 |
Correct |
1 ms |
336 KB |
n=4 |
5 |
Correct |
1 ms |
336 KB |
n=4 |
6 |
Correct |
1 ms |
440 KB |
n=2 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
n=4 |
2 |
Correct |
1 ms |
504 KB |
n=3 |
3 |
Correct |
1 ms |
336 KB |
n=3 |
4 |
Correct |
1 ms |
336 KB |
n=4 |
5 |
Correct |
1 ms |
336 KB |
n=4 |
6 |
Correct |
1 ms |
440 KB |
n=2 |
7 |
Correct |
1 ms |
336 KB |
n=5 |
8 |
Correct |
1 ms |
336 KB |
n=8 |
9 |
Correct |
1 ms |
336 KB |
n=14 |
10 |
Correct |
1 ms |
336 KB |
n=11 |
11 |
Correct |
15 ms |
3524 KB |
n=50000 |
12 |
Correct |
14 ms |
3012 KB |
n=50000 |
13 |
Correct |
1 ms |
336 KB |
n=10 |
14 |
Correct |
1 ms |
508 KB |
n=685 |
15 |
Correct |
1 ms |
500 KB |
n=623 |
16 |
Correct |
1 ms |
336 KB |
n=973 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
n=4 |
2 |
Correct |
1 ms |
504 KB |
n=3 |
3 |
Correct |
1 ms |
336 KB |
n=3 |
4 |
Correct |
1 ms |
336 KB |
n=4 |
5 |
Correct |
1 ms |
336 KB |
n=4 |
6 |
Correct |
1 ms |
440 KB |
n=2 |
7 |
Correct |
1 ms |
336 KB |
n=5 |
8 |
Correct |
1 ms |
336 KB |
n=8 |
9 |
Correct |
1 ms |
336 KB |
n=14 |
10 |
Correct |
1 ms |
336 KB |
n=11 |
11 |
Correct |
15 ms |
3524 KB |
n=50000 |
12 |
Correct |
14 ms |
3012 KB |
n=50000 |
13 |
Correct |
1 ms |
336 KB |
n=10 |
14 |
Correct |
1 ms |
508 KB |
n=685 |
15 |
Correct |
1 ms |
500 KB |
n=623 |
16 |
Correct |
1 ms |
336 KB |
n=973 |
17 |
Correct |
1 ms |
336 KB |
n=989 |
18 |
Correct |
1 ms |
336 KB |
n=563 |
19 |
Correct |
2 ms |
504 KB |
n=592 |
20 |
Correct |
2 ms |
592 KB |
n=938 |
21 |
Correct |
2 ms |
336 KB |
n=747 |
22 |
Correct |
2 ms |
456 KB |
n=991 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
489 ms |
63112 KB |
n=1000000 |
2 |
Correct |
315 ms |
41640 KB |
n=666666 |
3 |
Correct |
192 ms |
22964 KB |
n=400000 |
4 |
Incorrect |
315 ms |
50856 KB |
Taken too much stones from the heap |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
n=4 |
2 |
Correct |
1 ms |
504 KB |
n=3 |
3 |
Correct |
1 ms |
336 KB |
n=3 |
4 |
Correct |
1 ms |
336 KB |
n=4 |
5 |
Correct |
1 ms |
336 KB |
n=4 |
6 |
Correct |
1 ms |
440 KB |
n=2 |
7 |
Correct |
1 ms |
336 KB |
n=5 |
8 |
Correct |
1 ms |
336 KB |
n=8 |
9 |
Correct |
1 ms |
336 KB |
n=14 |
10 |
Correct |
1 ms |
336 KB |
n=11 |
11 |
Correct |
15 ms |
3524 KB |
n=50000 |
12 |
Correct |
14 ms |
3012 KB |
n=50000 |
13 |
Correct |
1 ms |
336 KB |
n=10 |
14 |
Correct |
1 ms |
508 KB |
n=685 |
15 |
Correct |
1 ms |
500 KB |
n=623 |
16 |
Correct |
1 ms |
336 KB |
n=973 |
17 |
Correct |
1 ms |
336 KB |
n=989 |
18 |
Correct |
1 ms |
336 KB |
n=563 |
19 |
Correct |
2 ms |
504 KB |
n=592 |
20 |
Correct |
2 ms |
592 KB |
n=938 |
21 |
Correct |
2 ms |
336 KB |
n=747 |
22 |
Correct |
2 ms |
456 KB |
n=991 |
23 |
Correct |
489 ms |
63112 KB |
n=1000000 |
24 |
Correct |
315 ms |
41640 KB |
n=666666 |
25 |
Correct |
192 ms |
22964 KB |
n=400000 |
26 |
Incorrect |
315 ms |
50856 KB |
Taken too much stones from the heap |
27 |
Halted |
0 ms |
0 KB |
- |