Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <set>
- #include <cmath>
- #include <map>
- using namespace std;
- vector <int> a;
- int main()
- {
- // freopen("cakes.in", "r", stdin);
- // freopen("cakes.out", "w", stdout);
- int n;
- cin >> n;
- a.resize(n);
- for (int i = 0; i < n; i++)
- cin >> a[i];
- bool dp[301][301]; //dp[i][j] - за i людей набрать сумму j
- bool dp2[301][301];
- int p[301][301];
- for (int q = 0; q <= n; q++)
- for (int j = 0; j <= n; j++)
- dp[q][j] = false, p[q][j] = -1;
- dp[0][0] = true;
- for (int i = 0; i < n; i++)
- {
- for (int q = 0; q <= n; q++)
- for (int j = 0; j <= n; j++)
- dp2[q][j] = false;
- for (int q = 0; q < n; q++)
- for (int j = 0; j <= n; j++)
- {
- if (dp[q][j])
- {
- if (j + a[i] <= n)
- {
- dp2[q + 1][j + a[i]] |= dp[q][j];
- if (p[q + 1][j + a[i]] == -1)
- p[q + 1][j + a[i]] = i;
- }
- else break;
- }
- }
- for (int q = 0; q <= n; q++)
- for (int j = 0; j <= n; j++)
- dp[q][j] |= dp2[q][j];
- }
- int res = -1;
- for (int q = 0; q <= n; q++)
- if (dp[q][n - q])
- {
- res = q;
- break;
- }
- if (res == -1)
- {
- cout << "NO\n";
- return 0;
- }
- else
- {
- cout << "YES\n";
- pair <int, int> cur = make_pair(res, n - res);
- vector <int> best;
- while (cur.first > 0)
- {
- int to = p[cur.first][cur.second];
- best.push_back(to);
- cur.first--;
- cur.second = cur.second - a[to];
- }
- vector <int> result;
- result.resize(n);
- for (int i = 0; i < n; i++)
- result[i] = 0;
- for (int i = 0; i < best.size(); i++)
- result[best[i]] = 1;
- for (int i = 0; i < n; i++)
- cout << result[i];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement