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>
//#pragma GCC optimize("Ofast")
#define fi first
#define se second
const int N = 1000010;
using namespace std;
int n;
int k;
long long a[N];
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin >> n >> k;
priority_queue < pair < long long, int > > s;
long long S = 0;
for(int i = 1; i <= n; i++){
cin >> a[i];
s.push({a[i], i});
S += a[i];
}
if(1ll * s.top().fi > S / k || S % k){
cout << -1 << "\n";
return 0;
}
vector < long long > qu;
vector < vector < int > > res;
vector < int > v;
while(s.size() > 0){
v.clear();
for(int i = 0; i < k; i++){
v.push_back(s.top().se);
s.pop();
}
long long rem = 0;
if(!s.empty()){
rem = s.top().fi;
}
long long l = (S - rem * k) / k;
l = min(l, a[v.back()]);
S -= k * l;
res.push_back(v);
qu.push_back(l);
for(int i: v){
a[i] -= l;
if(!a[i]){
continue;
}
s.push({a[i], i});
}
}
cout << qu.size() << "\n";
for(int i = 0; i < qu.size(); i++){
cout << qu[i];
for(int x: res[i]){
cout << " " << x;
}
cout << "\n";
}
}
Compilation message (stderr)
nicegift.cpp: In function 'int main()':
nicegift.cpp:66:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < qu.size(); i++){
~~^~~~~~~~~~~
# | 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... |