#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int m;
cin >> m;
vector<int> b(m);
for (int i = 0; i < m; i++) {
cin >> b[i];
}
const int MAX = 505;
vector<int> mn(MAX, MAX);
mn[0] = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j + b[i] < MAX; j++) {
mn[j + b[i]] = min(mn[j + b[i]], mn[j] + 1);
}
}
vector<int> cnt(MAX);
for (int i = 0; i < n; i++) {
cnt[a[i]] += 1;
}
for (int i = MAX - 1; i > 0; i--) {
cnt[i - 1] += cnt[i];
}
int mx = *max_element(a.begin(), a.end());
vector<vector<int>> dp(MAX, vector<int>(MAX, MAX));
vector<vector<pair<int, int>>> prv(MAX, vector<pair<int, int>>(MAX, {-1, -1}));
dp[1][0] = 0;
auto Update = [&](int i, int j, int x, int y, int f) {
if (dp[i][j] > dp[x][y] + f) {
dp[i][j] = dp[x][y] + f;
prv[i][j] = {x, y};
}
};
for (int c = 1; c <= mx; c++) {
for (int x = 0; x <= cnt[c]; x++) {
if (dp[c][x] == MAX) {
continue;
}
for (int i = 0; i < m; i++) {
if (b[i] > min(x, cnt[c + 1]) + (cnt[c] - x)) {
continue;
}
int y = x + b[i];
if (y <= cnt[c]) {
Update(c, y, c, x, 1);
} else {
if (y <= cnt[c] + cnt[c + 1]) {
Update(c + 1, y - cnt[c], c, x, 1);
}
}
}
}
Update(c + 1, 0, c, cnt[c], 0);
}
if (dp[mx + 1][0] == MAX) {
cout << -1 << '\n';
return 0;
}
int x = mx + 1, y = 0;
vector<int> bv;
while (x > 0) {
int pv;
if (prv[x][y].first == -1) {
pv = 0;
} else if (prv[x][y].first == x) {
pv = prv[x][y].second;
} else {
pv = prv[x][y].second - cnt[x - 1];
}
if (y != pv) {
bv.push_back(y - pv);
}
pair<int, int> p = prv[x][y];
x = p.first;
y = p.second;
}
set<pair<int, int>> st;
for (int i = 0; i < n; i++) {
st.emplace(a[i], i);
}
vector<vector<int>> res;
for (int v : bv) {
vector<int> t;
res.push_back({});
for (int iter = 0; iter < v; iter++) {
auto it = prev(st.end());
int i = it->second;
st.erase(it);
res.back().push_back(i);
a[i] -= 1;
if (a[i] > 0) {
t.push_back(i);
}
}
for (int i : t) {
st.emplace(a[i], i);
}
}
cout << (int) res.size() << '\n';
for (int i = 0; i < (int) res.size(); i++) {
cout << (int) res[i].size() << " ";
for (int j = 0; j < (int) res[i].size(); j++) {
cout << res[i][j] + 1 << " ";
}
cout << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3420 KB |
Output is correct |
7 |
Correct |
2 ms |
3420 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
9 |
Correct |
1 ms |
3420 KB |
Output is correct |
10 |
Correct |
2 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
12 |
Correct |
2 ms |
3420 KB |
Output is correct |
13 |
Correct |
1 ms |
3420 KB |
Output is correct |
14 |
Correct |
1 ms |
3420 KB |
Output is correct |
15 |
Correct |
1 ms |
3420 KB |
Output is correct |
16 |
Correct |
1 ms |
3420 KB |
Output is correct |
17 |
Correct |
1 ms |
3420 KB |
Output is correct |
18 |
Correct |
1 ms |
3420 KB |
Output is correct |
19 |
Correct |
1 ms |
3420 KB |
Output is correct |
20 |
Correct |
2 ms |
3420 KB |
Output is correct |
21 |
Correct |
1 ms |
3272 KB |
Output is correct |
22 |
Correct |
2 ms |
3420 KB |
Output is correct |
23 |
Correct |
1 ms |
3416 KB |
Output is correct |
24 |
Correct |
2 ms |
3416 KB |
Output is correct |
25 |
Correct |
2 ms |
3420 KB |
Output is correct |
26 |
Correct |
2 ms |
3416 KB |
Output is correct |
27 |
Correct |
2 ms |
3360 KB |
Output is correct |
28 |
Correct |
2 ms |
3420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3420 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
2 ms |
3268 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
2 ms |
3420 KB |
Output is correct |
6 |
Correct |
3 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
9 |
Runtime error |
3 ms |
6748 KB |
Execution killed with signal 11 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3428 KB |
Output is correct |
3 |
Correct |
2 ms |
3420 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3420 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
9 |
Correct |
1 ms |
3420 KB |
Output is correct |
10 |
Correct |
2 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
12 |
Correct |
1 ms |
3420 KB |
Output is correct |
13 |
Correct |
3 ms |
3420 KB |
Output is correct |
14 |
Correct |
1 ms |
3272 KB |
Output is correct |
15 |
Correct |
2 ms |
3420 KB |
Output is correct |
16 |
Correct |
1 ms |
3420 KB |
Output is correct |
17 |
Correct |
2 ms |
3420 KB |
Output is correct |
18 |
Incorrect |
1 ms |
3420 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3420 KB |
Output is correct |
7 |
Correct |
2 ms |
3420 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
9 |
Correct |
1 ms |
3420 KB |
Output is correct |
10 |
Correct |
2 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
12 |
Correct |
2 ms |
3420 KB |
Output is correct |
13 |
Correct |
1 ms |
3420 KB |
Output is correct |
14 |
Correct |
1 ms |
3420 KB |
Output is correct |
15 |
Correct |
1 ms |
3420 KB |
Output is correct |
16 |
Correct |
1 ms |
3420 KB |
Output is correct |
17 |
Correct |
1 ms |
3420 KB |
Output is correct |
18 |
Correct |
1 ms |
3420 KB |
Output is correct |
19 |
Correct |
1 ms |
3420 KB |
Output is correct |
20 |
Correct |
2 ms |
3420 KB |
Output is correct |
21 |
Correct |
1 ms |
3272 KB |
Output is correct |
22 |
Correct |
2 ms |
3420 KB |
Output is correct |
23 |
Correct |
1 ms |
3416 KB |
Output is correct |
24 |
Correct |
2 ms |
3416 KB |
Output is correct |
25 |
Correct |
2 ms |
3420 KB |
Output is correct |
26 |
Correct |
2 ms |
3416 KB |
Output is correct |
27 |
Correct |
2 ms |
3360 KB |
Output is correct |
28 |
Correct |
2 ms |
3420 KB |
Output is correct |
29 |
Correct |
1 ms |
3420 KB |
Output is correct |
30 |
Correct |
1 ms |
3428 KB |
Output is correct |
31 |
Correct |
2 ms |
3420 KB |
Output is correct |
32 |
Correct |
1 ms |
3420 KB |
Output is correct |
33 |
Correct |
1 ms |
3420 KB |
Output is correct |
34 |
Correct |
1 ms |
3420 KB |
Output is correct |
35 |
Correct |
1 ms |
3420 KB |
Output is correct |
36 |
Correct |
1 ms |
3420 KB |
Output is correct |
37 |
Correct |
1 ms |
3420 KB |
Output is correct |
38 |
Correct |
2 ms |
3420 KB |
Output is correct |
39 |
Correct |
1 ms |
3420 KB |
Output is correct |
40 |
Correct |
1 ms |
3420 KB |
Output is correct |
41 |
Correct |
3 ms |
3420 KB |
Output is correct |
42 |
Correct |
1 ms |
3272 KB |
Output is correct |
43 |
Correct |
2 ms |
3420 KB |
Output is correct |
44 |
Correct |
1 ms |
3420 KB |
Output is correct |
45 |
Correct |
2 ms |
3420 KB |
Output is correct |
46 |
Incorrect |
1 ms |
3420 KB |
Output isn't correct |
47 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3420 KB |
Output is correct |
7 |
Correct |
2 ms |
3420 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
9 |
Correct |
1 ms |
3420 KB |
Output is correct |
10 |
Correct |
2 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
12 |
Correct |
2 ms |
3420 KB |
Output is correct |
13 |
Correct |
1 ms |
3420 KB |
Output is correct |
14 |
Correct |
1 ms |
3420 KB |
Output is correct |
15 |
Correct |
1 ms |
3420 KB |
Output is correct |
16 |
Correct |
1 ms |
3420 KB |
Output is correct |
17 |
Correct |
1 ms |
3420 KB |
Output is correct |
18 |
Correct |
1 ms |
3420 KB |
Output is correct |
19 |
Correct |
1 ms |
3420 KB |
Output is correct |
20 |
Correct |
2 ms |
3420 KB |
Output is correct |
21 |
Correct |
1 ms |
3272 KB |
Output is correct |
22 |
Correct |
2 ms |
3420 KB |
Output is correct |
23 |
Correct |
1 ms |
3416 KB |
Output is correct |
24 |
Correct |
2 ms |
3416 KB |
Output is correct |
25 |
Correct |
2 ms |
3420 KB |
Output is correct |
26 |
Correct |
2 ms |
3416 KB |
Output is correct |
27 |
Correct |
2 ms |
3360 KB |
Output is correct |
28 |
Correct |
2 ms |
3420 KB |
Output is correct |
29 |
Correct |
1 ms |
3420 KB |
Output is correct |
30 |
Correct |
1 ms |
3428 KB |
Output is correct |
31 |
Correct |
2 ms |
3420 KB |
Output is correct |
32 |
Correct |
1 ms |
3420 KB |
Output is correct |
33 |
Correct |
1 ms |
3420 KB |
Output is correct |
34 |
Correct |
1 ms |
3420 KB |
Output is correct |
35 |
Correct |
1 ms |
3420 KB |
Output is correct |
36 |
Correct |
1 ms |
3420 KB |
Output is correct |
37 |
Correct |
1 ms |
3420 KB |
Output is correct |
38 |
Correct |
2 ms |
3420 KB |
Output is correct |
39 |
Correct |
1 ms |
3420 KB |
Output is correct |
40 |
Correct |
1 ms |
3420 KB |
Output is correct |
41 |
Correct |
3 ms |
3420 KB |
Output is correct |
42 |
Correct |
1 ms |
3272 KB |
Output is correct |
43 |
Correct |
2 ms |
3420 KB |
Output is correct |
44 |
Correct |
1 ms |
3420 KB |
Output is correct |
45 |
Correct |
2 ms |
3420 KB |
Output is correct |
46 |
Incorrect |
1 ms |
3420 KB |
Output isn't correct |
47 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Output is correct |
2 |
Correct |
1 ms |
3420 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3420 KB |
Output is correct |
7 |
Correct |
2 ms |
3420 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
9 |
Correct |
1 ms |
3420 KB |
Output is correct |
10 |
Correct |
2 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
12 |
Correct |
2 ms |
3420 KB |
Output is correct |
13 |
Correct |
1 ms |
3420 KB |
Output is correct |
14 |
Correct |
1 ms |
3420 KB |
Output is correct |
15 |
Correct |
1 ms |
3420 KB |
Output is correct |
16 |
Correct |
1 ms |
3420 KB |
Output is correct |
17 |
Correct |
1 ms |
3420 KB |
Output is correct |
18 |
Correct |
1 ms |
3420 KB |
Output is correct |
19 |
Correct |
1 ms |
3420 KB |
Output is correct |
20 |
Correct |
2 ms |
3420 KB |
Output is correct |
21 |
Correct |
1 ms |
3272 KB |
Output is correct |
22 |
Correct |
2 ms |
3420 KB |
Output is correct |
23 |
Correct |
1 ms |
3416 KB |
Output is correct |
24 |
Correct |
2 ms |
3416 KB |
Output is correct |
25 |
Correct |
2 ms |
3420 KB |
Output is correct |
26 |
Correct |
2 ms |
3416 KB |
Output is correct |
27 |
Correct |
2 ms |
3360 KB |
Output is correct |
28 |
Correct |
2 ms |
3420 KB |
Output is correct |
29 |
Correct |
2 ms |
3420 KB |
Output is correct |
30 |
Correct |
2 ms |
3420 KB |
Output is correct |
31 |
Correct |
2 ms |
3268 KB |
Output is correct |
32 |
Correct |
1 ms |
3420 KB |
Output is correct |
33 |
Correct |
2 ms |
3420 KB |
Output is correct |
34 |
Correct |
3 ms |
3420 KB |
Output is correct |
35 |
Correct |
1 ms |
3420 KB |
Output is correct |
36 |
Correct |
1 ms |
3420 KB |
Output is correct |
37 |
Runtime error |
3 ms |
6748 KB |
Execution killed with signal 11 |
38 |
Halted |
0 ms |
0 KB |
- |