//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 的格式要求是 "插入 [该代码片段的中文名]"

{
    "Huffman": {
        "prefix": "acm-2.2荷马史诗",
        "body": [
            "#pragma GCC optimize(2)",
            "#pragma GCC optimize(3,\"Ofast\",\"inline\")",
            "#include<algorithm>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register LL",
            "using namespace std;",
            "LL ans,fu,S,d,k,n,t,i,x;",
            "struct QAQ{LL x,d;inline bool operator<(QAQ b)const{return x==b.x?d<b.d:x<b.x;}}Q[100011];",
            "inline void up(Re p){",
            "\twhile(p>1)",
            "\t\tif(Q[p]<Q[p>>1])swap(Q[p],Q[p>>1]),p>>=1;",
            "\t\telse break;",
            "}",
            "inline void down(Re p){",
            "\tint s=p<<1;",
            "\twhile(s<=t){",
            "\t\tif(s<t&&Q[s|1]<Q[s])s++;",
            "\t\tif(Q[s]<Q[p])swap(Q[s],Q[p]),p=s,s<<=1;",
            "\t\telse break;",
            "\t}",
            "}",
            "inline void push(Re x,Re d){Q[++t].x=x,Q[t].d=d;up(t);}",
            "inline void delet(Re p){swap(Q[p],Q[t--]);down(p);}",
            "inline void in(Re &x){",
            "    x=fu=0;char c=getchar();",
            "    while(c<'0'||c>'9')fu|=c=='-',c=getchar();",
            "    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();",
            "    if(fu)x=-x;",
            "}",
            "int main(){",
            "\tin(n),in(k);",
            "\twhile(n--)in(x),push(x,0);",
            "\twhile((t-1)%(k-1))push(0,0);",
            "\twhile(t>1){",
            "\t\tx=k,S=0,d=0;",
            "\t\twhile(x--)d=max(d,Q[1].d),S+=Q[1].x,delet(1);",
            "\t\tans+=S,push(S,d+1);",
            "\t}",
            "\tprintf(\"%lld\\n%lld\",ans,Q[1].d);",
            "}",
            ""
        ],
        "description": "插入 Huffman"
    }
}