#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
priority_queue <pair<long long, int>> pq;
long long v[N];
vector<vector<int>> ans;
vector<long long> ansx;
int main()
{
//freopen(".in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n, k;
cin>>n>>k;
long long sum = 0, mx = 0;
for(int i=1;i<=n;i++){
cin>>v[i];
pq.push({v[i], i});
sum+=v[i];
mx = max(mx, v[i]);
}
if(sum%k!=0 || sum/k < mx ){
cout<<"-1\n";
return 0;
}
long long op = sum / k;
while(pq.size()){
vector<int> aux;
for(int i = 1 ;i <=k;i++){
aux.push_back(pq.top().second);
pq.pop();
}
long long mn = v[aux[aux.size() - 1]];
long long nxt = 0;
if(pq.size())
nxt = pq.top().first;
long long newX = min(mn, op - nxt);
op -= newX;
ansx.push_back(newX);
ans.push_back(aux);
for(auto x : aux){
v[x] -= newX;
if(v[x])
pq.push({v[x], x});
}
}
cout<<ans.size()<<"\n";
for(int i= 0; i<ans.size();i++){
cout<<ansx[i]<<" ";
for(auto x : ans[i])
cout<<x<<" ";
cout<<"\n";
}
return 0;
}
Compilation message
nicegift.cpp: In function 'int main()':
nicegift.cpp:48:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for(int i= 0; i<ans.size();i++){
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=4 |
2 |
Correct |
1 ms |
364 KB |
n=3 |
3 |
Correct |
0 ms |
364 KB |
n=3 |
4 |
Correct |
0 ms |
364 KB |
n=4 |
5 |
Correct |
0 ms |
364 KB |
n=4 |
6 |
Correct |
1 ms |
364 KB |
n=2 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=4 |
2 |
Correct |
1 ms |
364 KB |
n=3 |
3 |
Correct |
0 ms |
364 KB |
n=3 |
4 |
Correct |
0 ms |
364 KB |
n=4 |
5 |
Correct |
0 ms |
364 KB |
n=4 |
6 |
Correct |
1 ms |
364 KB |
n=2 |
7 |
Correct |
0 ms |
364 KB |
n=5 |
8 |
Correct |
0 ms |
364 KB |
n=8 |
9 |
Correct |
0 ms |
364 KB |
n=14 |
10 |
Correct |
1 ms |
364 KB |
n=11 |
11 |
Correct |
27 ms |
3944 KB |
n=50000 |
12 |
Correct |
24 ms |
3688 KB |
n=50000 |
13 |
Correct |
1 ms |
364 KB |
n=10 |
14 |
Correct |
1 ms |
364 KB |
n=685 |
15 |
Correct |
1 ms |
364 KB |
n=623 |
16 |
Correct |
1 ms |
364 KB |
n=973 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=4 |
2 |
Correct |
1 ms |
364 KB |
n=3 |
3 |
Correct |
0 ms |
364 KB |
n=3 |
4 |
Correct |
0 ms |
364 KB |
n=4 |
5 |
Correct |
0 ms |
364 KB |
n=4 |
6 |
Correct |
1 ms |
364 KB |
n=2 |
7 |
Correct |
0 ms |
364 KB |
n=5 |
8 |
Correct |
0 ms |
364 KB |
n=8 |
9 |
Correct |
0 ms |
364 KB |
n=14 |
10 |
Correct |
1 ms |
364 KB |
n=11 |
11 |
Correct |
27 ms |
3944 KB |
n=50000 |
12 |
Correct |
24 ms |
3688 KB |
n=50000 |
13 |
Correct |
1 ms |
364 KB |
n=10 |
14 |
Correct |
1 ms |
364 KB |
n=685 |
15 |
Correct |
1 ms |
364 KB |
n=623 |
16 |
Correct |
1 ms |
364 KB |
n=973 |
17 |
Correct |
1 ms |
364 KB |
n=989 |
18 |
Correct |
1 ms |
364 KB |
n=563 |
19 |
Correct |
1 ms |
384 KB |
n=592 |
20 |
Correct |
1 ms |
364 KB |
n=938 |
21 |
Correct |
1 ms |
364 KB |
n=747 |
22 |
Correct |
1 ms |
364 KB |
n=991 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
564 ms |
70332 KB |
n=1000000 |
2 |
Correct |
367 ms |
48316 KB |
n=666666 |
3 |
Correct |
207 ms |
25808 KB |
n=400000 |
4 |
Correct |
137 ms |
17360 KB |
n=285714 |
5 |
Correct |
9 ms |
1392 KB |
n=20000 |
6 |
Correct |
82 ms |
10588 KB |
n=181818 |
7 |
Correct |
5 ms |
1004 KB |
n=10000 |
8 |
Correct |
4 ms |
748 KB |
n=6666 |
9 |
Correct |
2 ms |
620 KB |
n=4000 |
10 |
Correct |
4 ms |
620 KB |
n=2857 |
11 |
Correct |
2 ms |
492 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
n=4 |
2 |
Correct |
1 ms |
364 KB |
n=3 |
3 |
Correct |
0 ms |
364 KB |
n=3 |
4 |
Correct |
0 ms |
364 KB |
n=4 |
5 |
Correct |
0 ms |
364 KB |
n=4 |
6 |
Correct |
1 ms |
364 KB |
n=2 |
7 |
Correct |
0 ms |
364 KB |
n=5 |
8 |
Correct |
0 ms |
364 KB |
n=8 |
9 |
Correct |
0 ms |
364 KB |
n=14 |
10 |
Correct |
1 ms |
364 KB |
n=11 |
11 |
Correct |
27 ms |
3944 KB |
n=50000 |
12 |
Correct |
24 ms |
3688 KB |
n=50000 |
13 |
Correct |
1 ms |
364 KB |
n=10 |
14 |
Correct |
1 ms |
364 KB |
n=685 |
15 |
Correct |
1 ms |
364 KB |
n=623 |
16 |
Correct |
1 ms |
364 KB |
n=973 |
17 |
Correct |
1 ms |
364 KB |
n=989 |
18 |
Correct |
1 ms |
364 KB |
n=563 |
19 |
Correct |
1 ms |
384 KB |
n=592 |
20 |
Correct |
1 ms |
364 KB |
n=938 |
21 |
Correct |
1 ms |
364 KB |
n=747 |
22 |
Correct |
1 ms |
364 KB |
n=991 |
23 |
Correct |
564 ms |
70332 KB |
n=1000000 |
24 |
Correct |
367 ms |
48316 KB |
n=666666 |
25 |
Correct |
207 ms |
25808 KB |
n=400000 |
26 |
Correct |
137 ms |
17360 KB |
n=285714 |
27 |
Correct |
9 ms |
1392 KB |
n=20000 |
28 |
Correct |
82 ms |
10588 KB |
n=181818 |
29 |
Correct |
5 ms |
1004 KB |
n=10000 |
30 |
Correct |
4 ms |
748 KB |
n=6666 |
31 |
Correct |
2 ms |
620 KB |
n=4000 |
32 |
Correct |
4 ms |
620 KB |
n=2857 |
33 |
Correct |
2 ms |
492 KB |
n=2000 |
34 |
Correct |
18 ms |
2540 KB |
n=23514 |
35 |
Correct |
18 ms |
2540 KB |
n=23514 |
36 |
Correct |
1 ms |
492 KB |
n=940 |
37 |
Correct |
1 ms |
364 KB |
n=2 |
38 |
Correct |
47 ms |
5476 KB |
n=100000 |
39 |
Correct |
51 ms |
5476 KB |
n=100000 |
40 |
Correct |
1 ms |
364 KB |
n=10 |
41 |
Correct |
1 ms |
364 KB |
n=100 |
42 |
Correct |
3 ms |
492 KB |
n=1000 |
43 |
Correct |
714 ms |
92804 KB |
n=1000000 |
44 |
Correct |
1119 ms |
102656 KB |
n=1000000 |
45 |
Correct |
734 ms |
64700 KB |
n=666666 |
46 |
Correct |
415 ms |
34256 KB |
n=400000 |
47 |
Correct |
11 ms |
876 KB |
n=2336 |
48 |
Correct |
747 ms |
48960 KB |
n=285714 |
49 |
Correct |
648 ms |
38240 KB |
n=181818 |
50 |
Correct |
30 ms |
2792 KB |
n=40000 |
51 |
Correct |
15 ms |
1644 KB |
n=20000 |
52 |
Correct |
9 ms |
1008 KB |
n=10000 |
53 |
Correct |
61 ms |
3308 KB |
n=6666 |
54 |
Correct |
5 ms |
620 KB |
n=4000 |
55 |
Correct |
240 ms |
11372 KB |
n=2857 |
56 |
Correct |
4 ms |
492 KB |
n=2000 |