//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<cstring>",
            "#include<cstdio>",
            "#include<queue>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=1200,M=1865,inf=2e9;",
            "int o=1,n,m,h,t,st,ed,O,Poi[23][43],A[23][43],cyf[N],pan[N],pre[N],dis[N],head[N];LL maxcost,maxflow; ",
            "struct QAQ{int w,to,next,flow;}a[M<<1];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){",
            "    for(Re i=0;i<=ed;++i)dis[i]=-inf,pan[i]=0;",
            "    Q.push(st),pan[st]=1,dis[st]=0,cyf[st]=inf;",
            "    while(!Q.empty()){",
            "    \tRe x=Q.front();Q.pop();pan[x]=0;",
            "    \tfor(Re i=head[x],to;i;i=a[i].next)",
            "    \t\tif(a[i].flow&&dis[to=a[i].to]<dis[x]+a[i].w){",
            "    \t\t\tdis[to]=dis[x]+a[i].w,pre[to]=i;",
            "    \t\t\tcyf[to]=min(cyf[x],a[i].flow);",
            "    \t\t\tif(!pan[to])pan[to]=1,Q.push(to);",
            "    \t\t}",
            "    }",
            "    return dis[ed]!=-inf;",
            "}",
            "inline void EK(Re st,Re ed){",
            "    while(SPFA(st,ed)){",
            "    \tRe x=ed;maxflow+=cyf[ed],maxcost+=(LL)cyf[ed]*dis[ed];",
            "    \twhile(x!=st){",
            "    \t\tRe i=pre[x];",
            "    \t\ta[i].flow-=cyf[ed];",
            "    \t\ta[i^1].flow+=cyf[ed];",
            "    \t\tx=a[i^1].to;",
            "    \t}",
            "    }",
            "}",
            "inline void TAT1(){",
            "    for(Re i=1;i<=m;++i)add_(st,Poi[1][i],1,0);",
            "    for(Re i=1;i<=m+n-1;++i)add_(Poi[n][i]+O,ed,1,0);",
            "    for(Re i=1;i<=n;++i)",
            "    \tfor(Re j=1;j<=m+i-1;++j){",
            "    \t\tadd_(Poi[i][j],Poi[i][j]+O,1,A[i][j]);",
            "    \t\tif(i<n)add_(Poi[i][j]+O,Poi[i+1][j],1,0),add_(Poi[i][j]+O,Poi[i+1][j+1],1,0);",
            "    \t}",
            "    EK(st,ed);",
            "    printf(\"%lld\\n\",maxcost);",
            "}",
            "inline void TAT2(){",
            "    for(Re i=1;i<=m;++i)add_(st,Poi[1][i],1,0);",
            "    for(Re i=1;i<=m+n-1;++i)add_(Poi[n][i]+O,ed,inf,0);",
            "    for(Re i=1;i<=n;++i)",
            "    \tfor(Re j=1;j<=m+i-1;++j){",
            "    \t\tadd_(Poi[i][j],Poi[i][j]+O,inf,A[i][j]);",
            "    \t\tif(i<n)add_(Poi[i][j]+O,Poi[i+1][j],1,0),add_(Poi[i][j]+O,Poi[i+1][j+1],1,0);",
            "    \t}",
            "    EK(st,ed);",
            "    printf(\"%lld\\n\",maxcost);",
            "}",
            "inline void TAT3(){",
            "    for(Re i=1;i<=m;++i)add_(st,Poi[1][i],1,0);",
            "    for(Re i=1;i<=m+n-1;++i)add_(Poi[n][i]+O,ed,inf,0);",
            "    for(Re i=1;i<=n;++i)",
            "    \tfor(Re j=1;j<=m+i-1;++j){",
            "            add_(Poi[i][j],Poi[i][j]+O,inf,A[i][j]);",
            "            if(i<n)add_(Poi[i][j]+O,Poi[i+1][j],inf,0),add_(Poi[i][j]+O,Poi[i+1][j+1],inf,0);",
            "    \t}",
            "    EK(st,ed);",
            "    printf(\"%lld\\n\",maxcost);",
            "}",
            "inline void CL(){",
            "    memset(head,0,sizeof(head));",
            "    memset(cyf,0,sizeof(cyf));",
            "    memset(pre,0,sizeof(pre));",
            "    memset(a,0,sizeof(a));",
            "    maxflow=maxcost=0,o=1;",
            "}",
            "int main(){",
            "    in(m),in(n);",
            "    for(Re i=1;i<=n;++i)",
            "    \tfor(Re j=1;j<=m+i-1;++j)",
            "            in(A[i][j]),Poi[i][j]=++O;",
            "    st=O<<1|1,ed=st+1;",
            "    TAT1(),CL(),TAT2(),CL(),TAT3();",
            "}",
            ""
        ],
        "description": "插入 数字梯形问题  (EK)"
    }
}