#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 500005
using namespace std;
typedef long long LL;
LL prefix[N], suffix[N], num[N];
LL cntSuf[N];
int main(){
int n;
scanf("%d", &n);
for(int i=1; i<=n; ++i){
scanf("%lld", &num[i]);
prefix[i]=prefix[i-1]+num[i];//前缀和
}
for(int i=n; i>=1; --i)
suffix[i]=suffix[i+1]+num[i];//后缀和
LL s=prefix[n]/3;
if(prefix[n]%3!=0){
printf("0\n");
return 0;
}
LL ans=0;
for(int i=1; i<=n; ++i)
if(suffix[n-i+1]==s) cntSuf[n-i+1]=cntSuf[n-i+2]+1;
else cntSuf[n-i+1]=cntSuf[n-i+2];
for(int i=1; i<=n; ++i)
if(prefix[i]==s) ans+=cntSuf[i+2];
printf("%lld\n", ans);
return 0;
}
时间: 2024-11-22 17:13:13