Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int N = 1e4 + 5;
- const int inf = 1e9 + 7;
- int n, m, a[11], b[11], dp[11][N];
- int main() {
- for (int i = 0; i < 11; i++) {
- for (int j = 0; j < N; j++) dp[i][j] = inf;
- }
- cin >> n;
- dp[0][0] = 0;
- for (int i = 1; i <= n; i++) {
- cin >> a[i] >> b[i];
- for (int j = 0; j < N; j++) dp[i][j] = dp[i - 1][j];
- for (int j = 1; j <= b[i]; j++) {
- int num = j * a[i];
- for (int k = N - 1; k >= num; k--) {
- dp[i][k] = min(dp[i][k], dp[i - 1][k - num] + j);
- }
- }
- }
- cin >> m;
- while (m--) {
- int x;
- cin >> x;
- if (dp[n][x] == inf) {
- cout << "Impossible\n";
- continue;
- }
- cout << dp[n][x] << " ";
- int cnt = dp[n][x];
- for (int i = n; i >= 1; i--) {
- for (int j = b[i]; j >= 1; j--) {
- int num = j * a[i];
- if (dp[i][x] == dp[i - 1][x - num] + j) {
- x -= num;
- while (j--) {
- if (--cnt)
- cout << a[i] << " ";
- else
- cout << a[i];
- }
- break;
- }
- }
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement