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

{
    "征途": {
        "prefix": "acm-5.1min",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define Re register LL",
            "const int N=3e3+5;",
            "LL p,i,j,n,m,h,t,Q[N],S[N],dp[N][N];",
            "//S_walk[m]=S[n]",
            "//              m                               m",
            "//             i=1                             i=1",
            "//        m",
            "//       i=1",
            "//dp[p][i]=min(dp[p-1][j]+(S[i]-S[j])^2)",
            "//(2*S[i]) * S[j] + (dp[p][i]-S[i]^2) = (dp[p-1][j]+S[j]^2)",
            "//   k     *  x   +         b         =          y",
            "inline LL min(Re a,Re b){return a<b?a:b;}",
            "inline LL X(Re j){return S[j];}",
            "inline LL Y(Re j){return dp[p-1][j]+S[j]*S[j];}",
            "inline long double slope(Re i,Re j){return (long double)(Y(j)-Y(i))/(X(j)-X(i));}",
            "int main(){",
            "    memset(dp,127,sizeof(dp));dp[0][0]=0;",
            "    scanf(\"%lld%lld\",&n,&m);",
            "    for(i=1;i<=n;S[i]+=S[i-1],++i)scanf(\"%lld\",&S[i]);",
            "    for(p=1;p<=m;++p){",
            "    \th=1,t=0,Q[++t]=0,dp[p][0]=0;",
            "\t    for(i=1;i<=n;++i){",
            "\t        while(h<t&&slope(Q[h],Q[h+1])<2*S[i])++h;",
            "\t        dp[p][i]=min(dp[p-1][i],dp[p-1][j=Q[h]]+(S[i]-S[j])*(S[i]-S[j]));",
            "\t\t\twhile(h<t&&slope(Q[t-1],Q[t])>slope(Q[t-1],i))--t;",
            "\t        Q[++t]=i;",
            "\t    }",
            "\t}",
            "    printf(\"%lld\",m*dp[m][n]-S[n]*S[n]);",
            "}",
            ""
        ],
        "description": "插入 征途"
    }
}