#include<bits/stdc++.h>
#define f first
#define s second
#define pii pair<int,int>
#define int long long
using namespace std;
const int N = 1e6 + 5, mod = 1e9 + 7; // !
int t,a[N],n,k;
vector<pii> v[N];
vector<vector<int> > ans;
set<pii> s;
main(){
cin >> n >> k;
int mx = 0, sum = 0;
for(int i = 1; i <= n; i++) cin >> a[i], sum += a[i], mx = max(mx, a[i]);
if(sum % k || mx > sum/k) {
cout << -1;
return 0;
}
sum /= k;
int cur = 0, c = 0;
for(int i = 1; i <= n; i++) {
if(cur == sum) c++, cur = 0;
int x = min(a[i], sum - cur);
cur += x; // cout << i <<" " << x << endl;
v[c].push_back({i, x});
if(a[i] > x) {
c++;
v[c].push_back({i, a[i] - x});
cur = a[i] - x;
}
}
for(int i = 0; i <= c; i++) reverse(v[i].begin(), v[i].end()),
s.insert({v[i].back().s, i});
while(s.size()) {
int x = (*s.begin()).f;
vector<int> a;
a.push_back(x);
assert(x > 0);
for(set<pii> :: iterator it = s.begin(); it != s.end(); it++) {
a.push_back( v[(*it).s].back().f);
v[(*it).s].back().s -= x;
if(!v[(*it).s].back().s) v[(*it).s].pop_back();
}
ans.push_back(a);
s.clear();
if(!v[0].size()) break;
for(int i = 0; i <= c; i++) {
s.insert({v[i].back().s, i});
}
}
cout << ans.size() << endl;
for(int i = 0; i < ans.size(); i++) {
for(int j = 0; j < ans[i].size(); j++) {
cout << ans[i][j] <<" ";
}
cout << endl;
}
}
Compilation message
nicegift.cpp:12:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
12 | main(){
| ^~~~
nicegift.cpp: In function 'int main()':
nicegift.cpp:53:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i = 0; i < ans.size(); i++) {
| ~~^~~~~~~~~~~~
nicegift.cpp:54:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int j = 0; j < ans[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
n=4 |
2 |
Correct |
13 ms |
23756 KB |
n=3 |
3 |
Correct |
12 ms |
23764 KB |
n=3 |
4 |
Correct |
13 ms |
23728 KB |
n=4 |
5 |
Correct |
12 ms |
23756 KB |
n=4 |
6 |
Correct |
13 ms |
23688 KB |
n=2 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
n=4 |
2 |
Correct |
13 ms |
23756 KB |
n=3 |
3 |
Correct |
12 ms |
23764 KB |
n=3 |
4 |
Correct |
13 ms |
23728 KB |
n=4 |
5 |
Correct |
12 ms |
23756 KB |
n=4 |
6 |
Correct |
13 ms |
23688 KB |
n=2 |
7 |
Correct |
14 ms |
23756 KB |
n=5 |
8 |
Correct |
13 ms |
23684 KB |
n=8 |
9 |
Correct |
13 ms |
23756 KB |
n=14 |
10 |
Correct |
12 ms |
23756 KB |
n=11 |
11 |
Correct |
88 ms |
27876 KB |
n=50000 |
12 |
Correct |
81 ms |
27732 KB |
n=50000 |
13 |
Correct |
12 ms |
23756 KB |
n=10 |
14 |
Correct |
14 ms |
23756 KB |
n=685 |
15 |
Correct |
14 ms |
23812 KB |
n=623 |
16 |
Correct |
14 ms |
23760 KB |
n=973 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
n=4 |
2 |
Correct |
13 ms |
23756 KB |
n=3 |
3 |
Correct |
12 ms |
23764 KB |
n=3 |
4 |
Correct |
13 ms |
23728 KB |
n=4 |
5 |
Correct |
12 ms |
23756 KB |
n=4 |
6 |
Correct |
13 ms |
23688 KB |
n=2 |
7 |
Correct |
14 ms |
23756 KB |
n=5 |
8 |
Correct |
13 ms |
23684 KB |
n=8 |
9 |
Correct |
13 ms |
23756 KB |
n=14 |
10 |
Correct |
12 ms |
23756 KB |
n=11 |
11 |
Correct |
88 ms |
27876 KB |
n=50000 |
12 |
Correct |
81 ms |
27732 KB |
n=50000 |
13 |
Correct |
12 ms |
23756 KB |
n=10 |
14 |
Correct |
14 ms |
23756 KB |
n=685 |
15 |
Correct |
14 ms |
23812 KB |
n=623 |
16 |
Correct |
14 ms |
23760 KB |
n=973 |
17 |
Correct |
14 ms |
23884 KB |
n=989 |
18 |
Correct |
16 ms |
23832 KB |
n=563 |
19 |
Correct |
17 ms |
23868 KB |
n=592 |
20 |
Correct |
16 ms |
23928 KB |
n=938 |
21 |
Correct |
16 ms |
23884 KB |
n=747 |
22 |
Correct |
18 ms |
23904 KB |
n=991 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1277 ms |
89192 KB |
n=1000000 |
2 |
Correct |
712 ms |
63072 KB |
n=666666 |
3 |
Correct |
370 ms |
44040 KB |
n=400000 |
4 |
Correct |
806 ms |
76900 KB |
n=285714 |
5 |
Correct |
26 ms |
24572 KB |
n=20000 |
6 |
Correct |
623 ms |
67748 KB |
n=181818 |
7 |
Correct |
21 ms |
24308 KB |
n=10000 |
8 |
Correct |
76 ms |
28172 KB |
n=6666 |
9 |
Correct |
16 ms |
23912 KB |
n=4000 |
10 |
Correct |
359 ms |
48964 KB |
n=2857 |
11 |
Correct |
14 ms |
23924 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
n=4 |
2 |
Correct |
13 ms |
23756 KB |
n=3 |
3 |
Correct |
12 ms |
23764 KB |
n=3 |
4 |
Correct |
13 ms |
23728 KB |
n=4 |
5 |
Correct |
12 ms |
23756 KB |
n=4 |
6 |
Correct |
13 ms |
23688 KB |
n=2 |
7 |
Correct |
14 ms |
23756 KB |
n=5 |
8 |
Correct |
13 ms |
23684 KB |
n=8 |
9 |
Correct |
13 ms |
23756 KB |
n=14 |
10 |
Correct |
12 ms |
23756 KB |
n=11 |
11 |
Correct |
88 ms |
27876 KB |
n=50000 |
12 |
Correct |
81 ms |
27732 KB |
n=50000 |
13 |
Correct |
12 ms |
23756 KB |
n=10 |
14 |
Correct |
14 ms |
23756 KB |
n=685 |
15 |
Correct |
14 ms |
23812 KB |
n=623 |
16 |
Correct |
14 ms |
23760 KB |
n=973 |
17 |
Correct |
14 ms |
23884 KB |
n=989 |
18 |
Correct |
16 ms |
23832 KB |
n=563 |
19 |
Correct |
17 ms |
23868 KB |
n=592 |
20 |
Correct |
16 ms |
23928 KB |
n=938 |
21 |
Correct |
16 ms |
23884 KB |
n=747 |
22 |
Correct |
18 ms |
23904 KB |
n=991 |
23 |
Correct |
1277 ms |
89192 KB |
n=1000000 |
24 |
Correct |
712 ms |
63072 KB |
n=666666 |
25 |
Correct |
370 ms |
44040 KB |
n=400000 |
26 |
Correct |
806 ms |
76900 KB |
n=285714 |
27 |
Correct |
26 ms |
24572 KB |
n=20000 |
28 |
Correct |
623 ms |
67748 KB |
n=181818 |
29 |
Correct |
21 ms |
24308 KB |
n=10000 |
30 |
Correct |
76 ms |
28172 KB |
n=6666 |
31 |
Correct |
16 ms |
23912 KB |
n=4000 |
32 |
Correct |
359 ms |
48964 KB |
n=2857 |
33 |
Correct |
14 ms |
23924 KB |
n=2000 |
34 |
Correct |
56 ms |
25944 KB |
n=23514 |
35 |
Correct |
55 ms |
25980 KB |
n=23514 |
36 |
Correct |
14 ms |
23884 KB |
n=940 |
37 |
Correct |
14 ms |
23756 KB |
n=2 |
38 |
Correct |
234 ms |
37956 KB |
n=100000 |
39 |
Correct |
227 ms |
37936 KB |
n=100000 |
40 |
Correct |
13 ms |
23756 KB |
n=10 |
41 |
Correct |
14 ms |
23744 KB |
n=100 |
42 |
Correct |
23 ms |
24416 KB |
n=1000 |
43 |
Correct |
1867 ms |
123880 KB |
n=1000000 |
44 |
Execution timed out |
2063 ms |
127900 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |