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 memo[10001];
- int f(int x) {
- if(x == 0) {
- return 0; // ni trebaat uste 0 broevi
- }
- if(memo[x] != -1) {
- return memo[x];
- }
- int najmal = 2000000;
- for(int i = 0; i < n; i++) {
- if(x - niza[i] >= 0) {
- if(najmal > f(x - niza[i]) + 1) {
- najmal = f(x - niza[i]) + 1;
- }
- }
- }
- memo[x] = najmal;
- return najmal;
- }
- int main()
- {
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> niza[i];
- }
- int x;
- cin >> x;
- for(int i = 0; i <= x; i++) {
- memo[i] = -1;
- }
- cout << f(x) << endl;
- return 0;
- }
- // f(10)
- // f(9) + 1
- // f(9) --> f(8) + 1; f(7) + 1; f(4) + 1; f(2) + 1
- // f(8) -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement