Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int n, m;
- vector<int> a;
- vector<pair<int, int>>b;
- ll f(int d){
- vector<vector<vector<ll>>> dp(2, vector<vector<ll>>(n+1, vector<ll> (m+1)));
- vector<vector<vector<ll>>> dp2(2, vector<vector<ll>>(n+1, vector<ll>(m+1)));
- dp[0][0][0] = dp[1][0][0] = 10000;
- for (int j = 1; j <= m; ++j){
- dp[1][0][j] = dp[1][0][j-1] - b[j].second;
- dp2[1][0][j] = dp[1][0][j-1]*b[j].first + dp2[1][0][j-1];
- }
- for (int i = 1; i <= n; ++i){
- dp[0][i][0] = dp[0][i-1][0] + a[i];
- dp2[0][i][0] = dp2[0][i-1][0];
- }
- for (int i = 1; i <= n; ++i){
- for (int j = 0; j <= m; ++j){
- if (j == 0){
- dp[0][i][j] = max(dp[0][i-1][j], dp[1][i-1][j]) + a[i];
- dp2[0][i][j] = max(dp2[0][i-1][j], dp2[1][i-1][j]);
- }
- else {
- dp[0][i][j] = max(dp[0][i-1][j], dp[1][i-1][j]) + a[i];
- dp[1][i][j] = max(dp[0][i][j-1], dp[1][i][j-1]) - b[j].second;
- dp2[0][i][j] = max(dp2[0][i-1][j], dp2[1][i-1][j]);
- if (dp2[1][i][j - 1] + dp[1][i][j - 1] * b[j].first >
- dp2[0][i][j-1] + dp[0][i][j-1] * b[j].first) {
- dp2[1][i][j] = dp2[1][i][j - 1] + dp[1][i][j - 1] * b[j].first;
- } else dp2[1][i][j] = dp2[0][i][j-1] + dp[0][i][j-1] * b[j].first;
- }
- }
- }
- ll ma = 0;
- for (int i = 0; i <= n; ++i){
- if (d-i <= m && d-i >= 0){
- ma = max({ma, dp2[0][i][d-i], dp2[1][i][d-i]});
- }
- }
- return ma;
- }
- int main() {
- iostream::sync_with_stdio(false); cout.tie(0); cin.tie(0);
- cin >> n;
- a.resize(n+1);
- for (int i = 1; i <= n; ++i)
- cin >> a[i];
- cin >> m;
- b.resize(m+1);
- for (int i = 1; i <= m; ++i){
- cin >> b[i].first >> b[i].second;
- }
- sort(a.begin(), a.end(), [](ll p1, ll p2){
- if (p1 == 0) return true;
- else if (p2 == 0) return false;
- return p1 > p2;
- });
- int q; cin >> q;
- auto t = b;
- while (q--){
- int d; cin >> d;
- ll ma = f(d);
- while(next_permutation(t.begin(), t.end())){
- b = t;
- for (int i = 0; i < m; ++i){
- if (b[i] == make_pair(0, 0)) {
- swap(b[0], b[i]);
- break;
- }
- }
- ma = max(ma, f(d));
- }
- cout << ma << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement