//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.1X",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LD long double",
            "#define LL long long",
            "#define Re register int",
            "using namespace std;",
            "const LL N=2e5+3,inf=1e18;",
            "int n,x,h=1,t,Q[N];LL ans=inf,Ans,D[N],S[N];",
            "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;",
            "}",
            "// S严格递增",
            "// D严格递减",
            "//F[j]=max(D[i]*S[i]-D[i]*S[j]+D[j]*S[j])",
            "//D[i]*S[j] + F[i]-D[i]*S[i] = D[j]*S[j]",
            "// k  * x   +       b        =     y",
            "inline LL X(Re j){return S[j];}",
            "inline LL Y(Re j){return D[j]*S[j];}",
            "inline LD slope(Re i,Re j){return (LD)(Y(j)-Y(i))/(X(j)-X(i));}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n);",
            "\tfor(Re i=1;i<=n;++i)in(x),S[i]=S[i-1]+x,in(x),D[i+1]=D[i]+x;",
            "\tfor(Re i=1;i<=n+1;++i)D[i]=D[n+1]-D[i],Ans+=D[i]*(S[i]-S[i-1]);",
            "\tfor(Re i=1;i<=n;++i){",
            "\t\tLL tmp=-inf;",
            "\t\twhile(h<t&&slope(Q[h],Q[h+1])>=D[i])++h;",
            "\t\tif(h<=t)ans=min(ans,Ans-(D[i]*(S[i]-S[Q[h]])+D[Q[h]]*S[Q[h]]));",
            "\t\twhile(h<t&&slope(Q[t-1],i)>=slope(Q[t-1],Q[t]))--t;",
            "\t\tQ[++t]=i;",
            "//\t\tfor(Re j=1;j<i;++j)tmp=max(tmp,D[i]*(S[i]-S[j])+D[j]*S[j]);",
            "\t}",
            "\tprintf(\"%lld\\n\",ans);",
            "}",
            ""
        ],
        "description": "插入 锯木厂选址"
    }
}