Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int INF = 1e8;
- long long dp[1000010];
- int main() {
- int n;
- vector<int> ans;
- cin >> n;
- int a[n + 10];
- for (int i = 1; i <= n; ++i)
- cin >> a[i];
- sort(a + 1, a + 1 + n);
- int s;
- cin >> s;
- for (int i = 1; i <= s; ++i) {
- dp[i] = INF;
- for (int j = 1; j <= n; ++j)
- if (i >= a[j] && dp[i - a[j]] + 1 < dp[i])
- dp[i] = dp[i - a[j]] + 1;
- }
- if (dp[s] == INF) {
- cout << "No solution";
- return 0;
- }
- else {
- while(s > 0)
- for(int i = 1; i <= n; ++i)
- if (dp[s - a[i]] + 1 == dp[s]) {
- cout << a[i] << ' ';
- s -= a[i];
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement