比赛 2024国庆练习3 评测结果 AAAAAAAAAAAAAAAAAAAAAATTT
题目名称 划分 最终得分 88
用户昵称 ┭┮﹏┭┮ 运行时间 15.612 s
代码语言 C++ 内存使用 8.16 MiB
提交时间 2024-10-06 17:44:49
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll,int>
#define fi first
#define in inline
#define se second
#define mp make_pair
#define pb push_back
const int N = 5e5+10;
const ll inf = 1e8;

ll read(){
	ll x = 0,f = 1;char c = getchar();
	for(;c < '0' || c > '9';c = getchar())if(c == '-')f = -1;
	for(;c >= '0' && c <= '9';c = getchar())x = (x<<1) + (x<<3) + c-'0';
	return x * f;
}
void cmin(ll &x,ll y){x = min(x,y);}


int n,type;
ll a[N],s[N],f[N],g[N];

priority_queue<pii,vector<pii>,greater<pii> >q;
int main(){
	freopen("2019partition.in","r",stdin);
	freopen("2019partition.out","w",stdout);
	n = read(),type = read();
	for(int i = 1;i <= n;i++)a[i] = read(),s[i] = s[i-1] + a[i];
	int x = 0;
	for(int i = 1;i <= n;i++){
		while(!q.empty() && s[i] >= q.top().fi){
			x = max(x,q.top().se);
			q.pop();
		}
		f[i] = s[i] - s[x];
		g[i] = g[x] + f[i] * f[i];
//		printf("-- %d %d %lld %lld\n",i,x,g[i],f[i]);
		q.push({f[i] + s[i],i});
	}
	printf("%lld\n",g[n]);

	return 0;

}