先前缀和,然后枚举每一个元素解决
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1000100;
const ll inf = 99999999999999;
int n;
ll sum[maxn],a[maxn];
bool ok(ll x,int l,int r)
{
while(l<=r)
{
int mid=(l+r)/2;
if(sum[mid]==x)return true;
else if(sum[mid]<x)l=mid+1;
else r=mid-1;
}
return false;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum[i]=sum[i-1]+a[i];
}
if(sum[n]&1)
cout<<"NO"<<endl;
else
{
ll ans=sum[n]/2;
int flag=0;
for(int i=1;i<=n;i++)
{
if(ok(ans-a[i],1,i-1)||ok(ans+a[i],i,n))
{
flag=1; break;
}
}
if(flag)
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}