#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define endl "\n"
#define dbg(x) cout << #x << " = " << (x) << endl;
//const int INF = 1e18;
const int MAXN = 1e6 + 10;
const int MOD = 1e9 + 7;
vector<int> max_coupons(int a, vector<int> p, vector<int> t) {
    int N = p.size();
    set<pair<int, int>> t1, t2; 
    for (int i = 0; i < N; ++i) {
        if (t[i] == 1) t1.insert({p[i], i});
        else t2.insert({p[i], i});
    }
    vector<int> result;
    while (true) {
        pair<int, int> best = {-1, -1};
        int new_tokens = -1;
        // try best T=2 coupon
        if (!t2.empty()) {
            auto it = t2.begin();
            int cost = it->first, idx = it->second;
            if (a >= cost) {
                int tokens_after = (a - cost) * 2;
                best = {cost, idx};
                new_tokens = tokens_after;
            }
        }
        // Try best T=1 coupon
        if (!t1.empty()) {
            auto it = t1.begin();
            int cost = it->first, idx = it->second;
            if (a >= cost) {
                int tokens_after = a - cost;
                if (new_tokens == -1 || tokens_after > new_tokens) {
                    best = {cost, idx};
                    new_tokens = tokens_after;
                }
            }
        }
        if (best.second == -1) break; 
        int idx = best.second;
        result.push_back(idx);
        if (t[idx] == 1) {
            a = a - p[idx];
            t1.erase({p[idx], idx});
        } else {
            a = (a - p[idx]) * 2;
            t2.erase({p[idx], idx});
        }
    }
    return result;
}
/*int32_t main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	//freopen("input.in", "r", stdin);
	//freopen("input.out", "w", stdout);    
	vector<int> ans = max_coupons(13, {4, 500, 8, 14}, {1, 1, 1, 1});
	for(auto x : ans) cout<<x<<endl;	
    return 0;
}*/
| # | 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... |