Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <string>
- #include <map>
- #include <set>
- #include <unordered_map>
- #include <unordered_set>
- #include <stack>
- #include <queue>
- #include <deque>
- using namespace std;
- #define ll long long
- #define ld long double
- #define mp make_pair
- #define pii pair<int, int>
- #define ft first
- #define sd second
- int n;
- vector<ll> a;
- vector<ll> dp;
- vector<ll> makedp;
- pair<ll, ll> countans(ll b) {
- if (b == 0)
- return mp(0, 0);
- int i = upper_bound(a.begin(), a.end(), b) - a.begin() - 1;
- ll c = b / a[i];
- pair<ll, ll> ans = mp((c - 1) * a[i] + makedp[i], c - 1 + dp[i]);
- auto otherans = countans(b - c * a[i]);
- if (otherans.second + c > ans.second) {
- ans = otherans;
- ans.second += c;
- ans.first += (c) * a[i];
- }
- return ans;
- }
- int32_t main() {
- cin.tie(NULL);
- cout.tie(NULL);
- ios_base::sync_with_stdio(false);
- cin >> n;
- a.resize(n);
- for (int i = 0; i < n; i++)
- cin >> a[i];
- dp.resize(n); // ans if b is equal to a[i] - 1;
- makedp.resize(n);
- dp[0] = 0;
- for (int i = 1; i < n; i++) {
- auto t = countans(a[i] - 1);
- dp[i] = t.second;
- makedp[i] = t.first;
- }
- int q;
- cin >> q;
- for (; q > 0; q--) {
- ll b;
- cin >> b;
- auto ans = countans(b);
- cout << ans.ft << ' ' << ans.sd << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement