#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;
#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif
const int N = 1e6 + 5;
const ll INF = 1e18 + 5;
int n, k;
ll a[N];
signed main() {
cin.tie(0) -> sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
ll sum = accumulate(a + 1, a + 1 + n, 0LL);
ll mx = *max_element(a + 1, a + 1 + n);
if (sum % k || mx > sum / k) {
cout << "-1\n";
return 0;
}
using T = pair <ll, ll>;
priority_queue <T> q;
for (int i = 1; i <= n; ++i)
q.push(make_pair(a[i], i));
vector <vector <ll>> ans;
// Take k max values
while ((int) q.size() >= k) {
int take = 0;
vector <T> opts;
while (!q.empty()) {
if (take == k) break;
opts.push_back(q.top());
q.pop();
++take;
}
ll y = (q.empty()? -1 : q.top().first);
// subtract value
ll x;
if (y == -1) x = opts.back().first;
else x = min(1LL * opts.back().first, (sum - y * k) / k);
vector <ll> ids;
ids.push_back(x);
for (auto [val, id] : opts) {
a[id] -= x;
ids.push_back(id);
if (a[id] > 0)
q.push(make_pair(a[id], id));
}
sum -= 1LL * k * x;
ans.push_back(ids);
}
cout << (int) ans.size() << "\n";
for (auto x : ans) {
for (ll j : x) cout << j << " ";
cout << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=4 |
2 |
Correct |
1 ms |
464 KB |
n=3 |
3 |
Correct |
0 ms |
348 KB |
n=3 |
4 |
Correct |
0 ms |
344 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
464 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=4 |
2 |
Correct |
1 ms |
464 KB |
n=3 |
3 |
Correct |
0 ms |
348 KB |
n=3 |
4 |
Correct |
0 ms |
344 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
464 KB |
n=2 |
7 |
Correct |
0 ms |
348 KB |
n=5 |
8 |
Correct |
1 ms |
348 KB |
n=8 |
9 |
Correct |
0 ms |
348 KB |
n=14 |
10 |
Correct |
1 ms |
348 KB |
n=11 |
11 |
Correct |
20 ms |
5520 KB |
n=50000 |
12 |
Correct |
25 ms |
5300 KB |
n=50000 |
13 |
Correct |
1 ms |
460 KB |
n=10 |
14 |
Correct |
1 ms |
344 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 |
1 ms |
464 KB |
n=3 |
3 |
Correct |
0 ms |
348 KB |
n=3 |
4 |
Correct |
0 ms |
344 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
464 KB |
n=2 |
7 |
Correct |
0 ms |
348 KB |
n=5 |
8 |
Correct |
1 ms |
348 KB |
n=8 |
9 |
Correct |
0 ms |
348 KB |
n=14 |
10 |
Correct |
1 ms |
348 KB |
n=11 |
11 |
Correct |
20 ms |
5520 KB |
n=50000 |
12 |
Correct |
25 ms |
5300 KB |
n=50000 |
13 |
Correct |
1 ms |
460 KB |
n=10 |
14 |
Correct |
1 ms |
344 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 |
1 ms |
348 KB |
n=563 |
19 |
Correct |
1 ms |
344 KB |
n=592 |
20 |
Correct |
1 ms |
348 KB |
n=938 |
21 |
Correct |
1 ms |
344 KB |
n=747 |
22 |
Correct |
1 ms |
348 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
441 ms |
80556 KB |
n=1000000 |
2 |
Correct |
268 ms |
51304 KB |
n=666666 |
3 |
Correct |
161 ms |
29276 KB |
n=400000 |
4 |
Correct |
91 ms |
21196 KB |
n=285714 |
5 |
Correct |
6 ms |
1500 KB |
n=20000 |
6 |
Correct |
56 ms |
11896 KB |
n=181818 |
7 |
Correct |
4 ms |
1096 KB |
n=10000 |
8 |
Correct |
2 ms |
856 KB |
n=6666 |
9 |
Correct |
1 ms |
604 KB |
n=4000 |
10 |
Correct |
3 ms |
600 KB |
n=2857 |
11 |
Correct |
1 ms |
600 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
n=4 |
2 |
Correct |
1 ms |
464 KB |
n=3 |
3 |
Correct |
0 ms |
348 KB |
n=3 |
4 |
Correct |
0 ms |
344 KB |
n=4 |
5 |
Correct |
0 ms |
348 KB |
n=4 |
6 |
Correct |
0 ms |
464 KB |
n=2 |
7 |
Correct |
0 ms |
348 KB |
n=5 |
8 |
Correct |
1 ms |
348 KB |
n=8 |
9 |
Correct |
0 ms |
348 KB |
n=14 |
10 |
Correct |
1 ms |
348 KB |
n=11 |
11 |
Correct |
20 ms |
5520 KB |
n=50000 |
12 |
Correct |
25 ms |
5300 KB |
n=50000 |
13 |
Correct |
1 ms |
460 KB |
n=10 |
14 |
Correct |
1 ms |
344 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 |
1 ms |
348 KB |
n=563 |
19 |
Correct |
1 ms |
344 KB |
n=592 |
20 |
Correct |
1 ms |
348 KB |
n=938 |
21 |
Correct |
1 ms |
344 KB |
n=747 |
22 |
Correct |
1 ms |
348 KB |
n=991 |
23 |
Correct |
441 ms |
80556 KB |
n=1000000 |
24 |
Correct |
268 ms |
51304 KB |
n=666666 |
25 |
Correct |
161 ms |
29276 KB |
n=400000 |
26 |
Correct |
91 ms |
21196 KB |
n=285714 |
27 |
Correct |
6 ms |
1500 KB |
n=20000 |
28 |
Correct |
56 ms |
11896 KB |
n=181818 |
29 |
Correct |
4 ms |
1096 KB |
n=10000 |
30 |
Correct |
2 ms |
856 KB |
n=6666 |
31 |
Correct |
1 ms |
604 KB |
n=4000 |
32 |
Correct |
3 ms |
600 KB |
n=2857 |
33 |
Correct |
1 ms |
600 KB |
n=2000 |
34 |
Correct |
17 ms |
4440 KB |
n=23514 |
35 |
Correct |
14 ms |
4476 KB |
n=23514 |
36 |
Correct |
1 ms |
348 KB |
n=940 |
37 |
Correct |
0 ms |
348 KB |
n=2 |
38 |
Correct |
33 ms |
7116 KB |
n=100000 |
39 |
Correct |
35 ms |
7012 KB |
n=100000 |
40 |
Correct |
1 ms |
344 KB |
n=10 |
41 |
Correct |
1 ms |
348 KB |
n=100 |
42 |
Correct |
2 ms |
604 KB |
n=1000 |
43 |
Correct |
501 ms |
87788 KB |
n=1000000 |
44 |
Correct |
760 ms |
97024 KB |
n=1000000 |
45 |
Correct |
515 ms |
66740 KB |
n=666666 |
46 |
Correct |
274 ms |
38400 KB |
n=400000 |
47 |
Correct |
11 ms |
1060 KB |
n=2336 |
48 |
Correct |
488 ms |
58020 KB |
n=285714 |
49 |
Correct |
427 ms |
46176 KB |
n=181818 |
50 |
Correct |
24 ms |
4816 KB |
n=40000 |
51 |
Correct |
14 ms |
1572 KB |
n=20000 |
52 |
Correct |
9 ms |
992 KB |
n=10000 |
53 |
Correct |
43 ms |
4520 KB |
n=6666 |
54 |
Correct |
4 ms |
856 KB |
n=4000 |
55 |
Correct |
179 ms |
16216 KB |
n=2857 |
56 |
Correct |
3 ms |
604 KB |
n=2000 |