Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int, int>
- #define pb(x) push_back(x)
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvl(vector <ll> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvv(vector <vector <int> > &v) {
- for (auto x : v) cv(x);
- cout << "\n";
- }
- void cvb(vector <bool> v) {
- for (bool x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvs(vector <string> v) {
- for (auto a : v) {
- cout << a << "\n";
- }
- }
- void cvp(vector <pii> a) {
- for (auto p : a) {
- cout << p.first << ' ' << p.second << "\n";
- }
- cout << "\n";
- }
- bool sh = 0;
- vector <vector <bool> > can1, can2;
- vector <vector <pii> > pr1, pr2;
- //vector <pii> ans1, ans2;
- vector <int> ans1, ans2;
- vector <int> w1, w2;
- void bld(vector <vector <bool> > &can, int &M, int &N, vector <vector <pii> > &pr, vector <int> &w) {
- int n, m = 0;
- cin >> n;
- N = n;
- w.resize(n + 1);
- for (int i = 1; i <= n; ++i) {
- cin >> w[i];
- m += w[i];
- }
- M = m;
- can.assign(m + 1, vector <bool> (n + 1, 0));
- pr.assign(m + 1, vector <pii> (n + 1, {-1, -1}));
- for (int i = 0; i <= n; ++i) {
- can[0][i] = 1;
- }
- for (int i = 1; i <= m; ++i) {
- for (int j = 1; j <= n; ++j) {
- can[i][j] = can[i][j - 1];
- pr[i][j] = pr[i][j - 1];
- if (i - w[j] < 0) {
- continue;
- }
- if (!can[i - w[j]][j - 1]) {
- continue;
- }
- can[i][j] = 1;
- pr[i][j] = {i - w[j], j - 1};
- }
- }
- }
- int main() {
- /*ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);*/
- int s; cin >> s;
- int m1, m2, n1, n2;
- bld(can1, m1, n1, pr1, w1);
- bld(can2, m2, n2, pr2, w2);
- for (int i = s; i <= m1; ++i) {
- if (i - s > m2) {
- continue;
- }
- if (can1[i][n1] && can2[i - s][n2]) {
- if (sh) {
- cout << "i i - s = " << i << ' ' << i - s << "\n";
- }
- pii p;
- p = {i, n1};
- p = pr1[p.first][p.second];
- int s0 = 0;
- while (p.first != 0 && p != pii{-1, -1}) {
- ans1.push_back(w1[p.second]);
- s0 += w1[p.second];
- p = pr1[p.first][p.second];
- }
- ans1.push_back(i - s0);
- if (sh) {
- cout << "s0 = " << s0 << "\n";
- }
- s0 = 0;
- p = {i - s, n2};
- p = pr2[p.first][p.second];
- while (p.first != 0 && p != pii{-1, -1}) {
- ans2.push_back(w2[p.second]);
- s0 += w2[p.second];
- p = pr2[p.first][p.second];
- }
- ans2.push_back(i - s - s0);
- if (sh) {
- cout << "s0 = " << s0 << "\n";
- }
- if (sh) {
- cout << "ans1\n";
- cv(ans1);
- cout << "ans2\n";
- cv(ans2);
- }
- for (int i: ans1 ) {
- cout << "+" << i << " ";
- }
- for (int i: ans2) {
- cout << "-" << i << " ";
- }
- //break;
- exit(0);
- }
- }
- //cout << "done\n";
- if (sh) {
- cout << "can1\n";
- for (auto v: can1) {
- cvb(v);
- }
- cout << "\n";
- cout << "pr1\n";
- for (auto v: pr1) {
- for (auto p: v) {
- cout << "(" << p.first << "," << p.second << ") ";
- } cout << "\n";
- } cout << "\n";
- cout << "can2\n";
- for (auto v: can2) {
- cvb(v);
- }
- cout << "\n";
- cout << "pr2\n";
- for (auto v: pr2) {
- for (auto p: v) {
- cout << "(" << p.first << "," << p.second << ") ";
- } cout << "\n";
- } cout << "\n";
- }
- cout << "Impossible";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement