All problems

Problem 306

Split Array into Three Equal Sum Parts

Easy · 10 pts · Array, Prefix Sum

Problem

Decide whether the array can be cut into three non-empty contiguous parts that all have the same sum. In other words, you must find two cut positions i < j so that A[0..i], A[i+1..j] and A[j+1..N-1] each contain at least one element and have equal sums. Negative numbers and zeros may appear, so a part can have a sum of 0.

Input

The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].

Output

Print YES if such a split exists, otherwise NO.

Constraints

3 <= N <= 100000; -10000 <= A[i] <= 10000

Example input

11
0 2 1 -6 6 -7 9 1 2 0 1

Example output

YES

Explanation

The array 0 2 1 -6 6 -7 9 1 2 0 1 can be split as [0 2 1], [-6 6 -7 9 1] and [2 0 1], and every part sums to 3.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Smallest Sum That Cannot Be Formed Next: Count Pairs with Given Sum