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

{
    "负载平衡问题  (EK)": {
        "prefix": "acm-3.5MCMF",
        "body": [
            "#include<algorithm>",
            "#include<cstdio>",
            "#include<queue>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=103,inf=2e9;",
            "int o=1,n,h,t,S,st,ed,A[N],cyf[N],pan[N],pre[N],dis[N],head[N];LL mincost; ",
            "struct QAQ{int w,to,next,flow;}a[N<<3];queue<int>Q;",
            "inline void in(Re &x){",
            "    int f=0;x=0;char c=getchar();",
            "    while(c<'0'||c>'9')f|=c=='-',c=getchar();",
            "    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();",
            "    x=f?-x:x;",
            "}",
            "inline void add(Re x,Re y,Re z,Re w){a[++o].flow=z,a[o].w=w,a[o].to=y,a[o].next=head[x],head[x]=o;}",
            "inline void add_(Re a,Re b,Re flow,Re w){add(a,b,flow,w),add(b,a,0,-w);}",
            "inline int SPFA(Re st,Re ed){",
            "\tfor(Re i=0;i<=n;++i)dis[i]=inf,pan[i]=0;",
            "\tQ.push(st),pan[st]=1,dis[st]=0,cyf[st]=inf;",
            "\twhile(!Q.empty()){",
            "\t\tRe x=Q.front();Q.pop();pan[x]=0;",
            "\t\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\t\tif(a[i].flow&&dis[to=a[i].to]>dis[x]+a[i].w){",
            "\t\t\t\tdis[to]=dis[x]+a[i].w,pre[to]=i;",
            "\t\t\t\tcyf[to]=min(cyf[x],a[i].flow);",
            "\t\t\t\tif(!pan[to])pan[to]=1,Q.push(to);",
            "\t\t\t}",
            "\t}",
            "\treturn dis[ed]!=inf;",
            "}",
            "inline void EK(Re st,Re ed){",
            "\twhile(SPFA(st,ed)){",
            "\t\tRe x=ed;mincost+=(LL)cyf[ed]*dis[ed];",
            "\t\twhile(x!=st){",
            "\t\t\tRe i=pre[x];",
            "\t\t\ta[i].flow-=cyf[ed];",
            "\t\t\ta[i^1].flow+=cyf[ed];",
            "\t\t\tx=a[i^1].to;",
            "\t\t}",
            "\t}",
            "}",
            "int main(){",
            "\tin(n);st=n+1,ed=n+2;",
            "\tfor(Re i=1;i<=n;++i)in(A[i]),S+=A[i];S/=n;",
            "\tfor(Re i=1;i<=n;++i){",
            "\t\tif(i>1)add_(i,i-1,inf,1),add_(i-1,i,inf,1);",
            "\t\tif(A[i]>S)add_(st,i,A[i]-S,0);",
            "\t\telse if(A[i]<S)add_(i,ed,S-A[i],0);",
            "\t}",
            "\tadd_(1,n,inf,1),add_(n,1,inf,1);",
            "\tn+=2;EK(st,ed);",
            "\tprintf(\"%lld\",mincost);",
            "}",
            ""
        ],
        "description": "插入 负载平衡问题  (EK)"
    }
}