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
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
If there are several optimal solutions, output any of them. If the Knight can't marry his beloved princess, just print
-1in the first line of the output.
Sample Input
sample input |
sample output |
6 d 10 d 12 p 2 d 1 p 2 |
13 2 3 5 |
·
#include<iostream>
· #include<cstdio>
· #include<cstdlib>
· #include<cmath>
· #include<cstring>
· #include<string>
· #include<vector>
· #include<list>
· #include<map>
· #include<set>
· #include<stack>
· #include<queue>
· #include<algorithm>
· #include<functional
· usingnamespace std;
· #define N200005
· · structnode
· {
· int x,id; //x是黄金数,或者公主期望值. id为第几个牢房.
· char tp[2];
· booloperator<(nodea)const{ //重载运算符,按升序排列
· if(x<a.x)return0;
· elsereturn1;
· }
· }a[N],ans[N];
· priority_queue<node>q; //优先队列
· intcmp(nodex,nodey) //升序排列牢房编号
· {
· return x.id<y.id;
· }
· //priority_queue<int,vector<int>,greater<int>> q;
· intmain()
· {
· int n,i;
· while(scanf("%d",&n)==1)
· {
· //getchar();
· for(i=1;i<n;i++)
· scanf("%s%d",&a[i].tp,&a[i].x),a[i].id=i+1; //因为牢房是从2到n;骑士刚开始在第一个牢房出发,所以加1
· while(!q.empty())q.pop(); //初始化容器,使容器为空.
· for(i=1;i<n-1;i++)
· {
· if(a[i].tp[0]=='d')q.push(a[i]); //如牢房是龙,加入容器
· else{ //q.size()是返回容器的容量,即元素的个数.
· //若是公主,判断容器里龙的数量是否大于公主的期望值,大于,就移除容器元素.
· while(q.size()>=a[i].x)q.pop() //知道龙的数量小于公主的期望,移除的龙是按黄金数小的优先出来.
· } //最后容器的里面的龙斗士要杀的,并且黄金数是最大的.
· } //若容器的容量小于最后一个公主的期望,即不能娶最后一个公主,则 -1 ;
· if(q.size()<a[i].x)printf("-1\n");
· else{
· int l=0,cnt=0;
· while(!q.empty())
· ans[l++]=q.top(),q.pop(); //取容器里的龙,
· sort(ans,ans+l,cmp); //按牢房编号升序排列
· for(i=0;i<l;i++)cnt+=ans[i].x; //总获得的黄金
· printf("%d\n",cnt);printf("%d\n",l);
· for(i=0;i<l-1;i++)printf("%d",ans[i].id);printf("%d\n",ans[i].id); //输出杀龙的编号.
· }
· }
· return0;
· }