Educational codeforces Round D (two points)

Source: Internet
Author: User

D. Array Division

Vasya have an array a consisting of positive integer numbers. Vasya wants to divide this array into, non-empty consecutive parts (the prefix and the suffix) so, the sum of all E Lements in the first part equals to the sum of elements in the second part. It isn't always possible, so Vasya would move some element before dividing the array (Vasya would erase some element and in SERT 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 a 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
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.

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

Approximate test instructions: give you n number assumption number is 1~n, you can choose one of the number to move to another position or not select. Then select a breakpoint k divides this n number into the front and back two parts, 1~k,k+1~n, asks whether can make the sum of the number of the front and back two parts equal, if may output yes, otherwise no.

Idea: If the direct brute-force enumeration of all situations must be timed out. We can enumerate the position of the moving point I, and then the position of the breakpoint in the case of two points, so that the complexity of time is reduced to NLOGN, the specific look at the code.

The code is as follows

#include <iostream> #include <algorithm> #include <string> #include <cstdio> #include < cstring> #include <queue> #include <vector> #include <cstring> #include <sstream> #include

<cmath> #include <map> #define LL Long long #define ULL unsigned long long using namespace std;
int s[100005];
        LL pre[100005];//prefix and, note open lnoglong bool Find (int l,int r,ll x) {while (l<=r) {int mid= (L+R)/2;
        if (pre[mid]==x) return 1;
        if (pre[mid]<x) l=mid+1;
    else r=mid-1;
} return 0;
    } int main () {memset (pre,0,sizeof (pre));
    int n;
    scanf ("%d", &n);
    LL sum=0;
        for (int i=1;i<=n;i++) {scanf ("%d", &s[i]);
        Sum+=s[i];
    Pre[i]=pre[i-1]+s[i];
       } if (sum%2)//Pruning {printf ("no\n");
    return 0;
    } sum/=2;
for (int i=1;i<=n;i++) {if (Find (I+1,n,sum+s[i]))//enum I, assuming that you need to move it to the breakpoint K, you need to find pre[k]=sum+s[i]        {printf ("yes\n");
        return 0;
        }} for (int i=1;i<=n;i++) {if (Find (1,i-1,sum-s[i]))//enum I, assuming you need to move it to the breakpoint K before you need to find pre[k]=sum-s[i]
            {printf ("yes\n");
        return 0;
    }} printf ("no\n"); 
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.