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

{
    "树上背包优化O复杂度 (树上染色)": {
        "prefix": "acm-4.5nk",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const int N=2003;",
            "int n,m,x,y,z,o,K,size[N],head[N];LL tmp[N],dp[N][N];",
            "struct QAQ{int w,to,next;}a[N<<1];",
            "inline void add(Re x,Re y,Re z){a[++o].w=z,a[o].to=y,a[o].next=head[x],head[x]=o;}",
            "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 dfs(Re x,Re fa){",
            "\tsize[x]=1,dp[x][0]=dp[x][1]=0;",
            "\tfor(Re i=head[x],to;i;i=a[i].next)",
            "\t\tif((to=a[i].to)!=fa){",
            "\t\t\tdfs(to,x);",
            "\t\t\tfor(Re j=0;j<=size[x]+size[to];++j)tmp[j]=dp[0][0];",
            "\t\t\tfor(Re j=size[x];j>=0;--j)",
            "\t\t\t\tif(dp[x][j]>=0)for(Re k=size[to];k>=0;--k)",
            "\t\t\t\t\tif(dp[to][k]>=0)",
            "\t\t\t\t\t\ttmp[j+k]=max(tmp[j+k],dp[x][j]+dp[to][k]+",
            "\t\t\t\t\t\t\t(LL)a[i].w*((LL)k*(K-k)+(LL)(size[to]-k)*(n-size[to]-(K-k)))",
            "\t\t\t\t\t\t);",
            "\t\t\tsize[x]+=size[to];",
            "\t\t\tfor(Re j=0;j<=size[x];++j)dp[x][j]=tmp[j];",
            "\t\t}",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(K),m=n-1;",
            "\twhile(m--)in(x),in(y),in(z),add(x,y,z),add(y,x,z);",
            "\tmemset(dp,-20,sizeof(dp));",
            "\tdfs(1,0),printf(\"%lld\\n\",dp[1][K]);",
            "}",
            ""
        ],
        "description": "插入 树上背包优化O复杂度 (树上染色)"
    }
}