#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int M = (1 << 20);
int n, k;
ll total;
pair<ll, int> a[M];
vector<vector<ll>> res;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i].first;
total += a[i].first;
a[i].second = i + 1;
}
sort(a, a + n);
if (total % k || a[n - 1].first > total / k) {
cout << -1;
return 0;
}
int l = 0, r = n - 1;
while (l <= r) {
cout << l << ' ' << r << '\n';
ll X = min(a[l].first, (total - k * a[r].first) / k);
if (X) {
res.push_back({X});
for (int i = l; i < l + k; i++) {
a[i].first -= X;
total -= X;
res.back().push_back(a[i].second);
}
for (int i = r + 1; i < n; i++) {
a[i].first -= X;
res.back().push_back(a[i].second);
}
}
if (a[r].first == total / k) {
total -= a[r].first;
r--,
k--;
}
while (l <= r && !a[l].first) l++;
}
if (a[n - 1].first) {
res.push_back({a[n - 1].first});
while (n && a[n - 1].first)
res.back().push_back(a[--n].second);
}
cout << (int)res.size() << '\n';
for (auto v : res) {
for (int c : v) cout << c << ' ';
cout << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Not all heaps are empty in the end |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Not all heaps are empty in the end |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Not all heaps are empty in the end |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
314 ms |
84964 KB |
Not all heaps are empty in the end |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Not all heaps are empty in the end |
2 |
Halted |
0 ms |
0 KB |
- |