p548.ans1
======================
13
2
3 5

=================
p548.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n;
vector<pair<char, int>> a;

void read() {
    cin >> n;
    a.resize(n - 1);
    for(int i = 0; i < n - 1; i++) {
        cin >> a[i].first >> a[i].second;
    }
}

void solve() {
    // Walk left to right, greedily keeping every dragon killed so far in a
    // min-heap keyed by the gold it holds (sum tracks the total gold of the
    // currently kept dragons). When we reach a princess with beauty x, the
    // Knight may not have killed x or more dragons or she would marry him and
    // end the journey early; so while the heap holds at least x dragons we
    // discard the cheapest ones until fewer than x remain.
    //
    // The final cell is the beloved princess: the chosen set is valid iff
    // after processing all earlier cells the heap holds fewer dragons than her
    // beauty. The remaining dragons in the heap are exactly the ones to kill,
    // and their stored 1-based cell indices form the answer.

    int sum = 0;
    priority_queue<
        pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
        q;

    for(int i = 0; i < n - 2; i++) {
        auto [c, x] = a[i];
        if(c == 'd') {
            sum += x;
            q.push({x, i});
        } else {
            while((int)q.size() >= x) {
                sum -= q.top().first;
                q.pop();
            }
        }
    }

    if(a.back().first != 'p' || (int)q.size() < a.back().second) {
        cout << -1 << '\n';
        return;
    }

    cout << sum << '\n';
    vector<int> kills;
    while(!q.empty()) {
        kills.push_back(q.top().second + 2);
        q.pop();
    }

    cout << kills.size() << '\n';
    sort(kills.begin(), kills.end());
    cout << kills << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
p548.in1
======================
6
d 10
d 12
p 2
d 1
p 2

=================
statement.txt
======================
548. Dragons and Princesses
Time limit per test: 1 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard



Once upon a time there lived the Knight. Being very courageous he decided to make a long journey full of fights and adventures. The map of this journey can be represented as a row with n cells numbered from 1 to n from left to right. Initially the Knight is located at the leftmost cell (cell number 1). He should pass all the cells one by one and finish his way at the rightmost cell (cell number n). He is not allowed to move back or skip some cells, he will visit all the cells from the first to the last.

Each cell except the first one contains either a dragon or a princess. Each dragon has a chest with gold coins. The dragon at the cell i keeps gi coins. Every time the Knight steps to a cell with a dragon he has a choice  — to kill the dragon or just to pass through. The Knight is very strong and dexterous, so it is not a problem for him to kill any dragon on his way. If a dragon is killed the Knight gets all the gold dragon possessed.

When the Knight steps to the cell with a princess, she wonders how many dragons he has killed. If that number is greater or equal to her beauty bi, the princess considers the Knight brave enough and instantly asks him to marry her. Being a true gentleman, the Knight cannot refuse and his adventure immediately ends.

The Knight loves the princess who lives in the cell number n and wants to marry her. Also during the journey he wants to collect as much gold as possible. Please help him to accomplish this task.

Input
The first line of the input contains a single integer n (2 ≤ n ≤ 2·105) — the number of cells. The next n-1 lines describe cells from 2 to n.

If the cell number i contains a dragon, the i-th line of the input contains letter "d" followed by a single integer gi (1 ≤ gi ≤ 104) — the number of coins the dragon keeps. The letter and the integer are separated by a single space.

If the cell number i contains a princess, the i-th line of the input contains letter "p" followed by a single integer bi (1 ≤ bi ≤ 2·105) — the beauty of the princess. The letter and the integer are separated by a single space. It is guaranteed that the last cell contains a princess.

Output
On the first line of the output print a single integer — the maximum number of gold coins the Knight can collect. On the second line print a single integer k — the number of dragons to kill. The third line should contain k integers — the numbers of the cells where the Knight should kill a dragon. The cell numbers should be printed in the increasing order.

If there are several optimal solutions, output any of them. If the Knight can't marry his beloved princess, just print -1 in the first line of the output.

Example(s)
sample input
sample output
6
d 10
d 12
p 2
d 1
p 2
13
2
3 5

sample input
sample output
6
d 10
d 12
p 2
d 1
p 3
-1

=================
