A fireworks show is logged as a sequence of n per-second intensity readings recorded in chronological order; a reading can be negative when a misfire briefly dims the display below its baseline. The show's director wants to cut the single continuous recording into exactly three consecutive acts, none of them empty, so that all three acts carry exactly the same total intensity and no act of the show feels more dramatic than the others. Decide whether such a division exists.
Line 1: one integer n, the number of recorded seconds. Line 2: n integers v_1 ... v_n, the intensity reading at each second, in chronological order.
Print "YES" if the recording can be divided into exactly three non-empty, contiguous, consecutive acts (covering the whole recording, in order) with equal total intensity; otherwise print "NO".
Example 1
Input
6 4 0 1 3 2 2
Expected
YES
Explanation
The recording splits as [4] | [0,1,3] | [2,2]; each act sums to 4, so the answer is YES.
Example 2
Input
4 3 3 3 3
Expected
NO
Explanation
The total intensity is 12, so each act would need to sum to 4, but the only prefix sums available for a cut are 3, 6, and 9 — none equal 4 — so no split into three equal-sum acts exists, and the answer is NO even though the grand total is divisible by three.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →