#include <bits/stdc++.h>
using namespace std;
#define ll long long
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll INF = (ll)1e15;
const int N = 70;
struct El {
ll p, idx;
};
ll dp[N+5][N+5][N+5];
vector<int> max_coupons(int a, vector<int> p, vector<int> t){
int n = (int)p.size();
vector<El> v1, v2, v3, v4;
for(int i = 0; i < n; i++){
if(t[i] == 1) v1.push_back({p[i], i});
else if(t[i] == 2) v2.push_back({p[i], i});
else if(t[i] == 3) v3.push_back({p[i], i});
else v4.push_back({p[i], i});
}
sort(v1.begin(), v1.end(), [&](auto l, auto r){ return l.p < r.p; });
sort(v2.begin(), v2.end(), [&](auto l, auto r){ return l.p < r.p; });
sort(v3.begin(), v3.end(), [&](auto l, auto r){ return l.p < r.p; });
sort(v4.begin(), v4.end(), [&](auto l, auto r){ return l.p < r.p; });
int osz = (int)v1.size();
vector<ll> pref1(osz);
for(int i = 0; i < osz; i++){
if(i == 0) pref1[i] = v1[i].p;
else pref1[i] = pref1[i-1] + v1[i].p;
}
auto get = [&](ll cnt){
return (upper_bound(pref1.begin(), pref1.end(), cnt) - pref1.begin());
};
for(int i = 0; i <= N; i++)
for(int j = 0; j <= N; j++)
for(int k = 0; k <= N; k++)
dp[i][j][k] = -1;
dp[0][0][0] = a;
array<int, 4> best = {0, 0, 0, 0};
for(int a = 0; a <= 70; a++){
for(int b = 0; b <= 70; b++){
for(int c = 0; c <= 70; c++){
if(dp[a][b][c] < 0) continue;
ll curr = dp[a][b][c];
if(a < (int)v4.size() && curr >= v4[a].p){
ll val = 4LL*(curr-v4[a].p);
if(val > INF) val = INF;
dp[a+1][b][c] = max(dp[a+1][b][c], val);
}
if(b < (int)v3.size() && curr >= v3[b].p){
ll val = 3LL*(curr-v3[b].p);
if(val > INF) val = INF;
dp[a][b+1][c] = max(dp[a][b+1][c], val);
}
if(c < (int)v2.size() && curr >= v2[c].p){
ll val = 2LL*(curr-v2[c].p);
if(val > INF) val = INF;
dp[a][b][c+1] = max(dp[a][b][c+1], val);
}
int d = get(curr);
if(dp[a][b][c]>=0 && a+b+c+d > best[0]+best[1]+best[2]+best[3]) best = {a,b,c,d};
}
}
}
vector<int> sol;
for(int i = 0; i < best[3]; i++) sol.push_back(v1[i].idx);
while(best[0]+best[1]+best[2] > 0){
ll rem = dp[best[0]][best[1]][best[2]];
if(best[0] > 0){
ll prev = dp[best[0]-1][best[1]][best[2]];
if(prev >= 0){
ll val = 4LL*(prev-v4[best[0]-1].p);
if(val > INF) val = INF;
if(val == rem){
sol.push_back(v4[best[0]-1].idx);
best[0]--;
continue;
}
}
}
if(best[1] > 0){
ll prev = dp[best[0]][best[1]-1][best[2]];
if(prev >= 0){
ll val = 3LL*(prev-v3[best[1]-1].p);
if(val > INF) val = INF;
if(val == rem){
sol.push_back(v3[best[1]-1].idx);
best[1]--;
continue;
}
}
}
if(best[2] > 0){
ll prev = dp[best[0]][best[1]][best[2]-1];
if(prev >= 0){
ll val = 2LL*(prev-v2[best[2]-1].p);
if(val > INF) val = INF;
if(val == rem){
sol.push_back(v2[best[2]-1].idx);
best[2]--;
continue;
}
}
}
}
reverse(sol.begin(), sol.end());
return sol;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |