Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int n;
- int niza[105];
- int main()
- {
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> niza[i];
- }
- int x;
- cin >> x;
- int memo[x + 1];
- for(int i = 0; i <= x; i++) {
- memo[i] = 2000000;
- }
- memo[0] = 0;
- for(int sum = 0; sum <= x; sum++) {
- for(int i = 0; i < n; i++) {
- if(sum + niza[i] <= x) {
- memo[sum + niza[i]] = min(memo[sum + niza[i]], memo[sum] + 1);
- }
- }
- }
- cout << memo[x] << endl;
- return 0;
- }
- // sum = 0; i = 0
- // memo[1] = min(memo[1], memo[0] + 1) = 1
- // memo[2] = min(memo[2], memo[0] + 1) = 1
- // memo[5] = min(memo[5], memo[0] + 1) = 1
- // memo[7] = min(memo[7], memo[0] + 1) = 1
- // sum = 1
- // memo[2] = min(memo[2], memo[1] + 1) = 1
- // memo[3] = min(memo[3], memo[1] + 1) = 2
- // memo[6] = min(memo[6], memo[1] + 1) = 2
- // memo[8] = min(memo[8], memo[1] + 1) = 2
- // sum = 2
- // memo[3] = min(memo[3], memo[2] + 1) = 2
- // memo[4] = min(memo[4], memo[2] + 1) = 2
- // memo[7] = min(memo[7], memo[2] + 1) = 1
- // memo[9] = min(memo[9], memo[2] + 1) = 2
- // sum = 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement