#include<bits/stdc++.h>
#define pb push_back
using namespace std;
vector<int> max_coupons(int A, vector<int> P, vector<int> T) {
    int n = P.size();
    vector<pair<int, int>> ones;
    vector<tuple<int, int, int>> k;
    for (int i = 0; i < n; ++i) {
        if (T[i] == 1) {
            ones.emplace_back(P[i], i);
        } else {
            k.emplace_back(P[i], T[i], i);
        }
    }
    sort(k.begin(), k.end(), [](const auto &a, const auto &b) {
        auto [p1, t1, i1] = a;
        auto [p2, t2, i2] = b;
        return p1 * (t1 - 1) * t2 > p2 * (t2 - 1) * t1;
    });
    int tokens = A;
    vector<int> result;
    vector<tuple<int, int, int>> used, rem;
    for (auto &[cost, mult, idx] : k) {
        if (tokens >= cost) {
            tokens = (tokens - cost) * mult;
            result.pb(idx);
            used.emplace_back(cost, mult, idx);
        } else {
            rem.emplace_back(cost, mult, idx);
        }
    }
    vector<pair<int, vector<int>>> states = {{tokens, {}}};
    for (auto &[cost, mult, idx] : rem) {
        vector<pair<int, vector<int>>> ns = states;
        for (auto &[tok, path] : states) {
            if (tok >= cost) {
                int new_tok = (tok - cost) * mult;
                vector<int> new_path = path;
                new_path.pb(idx);
                ns.emplace_back(new_tok, new_path);
            }
        }
        sort(ns.begin(), ns.end(), [](auto &a, auto &b) {
            if (a.second.size() != b.second.size()) return a.second.size() > b.second.size();
            return a.first > b.first;
        });
        if (ns.size() > 70) ns.resize(70);
        states = move(ns);
    }
    vector<int> bs;
    for (auto &[_, path] : states) {
        if (path.size() > bs.size())
            bs = path;
    }
    for (int idx : bs) result.pb(idx);
    sort(ones.begin(), ones.end());
    for (auto &[cost, idx] : ones) {
        if (tokens >= cost) {
            tokens -= cost;
            result.pb(idx);
        }
    }
    return result;
}
| # | 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... |