Educational Codeforces Round 21 Problem D(Codeforces 808D)

Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position).

Inserting an element in the same position he was erased from is also considered moving.

Can Vasya divide the array after choosing the right element to move and its new position?

Input

The first line contains single integer n (1 ≤ n ≤ 100000) — the size of the array.

The second line contains n integers a1, a2... an (1 ≤ ai ≤ 109) — the elements of the array.

Output

Print YES if Vasya can divide the array after moving one element. Otherwise print NO.

Examples
input
3
1 3 2
output
YES
input
5
1 2 3 4 5
output
NO
input
5
2 2 3 4 5
output
YES
Note

In the first example Vasya can move the second element to the end of the array.

In the second example no move can make the division possible.

In the third example Vasya can move the fourth element by one position to the left.


  我很好奇,我和同学内部玩Virtual Contest的时候有个人读不懂前三题,偏偏这道题我读错了,他读懂。最后看到题目描述最后一句话中的element,单数!才发现只能移动一个,内心极其崩溃绝望,但还是在10分钟赶出了程序。

  题目大意是说,给定一个数列,将一个数移动到另一个位置或者什么都不做,是否使得这个数列能被划分成2个部分。

  记两个sum,一个是左边求和的sum,还有一个是另一边求和的sum。最开始左边的sum为0,右边的和为整个数列的和,然后依次将下一个数添加进左边,在右边删去这个数并修改sum,再判断两个sum是否相等,如果相等就可以简单地puts("YES"),然后exit(0)了,如果不相等,就判断差是否为奇数,如果不是就看看和大的那一边存不存在值为差的一半的数(存在就把它拿到小的那一边,就可以使和相等了),如果存在就输出YES退出程序。循环结束了,直接输NO就好了。

  至于判断这个数存不存在,交给可重集就好了。

Code

 /**
* Codeforces
* Problem#808D
* Accepted
* Time:15ms
* Memory:0k
*/
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
typedef bool boolean;
#define inf 0xfffffff
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} #define LL long long int n;
int* a;
LL sum = ;
multiset<LL> s;
multiset<LL> s1, s2; inline void init() {
readInteger(n);
a = new int[(const int)(n + )];
for(int i = ; i <= n; i++) {
readInteger(a[i]);
sum += a[i];
s.insert(sum);
s2.insert(a[i]);
}
} LL sum1 = , sum2 = ; inline void solve() {
if(sum & ) {
puts("NO");
return;
}
sum2 = sum;
sum /= ;
if(s.count(sum)) {
puts("YES");
return;
}
for(int i = ; i <= n; i++) {
if(sum1 > sum2) {
LL temp = sum1 - sum2;
if((temp & ) == && s1.count(temp / )) {
puts("YES");
return;
}
} else {
LL temp = sum2 - sum1;
if((temp & ) == && s2.count(temp / )) {
puts("YES");
return;
}
}
sum1 += a[i], sum2 -= a[i];
s1.insert(a[i]), s2.erase(s2.find(a[i]));
}
puts("NO");
} int main() {
init();
solve();
return ;
}
上一篇:Hadoop的学习--安装配置与使用


下一篇:opencv笔记5:频域和空域的一点理解