从前往后扫一遍,再从后往前扫一遍。
每次查找能不能通过移动当前数完成,然后把差值记录
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <iomanip>
using namespace std;
#define _ ios::sync_with_stdio(false)
const int MAXN = 100010;
const int INF=0x7fffffff;
long long a[MAXN];
long long sum=0;
long long s1[MAXN];
set<long long > s;
int main(){
int n;
scanf("%d",&n);
s1[0]=0;
for(int i=1;i<=n;i++){
scanf("%lld",a+i);
sum+=a[i];
s1[i]=s1[i-1]+a[i];
}
int ok=0;
s.clear();
for(int i=1;i<=n;i++){
if(s.count(2*a[i])){
ok=1;
break;
}
long long x=sum-2*s1[i];
if(x==0){
ok=1;
break;
}
if(x>=0)
s.insert(x);
}
s.clear();
for(int i=n;i>=1;i--){
if(s.count(2*a[i])){
ok=1;
break;
}
long long x=2*s1[i]-sum;
if(x==0){
ok=1;
break;
}
if(x>=0)
s.insert(x);
}
if(ok)
puts("YES");
else
puts("NO");
}