#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
using namespace std;
ll n,k,a[2000005],s,las,p,cur,maxi,l,r,mid,can,ans1,x1;
set<pair<ll,int> >st;
vector<vector<ll> >ans;
ll g[4000005],sz=0,wsz;
set<pair<ll,int> >::iterator it;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
bool ok = 1;
for(int i=1; i<=n; i++){
cin >> a[i];
s += a[i];
st.insert(mp(-a[i] , i));
}
if(s % k){
cout << "-1";
return 0;
}
ll sum = s;
ll ra = 0;
while(sum){
if(ra > 200000000){
cout << "-1";
return 0;
}
if((int)st.size() < k){
cout << "-1";
return 0;
}
p = 0,cur = 0;
maxi = -(*st.begin()).f;
g[sz] = -1;
sz++;
wsz = sz;
for(it = st.begin(); it != st.end(); it++){
p++;
if(p > k){
cur = (*it).s;
break;
}
las = (*it).s;
g[sz] = (*it).s;
sz++;
}
x1 = a[las];
ra += k * 100;
if(k > 1)x1 = (sum - maxi) / (k - 1);
can = (sum - a[cur] * k) / k;
if(can > x1)can = x1;
if(can > a[las])can = a[las];
sum -= can * k;
ans1++;
g[wsz - 1] = can;
for(int i=wsz; i<sz; i++){
st.erase(st.find(mp(-a[g[i]] , g[i])));
a[g[i]]-=can;
if(a[g[i]])st.insert(mp(-a[g[i]] , g[i]));
}
}
cout << ans1 << " ";
for(int i=0; i<sz; i++)
cout << g[i] << " ";
return 0;
}
Compilation message
nicegift.cpp: In function 'int main()':
nicegift.cpp:18:8: warning: unused variable 'ok' [-Wunused-variable]
bool ok = 1;
^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=4 |
2 |
Correct |
2 ms |
376 KB |
n=3 |
3 |
Correct |
2 ms |
376 KB |
n=3 |
4 |
Correct |
2 ms |
376 KB |
n=4 |
5 |
Correct |
2 ms |
376 KB |
n=4 |
6 |
Correct |
2 ms |
376 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=4 |
2 |
Correct |
2 ms |
376 KB |
n=3 |
3 |
Correct |
2 ms |
376 KB |
n=3 |
4 |
Correct |
2 ms |
376 KB |
n=4 |
5 |
Correct |
2 ms |
376 KB |
n=4 |
6 |
Correct |
2 ms |
376 KB |
n=2 |
7 |
Correct |
2 ms |
376 KB |
n=5 |
8 |
Correct |
2 ms |
376 KB |
n=8 |
9 |
Correct |
2 ms |
376 KB |
n=14 |
10 |
Correct |
2 ms |
376 KB |
n=11 |
11 |
Correct |
34 ms |
4984 KB |
n=50000 |
12 |
Correct |
29 ms |
4856 KB |
n=50000 |
13 |
Correct |
2 ms |
376 KB |
n=10 |
14 |
Correct |
2 ms |
376 KB |
n=685 |
15 |
Correct |
2 ms |
376 KB |
n=623 |
16 |
Correct |
2 ms |
504 KB |
n=973 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=4 |
2 |
Correct |
2 ms |
376 KB |
n=3 |
3 |
Correct |
2 ms |
376 KB |
n=3 |
4 |
Correct |
2 ms |
376 KB |
n=4 |
5 |
Correct |
2 ms |
376 KB |
n=4 |
6 |
Correct |
2 ms |
376 KB |
n=2 |
7 |
Correct |
2 ms |
376 KB |
n=5 |
8 |
Correct |
2 ms |
376 KB |
n=8 |
9 |
Correct |
2 ms |
376 KB |
n=14 |
10 |
Correct |
2 ms |
376 KB |
n=11 |
11 |
Correct |
34 ms |
4984 KB |
n=50000 |
12 |
Correct |
29 ms |
4856 KB |
n=50000 |
13 |
Correct |
2 ms |
376 KB |
n=10 |
14 |
Correct |
2 ms |
376 KB |
n=685 |
15 |
Correct |
2 ms |
376 KB |
n=623 |
16 |
Correct |
2 ms |
504 KB |
n=973 |
17 |
Correct |
2 ms |
504 KB |
n=989 |
18 |
Correct |
2 ms |
376 KB |
n=563 |
19 |
Correct |
2 ms |
504 KB |
n=592 |
20 |
Correct |
2 ms |
504 KB |
n=938 |
21 |
Correct |
2 ms |
504 KB |
n=747 |
22 |
Correct |
2 ms |
376 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
909 ms |
96772 KB |
n=1000000 |
2 |
Correct |
586 ms |
62204 KB |
n=666666 |
3 |
Correct |
330 ms |
36160 KB |
n=400000 |
4 |
Correct |
228 ms |
25592 KB |
n=285714 |
5 |
Correct |
14 ms |
2040 KB |
n=20000 |
6 |
Correct |
140 ms |
16128 KB |
n=181818 |
7 |
Correct |
8 ms |
1272 KB |
n=10000 |
8 |
Correct |
7 ms |
1016 KB |
n=6666 |
9 |
Correct |
4 ms |
760 KB |
n=4000 |
10 |
Correct |
7 ms |
760 KB |
n=2857 |
11 |
Correct |
3 ms |
508 KB |
n=2000 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n=4 |
2 |
Correct |
2 ms |
376 KB |
n=3 |
3 |
Correct |
2 ms |
376 KB |
n=3 |
4 |
Correct |
2 ms |
376 KB |
n=4 |
5 |
Correct |
2 ms |
376 KB |
n=4 |
6 |
Correct |
2 ms |
376 KB |
n=2 |
7 |
Correct |
2 ms |
376 KB |
n=5 |
8 |
Correct |
2 ms |
376 KB |
n=8 |
9 |
Correct |
2 ms |
376 KB |
n=14 |
10 |
Correct |
2 ms |
376 KB |
n=11 |
11 |
Correct |
34 ms |
4984 KB |
n=50000 |
12 |
Correct |
29 ms |
4856 KB |
n=50000 |
13 |
Correct |
2 ms |
376 KB |
n=10 |
14 |
Correct |
2 ms |
376 KB |
n=685 |
15 |
Correct |
2 ms |
376 KB |
n=623 |
16 |
Correct |
2 ms |
504 KB |
n=973 |
17 |
Correct |
2 ms |
504 KB |
n=989 |
18 |
Correct |
2 ms |
376 KB |
n=563 |
19 |
Correct |
2 ms |
504 KB |
n=592 |
20 |
Correct |
2 ms |
504 KB |
n=938 |
21 |
Correct |
2 ms |
504 KB |
n=747 |
22 |
Correct |
2 ms |
376 KB |
n=991 |
23 |
Correct |
909 ms |
96772 KB |
n=1000000 |
24 |
Correct |
586 ms |
62204 KB |
n=666666 |
25 |
Correct |
330 ms |
36160 KB |
n=400000 |
26 |
Correct |
228 ms |
25592 KB |
n=285714 |
27 |
Correct |
14 ms |
2040 KB |
n=20000 |
28 |
Correct |
140 ms |
16128 KB |
n=181818 |
29 |
Correct |
8 ms |
1272 KB |
n=10000 |
30 |
Correct |
7 ms |
1016 KB |
n=6666 |
31 |
Correct |
4 ms |
760 KB |
n=4000 |
32 |
Correct |
7 ms |
760 KB |
n=2857 |
33 |
Correct |
3 ms |
508 KB |
n=2000 |
34 |
Correct |
24 ms |
2680 KB |
n=23514 |
35 |
Correct |
24 ms |
2760 KB |
n=23514 |
36 |
Correct |
3 ms |
504 KB |
n=940 |
37 |
Correct |
2 ms |
376 KB |
n=2 |
38 |
Correct |
66 ms |
8952 KB |
n=100000 |
39 |
Correct |
67 ms |
9080 KB |
n=100000 |
40 |
Correct |
2 ms |
376 KB |
n=10 |
41 |
Correct |
2 ms |
376 KB |
n=100 |
42 |
Correct |
6 ms |
504 KB |
n=1000 |
43 |
Correct |
1231 ms |
102904 KB |
n=1000000 |
44 |
Execution timed out |
2069 ms |
95516 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |