#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MN = 1000010;
int N, K;
priority_queue<pair<ll, int> > pq;
vector<vector<int> > S;
vector<ll> X;
int main() {
std::ios::sync_with_stdio(false);
cin >> N >> K;
//N = 1000, K = 20;
ll sum = 0;
for(int i = 0; i < N; i++) {
ll t; cin >> t;
//ll t = 100;
pq.push({t, i});
sum += t;
}
if(sum % K) {
cout << -1;
return 0;
}
while(!pq.empty()) {
vector<pair<ll, int> > V;
for(int i = 0; i < K; i++) {
if(pq.empty()) {
printf("-1");
return 0;
}
V.push_back(pq.top());
sum -= pq.top().first;
pq.pop();
}
if(pq.empty() || V.back().first < V[0].first - pq.top().first) {
X.push_back(V.back().first);
S.push_back(vector<int>());
for(int i = 0; i < K; i++) {
S.back().push_back(V[i].second);
V[i].first -= X.back();
}
}
else {
ll d = max(0LL, (pq.top().first * K - sum + K - 1) / K);
ll t = V[0].first - pq.top().first;
//cout << d << ' ' << t << endl;
X.push_back(max(t, V.back().first - d));
S.push_back(vector<int>());
for(int i = 0; i < K; i++) {
S.back().push_back(V[i].second);
V[i].first -= X.back();
}
}
for(int i = 0; i < K; i++) {
if(V[i].first) {
pq.push(V[i]);
sum += V[i].first;
}
}
}
cout << X.size() << '\n';
for(int i = 0; i < X.size(); i++) {
cout << X[i] << ' ';
for(int j = 0; j < S[i].size(); j++) {
cout << S[i][j] + 1 << ' ';
}
cout << '\n';
}
}
Compilation message
nicegift.cpp: In function 'int main()':
nicegift.cpp:76:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < X.size(); i++) {
~~^~~~~~~~~~
nicegift.cpp:79:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < S[i].size(); j++) {
~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
n=4 |
2 |
Correct |
3 ms |
384 KB |
n=3 |
3 |
Correct |
2 ms |
256 KB |
n=3 |
4 |
Correct |
2 ms |
384 KB |
n=4 |
5 |
Correct |
2 ms |
384 KB |
n=4 |
6 |
Correct |
3 ms |
384 KB |
n=2 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
n=4 |
2 |
Correct |
3 ms |
384 KB |
n=3 |
3 |
Correct |
2 ms |
256 KB |
n=3 |
4 |
Correct |
2 ms |
384 KB |
n=4 |
5 |
Correct |
2 ms |
384 KB |
n=4 |
6 |
Correct |
3 ms |
384 KB |
n=2 |
7 |
Correct |
2 ms |
384 KB |
n=5 |
8 |
Correct |
2 ms |
300 KB |
n=8 |
9 |
Correct |
2 ms |
384 KB |
n=14 |
10 |
Correct |
3 ms |
384 KB |
n=11 |
11 |
Correct |
29 ms |
3568 KB |
n=50000 |
12 |
Correct |
26 ms |
3308 KB |
n=50000 |
13 |
Correct |
3 ms |
384 KB |
n=10 |
14 |
Correct |
3 ms |
384 KB |
n=685 |
15 |
Correct |
3 ms |
384 KB |
n=623 |
16 |
Correct |
3 ms |
384 KB |
n=973 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
n=4 |
2 |
Correct |
3 ms |
384 KB |
n=3 |
3 |
Correct |
2 ms |
256 KB |
n=3 |
4 |
Correct |
2 ms |
384 KB |
n=4 |
5 |
Correct |
2 ms |
384 KB |
n=4 |
6 |
Correct |
3 ms |
384 KB |
n=2 |
7 |
Correct |
2 ms |
384 KB |
n=5 |
8 |
Correct |
2 ms |
300 KB |
n=8 |
9 |
Correct |
2 ms |
384 KB |
n=14 |
10 |
Correct |
3 ms |
384 KB |
n=11 |
11 |
Correct |
29 ms |
3568 KB |
n=50000 |
12 |
Correct |
26 ms |
3308 KB |
n=50000 |
13 |
Correct |
3 ms |
384 KB |
n=10 |
14 |
Correct |
3 ms |
384 KB |
n=685 |
15 |
Correct |
3 ms |
384 KB |
n=623 |
16 |
Correct |
3 ms |
384 KB |
n=973 |
17 |
Correct |
4 ms |
512 KB |
n=989 |
18 |
Correct |
3 ms |
384 KB |
n=563 |
19 |
Correct |
3 ms |
384 KB |
n=592 |
20 |
Correct |
4 ms |
384 KB |
n=938 |
21 |
Correct |
3 ms |
384 KB |
n=747 |
22 |
Correct |
3 ms |
384 KB |
n=991 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
717 ms |
62504 KB |
n=1000000 |
2 |
Correct |
378 ms |
42820 KB |
n=666666 |
3 |
Correct |
240 ms |
23604 KB |
n=400000 |
4 |
Correct |
166 ms |
15224 KB |
n=285714 |
5 |
Correct |
15 ms |
1404 KB |
n=20000 |
6 |
Correct |
98 ms |
9440 KB |
n=181818 |
7 |
Correct |
8 ms |
896 KB |
n=10000 |
8 |
Correct |
6 ms |
896 KB |
n=6666 |
9 |
Correct |
7 ms |
640 KB |
n=4000 |
10 |
Correct |
8 ms |
640 KB |
n=2857 |
11 |
Correct |
4 ms |
512 KB |
n=2000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
n=4 |
2 |
Correct |
3 ms |
384 KB |
n=3 |
3 |
Correct |
2 ms |
256 KB |
n=3 |
4 |
Correct |
2 ms |
384 KB |
n=4 |
5 |
Correct |
2 ms |
384 KB |
n=4 |
6 |
Correct |
3 ms |
384 KB |
n=2 |
7 |
Correct |
2 ms |
384 KB |
n=5 |
8 |
Correct |
2 ms |
300 KB |
n=8 |
9 |
Correct |
2 ms |
384 KB |
n=14 |
10 |
Correct |
3 ms |
384 KB |
n=11 |
11 |
Correct |
29 ms |
3568 KB |
n=50000 |
12 |
Correct |
26 ms |
3308 KB |
n=50000 |
13 |
Correct |
3 ms |
384 KB |
n=10 |
14 |
Correct |
3 ms |
384 KB |
n=685 |
15 |
Correct |
3 ms |
384 KB |
n=623 |
16 |
Correct |
3 ms |
384 KB |
n=973 |
17 |
Correct |
4 ms |
512 KB |
n=989 |
18 |
Correct |
3 ms |
384 KB |
n=563 |
19 |
Correct |
3 ms |
384 KB |
n=592 |
20 |
Correct |
4 ms |
384 KB |
n=938 |
21 |
Correct |
3 ms |
384 KB |
n=747 |
22 |
Correct |
3 ms |
384 KB |
n=991 |
23 |
Correct |
717 ms |
62504 KB |
n=1000000 |
24 |
Correct |
378 ms |
42820 KB |
n=666666 |
25 |
Correct |
240 ms |
23604 KB |
n=400000 |
26 |
Correct |
166 ms |
15224 KB |
n=285714 |
27 |
Correct |
15 ms |
1404 KB |
n=20000 |
28 |
Correct |
98 ms |
9440 KB |
n=181818 |
29 |
Correct |
8 ms |
896 KB |
n=10000 |
30 |
Correct |
6 ms |
896 KB |
n=6666 |
31 |
Correct |
7 ms |
640 KB |
n=4000 |
32 |
Correct |
8 ms |
640 KB |
n=2857 |
33 |
Correct |
4 ms |
512 KB |
n=2000 |
34 |
Correct |
32 ms |
2428 KB |
n=23514 |
35 |
Correct |
25 ms |
2428 KB |
n=23514 |
36 |
Correct |
3 ms |
384 KB |
n=940 |
37 |
Correct |
2 ms |
384 KB |
n=2 |
38 |
Correct |
68 ms |
4840 KB |
n=100000 |
39 |
Correct |
71 ms |
4968 KB |
n=100000 |
40 |
Correct |
2 ms |
384 KB |
n=10 |
41 |
Correct |
3 ms |
384 KB |
n=100 |
42 |
Correct |
5 ms |
512 KB |
n=1000 |
43 |
Correct |
819 ms |
84512 KB |
n=1000000 |
44 |
Correct |
1240 ms |
94532 KB |
n=1000000 |
45 |
Correct |
876 ms |
59404 KB |
n=666666 |
46 |
Correct |
509 ms |
32980 KB |
n=400000 |
47 |
Correct |
13 ms |
896 KB |
n=2336 |
48 |
Correct |
802 ms |
46552 KB |
n=285714 |
49 |
Correct |
826 ms |
39272 KB |
n=181818 |
50 |
Correct |
35 ms |
2540 KB |
n=40000 |
51 |
Correct |
19 ms |
1404 KB |
n=20000 |
52 |
Correct |
11 ms |
1024 KB |
n=10000 |
53 |
Correct |
77 ms |
3576 KB |
n=6666 |
54 |
Correct |
11 ms |
640 KB |
n=4000 |
55 |
Correct |
118 ms |
6888 KB |
n=2857 |
56 |
Correct |
8 ms |
640 KB |
n=2000 |