| 比赛 | 2024国庆练习3 | 评测结果 | AAAAAAAAAAAAAAAAAAAAAATTT | 
    | 题目名称 | 划分 | 最终得分 | 88 | 
    | 用户昵称 | 小金 | 运行时间 | 15.505 s | 
    | 代码语言 | C++ | 内存使用 | 6.18 MiB | 
    | 提交时间 | 2024-10-06 17:54:39 | 
显示代码纯文本
#include<iostream>
#include<cstdio>
int n,type,q[500010],g[500010];
long long s[500010],b[500010],ans;
long long calc(int x)
{
    return 2*s[x]-s[g[x]]; 
}
int main()
{
    freopen("2019partition.in","r",stdin);
    freopen("2019partition.out","w",stdout);
	scanf("%d%d",&n,&type);
	for(int i=1;i<=n;i++)
	{
	    long long a;
	    scanf("%lld",&a);
	    s[i]=s[i-1]+a;
    }	
	int h=1,t=1;
	for(int i=1;i<=n;i++)
	{
		while(h<t&&calc(q[h+1])<=s[i]) h++;
		g[i]=q[h];
		while(h<t&&calc(q[t])>=calc(i)) t--;
		t++;
		q[t]=i;
	}
	int now=n;
	while(now)
    {
        ans=(ans+((s[now]-s[g[now]]))*((s[now]-s[g[now]])));
        now=g[now];
    } 
	printf("%lld",ans);
}