#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define LLA(x) x.rbegin(),x.rend()
#define print(x) for(auto el:x)cout<<el<<' ';cout<<"\n";
#define printPII(x) for(auto [a,b]:x)cout<<a<<","<<b<<' ';cout<<'\n';
using namespace std;
using ll = long long;
using PII = pair <ll, ll>;
ll n, k, onePick = 1;
bool impossible;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
cin >> n >> k;
vector <vector <ll> > ans;
set <PII> bfs;
ll sum = 0, dbg = 0;
for (int i = 1; i <= n; i++) {
ll x; cin >> x;
sum += x;
bfs.insert({x, i});
}
if (sum % k != 0) {
cout << "-1\n";
return 0;
}
while(!impossible && sum > 0) {
vector <ll> pick;
vector <PII> add;
for (int i = 0; i < k; i++) {
auto it = --bfs.end();
PII cur = *it;
bfs.erase(it);
pick.pb(cur.ss);
if (cur.ff < 0) {
impossible = 1;
break;
}
add.pb(cur);
}
ll mini = add[0].ff, And = add[0].ff;
for (int i = 1; i < k; i++) {
mini = min(mini, add[i].ff);
And &= add[i].ff;
}
ll mxBit = 1;
for (int i = 1; i < 60; i++)
if (mini & (1ll<<i)) mxBit = i;
mxBit = (1<<(mxBit-1));
if (And * k == sum) mxBit = And;
pick.pb(mxBit);
swap(pick[0], pick.back());
for (auto& [a, b] : add) {
a -= mxBit;
sum -= mxBit;
bfs.insert({a, b});
}
ans.pb(pick);
// for (auto& [a, b] : bfs) {
// cout << a << ',' << b << ' ';
// }
// cout << '\n';
// if (dbg++ == 30) break;
}
// int dbg = 0;
if (impossible) {
cout << "-1\n";
return 0;
}
cout << ans.size() << '\n';
for (auto el : ans) {
print(el);
}
}
Compilation message
nicegift.cpp: In function 'int main()':
nicegift.cpp:22:14: warning: unused variable 'dbg' [-Wunused-variable]
22 | ll sum = 0, dbg = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
n=4 |
2 |
Correct |
0 ms |
204 KB |
n=3 |
3 |
Correct |
0 ms |
204 KB |
n=3 |
4 |
Correct |
0 ms |
204 KB |
n=4 |
5 |
Correct |
0 ms |
204 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
n=4 |
2 |
Correct |
0 ms |
204 KB |
n=3 |
3 |
Correct |
0 ms |
204 KB |
n=3 |
4 |
Correct |
0 ms |
204 KB |
n=4 |
5 |
Correct |
0 ms |
204 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
7 |
Correct |
1 ms |
312 KB |
n=5 |
8 |
Correct |
1 ms |
316 KB |
n=8 |
9 |
Correct |
0 ms |
204 KB |
n=14 |
10 |
Correct |
0 ms |
204 KB |
n=11 |
11 |
Correct |
41 ms |
5624 KB |
n=50000 |
12 |
Correct |
35 ms |
5204 KB |
n=50000 |
13 |
Correct |
0 ms |
204 KB |
n=10 |
14 |
Correct |
5 ms |
588 KB |
n=685 |
15 |
Correct |
4 ms |
572 KB |
n=623 |
16 |
Correct |
4 ms |
716 KB |
n=973 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
n=4 |
2 |
Correct |
0 ms |
204 KB |
n=3 |
3 |
Correct |
0 ms |
204 KB |
n=3 |
4 |
Correct |
0 ms |
204 KB |
n=4 |
5 |
Correct |
0 ms |
204 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
7 |
Correct |
1 ms |
312 KB |
n=5 |
8 |
Correct |
1 ms |
316 KB |
n=8 |
9 |
Correct |
0 ms |
204 KB |
n=14 |
10 |
Correct |
0 ms |
204 KB |
n=11 |
11 |
Correct |
41 ms |
5624 KB |
n=50000 |
12 |
Correct |
35 ms |
5204 KB |
n=50000 |
13 |
Correct |
0 ms |
204 KB |
n=10 |
14 |
Correct |
5 ms |
588 KB |
n=685 |
15 |
Correct |
4 ms |
572 KB |
n=623 |
16 |
Correct |
4 ms |
716 KB |
n=973 |
17 |
Correct |
4 ms |
668 KB |
n=989 |
18 |
Correct |
2 ms |
332 KB |
n=563 |
19 |
Correct |
2 ms |
440 KB |
n=592 |
20 |
Correct |
3 ms |
460 KB |
n=938 |
21 |
Correct |
3 ms |
460 KB |
n=747 |
22 |
Correct |
3 ms |
460 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2066 ms |
164416 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
n=4 |
2 |
Correct |
0 ms |
204 KB |
n=3 |
3 |
Correct |
0 ms |
204 KB |
n=3 |
4 |
Correct |
0 ms |
204 KB |
n=4 |
5 |
Correct |
0 ms |
204 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
7 |
Correct |
1 ms |
312 KB |
n=5 |
8 |
Correct |
1 ms |
316 KB |
n=8 |
9 |
Correct |
0 ms |
204 KB |
n=14 |
10 |
Correct |
0 ms |
204 KB |
n=11 |
11 |
Correct |
41 ms |
5624 KB |
n=50000 |
12 |
Correct |
35 ms |
5204 KB |
n=50000 |
13 |
Correct |
0 ms |
204 KB |
n=10 |
14 |
Correct |
5 ms |
588 KB |
n=685 |
15 |
Correct |
4 ms |
572 KB |
n=623 |
16 |
Correct |
4 ms |
716 KB |
n=973 |
17 |
Correct |
4 ms |
668 KB |
n=989 |
18 |
Correct |
2 ms |
332 KB |
n=563 |
19 |
Correct |
2 ms |
440 KB |
n=592 |
20 |
Correct |
3 ms |
460 KB |
n=938 |
21 |
Correct |
3 ms |
460 KB |
n=747 |
22 |
Correct |
3 ms |
460 KB |
n=991 |
23 |
Execution timed out |
2066 ms |
164416 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |