//Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
// same ids are connected.
// prefix 的格式要求是 acm-[一级标题编号].[二级标题编号][该代码片段的英文简称]
// description 的格式要求是 "插入 [该代码片段的中文名]"

{
    "二叉堆 (Supermarket)": {
        "prefix": "acm-2.2小根堆",
        "body": [
            "#pragma GCC optimize(2)",
            "#pragma GCC optimize(3,\"Ofast\",\"inline\")",
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#include<queue>",
            "using namespace std;",
            "struct QAQ{int w,d;}a[10003];",
            "int ans,n,i;",
            "inline bool cmp(QAQ a,QAQ b){return a.d!=b.d?a.d<b.d:a.w>b.w;}",
            "struct QWQ{",
            "\tint heap[10003],t;",
            "\tinline void CL(){memset(heap,0,sizeof(heap));t=0;}",
            "\tinline void up(int p){",
            "\t\twhile(p>1)",
            "\t\t\tif(heap[p>>1]>heap[p])swap(heap[p],heap[p>>1]),p>>=1;",
            "\t\t\telse break;",
            "\t}",
            "\tinline void down(int p){",
            "\t\tint s=(p<<1);",
            "\t\twhile(s<=t){",
            "\t\t\tif(s<t&&heap[s]>heap[s|1])s++;",
            "\t\t\tif(heap[s]<heap[p]){",
            "\t\t\t\tswap(heap[s],heap[p]);",
            "\t\t\t\tp=s,s<<=1;",
            "\t\t\t}",
            "\t\t\telse break;",
            "\t\t}",
            "\t}",
            "\tinline void pop(){delet(1);}",
            "\tinline int top(){return heap[1];}",
            "\tinline int size(){return t;}",
            "\tinline void push(int x){heap[++t]=x;up(t);}",
            "\tinline void delet(int p){swap(heap[p],heap[t--]);down(p);}",
            "}Q;",
            "int main(){",
            "    while(scanf(\"%d\",&n)!=EOF){",
            "        for(i=1;i<=n;i++)scanf(\"%d%d\",&a[i].w,&a[i].d);",
            "        sort(a+1,a+n+1,cmp);ans=0;Q.CL();",
            "        for(i=1;i<=n;i++)",
            "            if(Q.size()<a[i].d)Q.push(a[i].w);",
            "            else if(Q.top()<a[i].w)Q.pop(),Q.push(a[i].w);",
            "        while(Q.size())ans+=Q.top(),Q.pop();",
            "        printf(\"%d\\n\",ans);",
            "    }",
            "}",
            ""
        ],
        "description": "插入 二叉堆 (Supermarket)"
    }
}