#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long s = accumulate(a.begin(), a.end(), 0LL);
if (s % k != 0 || *max_element(a.begin(), a.end()) > s / k) {
cout << -1 << '\n';
return 0;
}
set<pair<long long, int>> st;
for (int i = 0; i < n; i++) {
st.emplace(a[i], i);
}
vector<vector<long long>> res;
while (s > 0) {
vector<int> ids;
for (int foo = 0; foo < k; foo++) {
auto it = prev(st.end());
ids.push_back(it->second);
st.erase(it);
}
long long v = a[ids.back()];
if (!st.empty()) {
v = min(v, s / k - prev(st.end())->first);
}
res.push_back({v});
for (int i : ids) {
res.back().push_back(i + 1);
}
for (int i : ids) {
s -= v;
a[i] -= v;
if (a[i] > 0) {
st.emplace(a[i], i);
}
}
}
cout << (int) res.size() << '\n';
for (int i = 0; i < (int) res.size(); i++) {
for (int j = 0; j < (int) res[i].size(); j++) {
cout << res[i][j] << " ";
}
cout << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=4 |
2 |
Correct |
0 ms |
348 KB |
n=3 |
3 |
Correct |
0 ms |
348 KB |
n=3 |
4 |
Correct |
0 ms |
348 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
348 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=4 |
2 |
Correct |
0 ms |
348 KB |
n=3 |
3 |
Correct |
0 ms |
348 KB |
n=3 |
4 |
Correct |
0 ms |
348 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
348 KB |
n=2 |
7 |
Correct |
0 ms |
348 KB |
n=5 |
8 |
Correct |
0 ms |
348 KB |
n=8 |
9 |
Correct |
1 ms |
348 KB |
n=14 |
10 |
Correct |
0 ms |
348 KB |
n=11 |
11 |
Correct |
18 ms |
5232 KB |
n=50000 |
12 |
Correct |
16 ms |
4892 KB |
n=50000 |
13 |
Correct |
0 ms |
348 KB |
n=10 |
14 |
Correct |
1 ms |
348 KB |
n=685 |
15 |
Correct |
1 ms |
348 KB |
n=623 |
16 |
Correct |
1 ms |
348 KB |
n=973 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=4 |
2 |
Correct |
0 ms |
348 KB |
n=3 |
3 |
Correct |
0 ms |
348 KB |
n=3 |
4 |
Correct |
0 ms |
348 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
348 KB |
n=2 |
7 |
Correct |
0 ms |
348 KB |
n=5 |
8 |
Correct |
0 ms |
348 KB |
n=8 |
9 |
Correct |
1 ms |
348 KB |
n=14 |
10 |
Correct |
0 ms |
348 KB |
n=11 |
11 |
Correct |
18 ms |
5232 KB |
n=50000 |
12 |
Correct |
16 ms |
4892 KB |
n=50000 |
13 |
Correct |
0 ms |
348 KB |
n=10 |
14 |
Correct |
1 ms |
348 KB |
n=685 |
15 |
Correct |
1 ms |
348 KB |
n=623 |
16 |
Correct |
1 ms |
348 KB |
n=973 |
17 |
Correct |
1 ms |
344 KB |
n=989 |
18 |
Correct |
0 ms |
348 KB |
n=563 |
19 |
Correct |
1 ms |
348 KB |
n=592 |
20 |
Correct |
1 ms |
348 KB |
n=938 |
21 |
Correct |
0 ms |
348 KB |
n=747 |
22 |
Correct |
1 ms |
348 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
429 ms |
97096 KB |
n=1000000 |
2 |
Correct |
256 ms |
55284 KB |
n=666666 |
3 |
Correct |
175 ms |
32520 KB |
n=400000 |
4 |
Correct |
101 ms |
22864 KB |
n=285714 |
5 |
Correct |
7 ms |
1884 KB |
n=20000 |
6 |
Correct |
64 ms |
14688 KB |
n=181818 |
7 |
Correct |
3 ms |
1112 KB |
n=10000 |
8 |
Correct |
3 ms |
860 KB |
n=6666 |
9 |
Correct |
1 ms |
604 KB |
n=4000 |
10 |
Correct |
3 ms |
668 KB |
n=2857 |
11 |
Correct |
1 ms |
604 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=4 |
2 |
Correct |
0 ms |
348 KB |
n=3 |
3 |
Correct |
0 ms |
348 KB |
n=3 |
4 |
Correct |
0 ms |
348 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
348 KB |
n=2 |
7 |
Correct |
0 ms |
348 KB |
n=5 |
8 |
Correct |
0 ms |
348 KB |
n=8 |
9 |
Correct |
1 ms |
348 KB |
n=14 |
10 |
Correct |
0 ms |
348 KB |
n=11 |
11 |
Correct |
18 ms |
5232 KB |
n=50000 |
12 |
Correct |
16 ms |
4892 KB |
n=50000 |
13 |
Correct |
0 ms |
348 KB |
n=10 |
14 |
Correct |
1 ms |
348 KB |
n=685 |
15 |
Correct |
1 ms |
348 KB |
n=623 |
16 |
Correct |
1 ms |
348 KB |
n=973 |
17 |
Correct |
1 ms |
344 KB |
n=989 |
18 |
Correct |
0 ms |
348 KB |
n=563 |
19 |
Correct |
1 ms |
348 KB |
n=592 |
20 |
Correct |
1 ms |
348 KB |
n=938 |
21 |
Correct |
0 ms |
348 KB |
n=747 |
22 |
Correct |
1 ms |
348 KB |
n=991 |
23 |
Correct |
429 ms |
97096 KB |
n=1000000 |
24 |
Correct |
256 ms |
55284 KB |
n=666666 |
25 |
Correct |
175 ms |
32520 KB |
n=400000 |
26 |
Correct |
101 ms |
22864 KB |
n=285714 |
27 |
Correct |
7 ms |
1884 KB |
n=20000 |
28 |
Correct |
64 ms |
14688 KB |
n=181818 |
29 |
Correct |
3 ms |
1112 KB |
n=10000 |
30 |
Correct |
3 ms |
860 KB |
n=6666 |
31 |
Correct |
1 ms |
604 KB |
n=4000 |
32 |
Correct |
3 ms |
668 KB |
n=2857 |
33 |
Correct |
1 ms |
604 KB |
n=2000 |
34 |
Correct |
16 ms |
3224 KB |
n=23514 |
35 |
Correct |
19 ms |
3236 KB |
n=23514 |
36 |
Correct |
1 ms |
348 KB |
n=940 |
37 |
Correct |
0 ms |
372 KB |
n=2 |
38 |
Correct |
37 ms |
9276 KB |
n=100000 |
39 |
Correct |
39 ms |
9228 KB |
n=100000 |
40 |
Correct |
0 ms |
344 KB |
n=10 |
41 |
Correct |
1 ms |
348 KB |
n=100 |
42 |
Correct |
2 ms |
600 KB |
n=1000 |
43 |
Correct |
570 ms |
112476 KB |
n=1000000 |
44 |
Correct |
1308 ms |
121844 KB |
n=1000000 |
45 |
Correct |
892 ms |
78068 KB |
n=666666 |
46 |
Correct |
388 ms |
49656 KB |
n=400000 |
47 |
Correct |
9 ms |
1372 KB |
n=2336 |
48 |
Correct |
413 ms |
53604 KB |
n=285714 |
49 |
Correct |
371 ms |
51456 KB |
n=181818 |
50 |
Correct |
30 ms |
4944 KB |
n=40000 |
51 |
Correct |
13 ms |
2648 KB |
n=20000 |
52 |
Correct |
8 ms |
1624 KB |
n=10000 |
53 |
Correct |
48 ms |
6344 KB |
n=6666 |
54 |
Correct |
4 ms |
860 KB |
n=4000 |
55 |
Correct |
198 ms |
20852 KB |
n=2857 |
56 |
Correct |
3 ms |
604 KB |
n=2000 |