This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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) {
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 (ll c : v) cout << c << ' ';
cout << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |