/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 500+100, M = 1e5+10, K = 52, MX = 30;
int n, b[N], m, DP[N][N];
array<int, 2> a[N];
pair<bool, vector<vi>> f(vector<pii> A){
vector<vi> res(A.size());
for(int i = 1; i <= n; ++i){
if(A.size() < a[i][0]){
return pair<bool, vector<vi>>{false, res};
}
for(int j = 0; j < a[i][0]; ++j){
if(A[j].ff == 0){
return pair<bool, vector<vi>>{false, res};
}
A[j].ff--;
res[A[j].ss].pb(a[i][1]);
}
sort(all(A));
reverse(all(A));
}
return pair<bool, vector<vi>>{true, res};
}
void solve(){
cin >> n;
int s = 0;
for(int i = 1; i <= n; ++i){
cin >> a[i][0];
a[i][1] = i;
s += a[i][0];
}
sort(a+1, a+1+n);
cin >> m;
for(int i = 1; i <= m; ++i){
cin >> b[i];
}
int ans = MOD;
for(int i = 0; i <= s; ++i){
for(int j = 0; j <= s; ++j) DP[j][i] = -1;
}
DP[0][0] = 0;
for(int i = 1; i <= m; ++i){
for(int k = 1; k <= s; ++k){
for(int j = b[i]; j <= s; ++j){
if(DP[k][j] == -1 && DP[k - 1][j-b[i]] != -1){
DP[k][j] = b[i];
}
}
}
}
// for(int i = 0; i <= s; ++i){
// for(int j= 0; j <= s; ++j){
// cout << DP[i][j] << ' ';
// }
// en;
// }
for(int i = 1; i <= s; ++i){
if(DP[i][s] != -1){
vector<pii> A;
int v = s, cur = i;
while(v > 0 && cur > 0){
A.pb({DP[cur][v], cur-1});
v -= DP[cur][v];
--cur;
}
pair<bool, vector<vector<int>>> res = f(A);
if(res.ff){
cout << i << '\n';
for(auto v: res.ss){
cout << v.size() << ' ';
for(int u: v) cout << u << ' ';
en;
}
return;
}
}
}
cout << -1;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |