Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <exception>
- #include <vector>
- using namespace std;
- vector<int> ans;
- vector<int> nom;
- vector<int> cnt;
- void say(int n)
- {
- cout << "solution: \n";
- for (int i = 0; i < n; ++i)
- {
- if (ans[i])
- cout << ans[i] << ' ' << nom[i] << endl;
- }
- }
- int sum = 0;
- bool fl = false;
- const int INF = 1e9 + 13;
- void solve(int s, int current = 0)
- {
- if (current == sum)
- {
- say(s);
- sum = INF;
- fl = true;
- }
- if (s == ans.size() || nom[s] > sum || fl)
- return;
- // запуск при количестве купюр = 0
- solve(s + 1, current);
- for (int i = 1; i <= cnt[s]; ++i)
- {
- ans[s] = i;
- current += nom[s];
- if (current > sum)
- {
- ans[s] = 0;
- return;
- }
- solve(s + 1, current);
- }
- ans[s] = 0;
- }
- int main()
- {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- int k;
- cin >> k;
- nom.resize(k);
- ans.resize(k);
- cnt.resize(k);
- for (int i = 0; i < k; ++i)
- cin >> nom[i];
- for (int i = 0; i < k; ++i)
- cin >> cnt[i];
- cin >> sum;
- solve(0);
- }
Add Comment
Please, Sign In to add comment