Recently Vasya found a golden ticket — a sequence which consists of n digits a_1a_2... a_n. Vasya considers a ticket to be lucky if it can be divided into two or more non-intersecting segments with equal sums. For example, ticket 350178 is lucky since it can be divided into three segments 350, 17 and 8: 3+5+0=1+7=8. Note that each digit of sequence should belong to exactly one segment.
Help Vasya! Tell him if the golden ticket he found is lucky or not.
Input
The first line contains one integer n (2 ≤ n ≤ 100) — the number of digits in the ticket.
The second line contains n digits a_1 a_2 ... a_n (0 ≤ a_i ≤ 9) — the golden ticket. Digits are printed without spaces.
Output
If the golden ticket is lucky then print "YES", otherwise print "NO" (both case insensitive).
Examples
Input
5
73452
Output
YES
Input
4
1248
Output
NO
Note
In the first example the ticket can be divided into 7, 34 and 52: 7=3+4=5+2.
In the second example it is impossible to divide ticket into segments with equal sum.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to parse problem: Given a string of digits (length n up to 100). We want to partition the entire sequence into at least two contiguous non-overlapping segments that cover the entire string (each digit belongs to exactly one segment). The sums of the digits in each segment should all be equal.
We need to output YES if such a partition exists, else NO.
Brute force: Since n ≤ 100, we could try all possible target sums. Because each digit is 0-9, total sum S = sum of digits. For partition into k segments (k≥2) each segment sum = S/k, so S must be divisible by k and we need to be able to split the sequence into contiguous segments each summing to S/k.
We can also approach: Try every possible first segment ending position? Actually we need to check if there exists a partition into at least 2 equal-sum segments. So we can simply try all possible target sums t from 0 up to maybe 900 (since max sum 9*100=900). For each t, attempt to partition from left to right: accumulate sum; whenever accumulated sum equals t, start new segment (reset sum to 0). If at the end accumulated sum is 0 (i.e., exactly finished a segment) and we made at least 2 segments, then success. But note: t can be 0? But segments sums could be 0 if all digits are zero? Let's examine. Ticket digits can be 0. If all digits are zero, then any partition works because sum of each segment is 0. So we should return YES if n≥2 (since at least two segments). But careful: target t=0. In our algorithm, if t=0, then the accumulation: we start with sum=0; as we add digits, sum will remain 0 only if all digits are zero. Actually if any digit >0, sum will become >0 and never return to 0. So only if all digits are zero would we get partition success with t=0. But also, note that segments sum equal and could be 0 if all digits are 0. So that works.
But is it always correct to try each t? Actually we need to partition into at least two segments, but the number of segments is not predetermined. The target sum must be such that we can partition into contiguous segments each summing to that t, and the sum of all digits S must be multiple of t (i.e., t divides S) and t > 0? Actually if S=0, then t=0 works. So we can simply iterate t from 0 to S-1? But better: Since n is small, we can just try all possible positions for the first segment sum. Because the first segment determines the target sum. For each possible end index i (1..n-1), compute sum of first segment s = sum_{1..i}. Then try to partition rest: iterate through remaining digits, accumulating; whenever accum equals s, reset; if at end accum is 0 (exactly finished) and we formed at least one more segment, then YES. That is O(n^2) (since for each i we scan remainder). n ≤ 100, so O(n^2) is fine. But careful: If s=0, we need to handle that: Since first segment sum s=0 would only happen if the digits up to i sum to 0. That means all those digits are zero. For the rest, we need to check if we can partition into segments of sum 0, meaning all digits must be zero. So if the entire string is all zeros, we can just return YES. But if first segment sum is 0 and some later digit is non-zero, then can't partition because target sum 0, but later sum becomes positive and never return to 0. Actually could we partition into segments of sum 0 if there are positive digits? No, because segment sum must be 0, but any positive digit would make that segment sum >0. So impossible. So algorithm works.
But there is also possibility that the partition has more than two segments, but the target sum may not equal the sum of the first segment because maybe first segment is not exactly one of the segments? But in any valid partition, we can consider the first segment from start to some boundary. That boundary is where the first segment ends. So the sum of first segment is the target sum. So checking all possible first segment lengths will cover all possibilities.
Thus solution: read n, read string s of digits (no spaces, just a string). Convert digits to array of ints. Compute total sum. If all digits are zero (total sum == 0), then we can partition into at least two segments (since n>=2) so print YES. Actually need at least two segments. But if n=2 and both zeros, can partition into two segments each zero, so YES. So that's fine.
Otherwise, for i from 0 to n-2 (0-indexed first segment ending before i? Let's define: we consider the first segment covering indices 0..i inclusive? Actually contiguous from start. So we choose end index i from 0 to n-2 (so that there is at least one more segment). Compute target = sum of digits[0..i]. Then we try to partition the rest: start from j = i+1, accum = 0; while j < n: accum += digits[j]; if accum == target then accum = 0; else if accum > target then break; (since digits are non-negative, if accum exceeds target, it's impossible because further addition only increases). However careful: digits can be zero, so accum could equal target early? But also, if target==0 and we have zeros, accum remains 0 after each zero. That would cause infinite segments? Actually if target=0, then we would reset accum to 0 whenever it becomes 0, which is always if digits are zero. But if any non-zero digit appears, accum becomes >0 and never returns to 0, so fail. But we already handled all zeros case separately. For target>0, we can proceed.
Edge cases: It is possible that after resetting accum to 0, next digit might be 0, which doesn't increase accum, so we might have multiple resets with zero-length segments? But segments must be non-empty? The problem states "divided into two or more non-intersecting segments". Usually a segment should be contiguous and non-empty. So we can't have zero-length segments. In our algorithm, we only reset when accum exactly equals target. But if we have accum=0 and target>0, that would not reset because accum < target. So we only form a segment when the accumulated sum exactly hits target. However, if we have a sequence of zeros after a reset, accum remains 0, but that does not form a segment until we accumulate enough to reach target. But zeros themselves could be part of a segment that includes later non-zero digits. So it's fine.
But what about the case where target>0 and after processing all digits, accum becomes 0 exactly (meaning we ended exactly at a segment boundary) and we have at least one reset (i.e., at least two segments: the first plus at least one more). In our loop, we need to count segments: initially we have the first segment. Then for each time we reset (when accum == target), that indicates we completed another segment. At the end, if we have completed at least one more segment and accum == 0, success.
But there is nuance: Suppose after finishing all digits, accum == 0 but we never had any reset? That would mean the entire rest sum is 0 and target>0, but then the rest sum would be 0 which is less than target, unless the rest is empty? Actually if we have only the first segment and no other segments, then we have not partitioned into at least two segments. So we need to ensure we have at least one reset. In code, we can set a flag segmentCount that starts at 1 (first segment). When we successfully finish a subsequent segment (i.e., accum == target and we reset), we increment segmentCount. At the end, if segmentCount >=2 and accum == 0, success.
Alternatively, we could just check after scanning rest: we have made at least one segment (i.e., we have encountered at least one time where accum became target) and at end accum == 0.
But careful: Could there be a situation where we never reset but the rest sum equals target? Actually if the rest sum exactly equals target, then as we scan we will accumulate until the end where accum becomes target, then we would detect that at j == n? In our loop we would check after adding each digit if accum == target, then reset. So at the final digit we might get accum == target, and we would reset. So that counts as a segment. So yes, we will reset at the end.
Thus algorithm works.
One more nuance: What if the first segment sum is zero? As we said, all zeros case is handled. But what if the first segment sum is zero but there are non-zero digits later? Then target=0. Our algorithm for target=0 would: start accum=0; then for each digit, add to accum; if accum==0 (which would be true only if no positive digit added yet) we reset? But that would incorrectly split at every zero? Actually let's think: If target=0, then any segment must sum to 0. That means each segment can only contain zeros. If there is any non-zero digit, it's impossible because that digit would have to be in a segment, and that segment's sum would be >0. So we can simply treat target=0 as special: if target==0, then we need to check that all remaining digits are zero. So we can do: if target==0, then check that sum of digits from i+1 to end is 0. But also, we need at least one more segment, i.e., there must be at least one digit after i. Since i goes up to n-2, there is at least one digit after, and if all are zero, then we have at least two segments (the first zeros segment and the rest zeros). So that works. However, note: Could there be valid partitions where target=0 and segments are non-zero length? For example, string "00" n=2, first segment sum zero, second segment sum zero. Our all-zero check already covers that. So we can either treat target=0 specially or rely on all-zero case. But if the first segment sum is zero but not all digits are zero, it's impossible. So we can just skip those i where the first segment sum is zero but total sum > 0, because they won't work.
Simplify: If total sum == 0, return YES. Else, total sum > 0. Then for each i from 0 to n-2, compute target = prefix sum up to i. If target == 0, continue (since total sum > 0, target cannot be zero? Actually target could be zero if first few digits are zeros. But target zero with later non-zero digits won't work, as argued. So we can skip.) Else, attempt to partition: start j = i+1, accum=0, segmentCount=1; for j from i+1 to n-1: accum += digit[j]; if accum == target: segmentCount++; accum=0; else if accum > target: break (fail). After loop, if j == n and accum == 0 and segmentCount >= 2, then success.
But careful: There might be cases where target is not zero but we might have a segment that ends with zeros? For example, digits: [1,0,1]? Sum of first segment? If first segment is "10" sum=1, but second segment "1" sum=1. Actually target=1. In scanning rest: we have digit 1 (i+1) which gives accum=1, so reset. Works. Good.
What about zeros inside segments: like digits: [2,0,2]? If first segment is "20" sum=2, then rest is "2" sum=2 works. In scanning: add 2 => accum=2 reset. Works.
What about scenario: target=5, remaining digits: 2,3,0,0,5? Actually let's simulate: after adding 2, accum=2; add 3 => accum=5 reset; then zeros: add 0 => accum=0, not equal to target (5) and less; then add 0 => same; add 5 => accum=5 reset. Works. Our algorithm: after resetting accum to 0, we then add zeros, accum remains 0, which is < target, so continue. Then add 5, accum=5, reset. That's okay. So zeros are fine.
Now complexity O(n^2) = 10^4 operations, trivial.
Thus solution.
Let's test examples:
Example1: n=5, digits 7,3,4,5,2. total sum=21? Actually 7+3+4+5+2=21. total>0.
Edge: n=2 "01": total=1 >0. Check i=0: target=0, skip? Actually target=0 because first digit 0, but we skip target==0? Then no other i (i=0 only, i max n-2 =0). So no success. But is there a valid partition? Could we divide "01" into two segments "0" and "1"? Sums 0 and 1 not equal. So answer should be NO. Good.
Edge: n=3 "101": total=2. i=0: target=1, then rest: start j=1 digit0->accum=0; j=2 digit1->accum=1 -> reset segmentCount=2; end accum=0 => success YES. Partition: "1", "01"? Actually segments: first "1", second "01" sums 1 each. Works. Good.
Edge: n=3 "110": total=2. i=0: target=1, rest: j=1 digit1->accum=1 reset segmentCount=2; j=2 digit0->accum=0 (after reset, accum=0 then add digit0 => accum=0) and end; accum=0, segmentCount>=2 => success. Partition: "1","10"? sum 1 and 1? Actually "10" sum=1, yes. So YES.
Edge: n=3 "200": total=2. i=0: target=2, rest: j=1 digit0->accum=0; j=2 digit0->accum=0; end accum=0? But we never reset because accum never reached 2. segmentCount=1, accum=0 => but we need at least 2 segments. So fail. i=1: target=2+0=2, rest: j=2 digit0->accum=0; end accum=0, segmentCount still 1? Actually we start segmentCount=1 for first segment (indices 0-1). Then process rest: j=2, accum=0, not equal to 2. At end, accum=0, segmentCount=1, fail. So answer NO. But could we partition? "20","0": sums 2 and 0 not equal; "2","00": sums 2 and 0 not equal. So NO.
Edge: n=4 "0011": total=2. total>0. i=0: target=0 skip. i=1: target=0+0=0 skip. i=2: target=0+0+1=1, rest: j=3 digit1 -> accum=1 reset segmentCount=2; end accum=0 => success. Partition: first three digits "001" sum=1, last "1" sum=1. Yes.
One nuance: The problem statement says digits are printed without spaces. So input line is a string of n characters. So we can read string s, then convert each char to int by ord(c)-ord('0').
Implementation steps:
Read integer n.
Read string s (maybe with newline). Might contain spaces? Problem says digits are printed without spaces, so it's a continuous string. Could also be that the second line might have spaces? No, it says digits are printed without spaces. So just read string.
If len(s) != n? Possibly extra whitespace, but we can just take the first n characters.
Compute array a = [int(ch) for ch in s.strip()] (since there could be newline).
Compute total_sum = sum(a).
If total_sum == 0:
Since n >= 2 (given), print("YES")
return.
For i in range(0, n-1): (since we need at least one more digit after i, so i <= n-2). Actually range(n-1) gives 0..n-2 inclusive.
target = sum(a[0:i+1]) # inclusive.
if target == 0: continue # because total_sum>0, so rest cannot be partitioned into sum 0.
accum = 0
segments = 1 # first segment already counted
ok = True
for j in range(i+1, n):
accum += a[j]
if accum == target:
segments += 1
accum = 0
elif accum > target:
ok = False
break
if ok and accum == 0 and segments >= 2:
print("YES")
return
After loop, print("NO").
That's it.
But wait, there is a subtlety: What if the target sum is large, and we have zeros after reset, but the accum becomes 0 (after reset) and then we add zeros, it's okay. Our condition accum > target breaks only if accum exceeds target. Since digits are non-negative, once accum > target, it's impossible to reduce, so we can break.
But could there be a scenario where accum equals target, we reset, and later we accumulate again but exceed? Already covered.
Thus correct.
Now, about the all-zero case: total_sum == 0, we output YES. But is it always possible to divide into at least two segments? Since n >= 2, we can split at any position, e.g., first segment of length 1 and second of length n-1, both sum to 0. So yes.
Edge: n=1? But constraints say n ≥ 2, so fine.
Thus solution done.
Let's write final answer in Python.
But also note that there might be possibility that total_sum > 0 but target sum could be zero for first segment (if first digits are zeros). We skip those because they cannot work unless all digits are zero (already handled). Good.
Double-check for case where first segment sum equals total_sum? That would be i = n-1? But we only go up to n-2 because need at least two segments. So no.
Now let's test a tricky case: n=5, digits = 2,0,2,0,2. total=6. Valid partitions? Maybe target=2: first segment "2" -> rest "0,2,0,2". Scan: accum 0, then 2 => 2 reset, then 0 =>0, then 2 =>2 reset. Segments: first, then "02"? Actually after reset we had accum reset after second digit? Let's simulate: i=0 target=2, j=1: add 0 -> accum=0; j=2: add 2 -> accum=2 reset (segments=2); j=3: add 0 -> accum=0; j=4: add 2 -> accum=2 reset (segments=3); end accum=0 => success. Partition: "2", "02", "02"? Actually segments: indices: [0], [1,2], [3,4] sums 2 each. Works.
Another tricky: digits: 1,2,3,0,6? n=5, sum=12. Possibly target=3? i=1 sum=3? Actually i=1 sum=1+2=3. Then rest: 3,0,6 => accum 3 -> reset (segments=2), then 0,6 -> accum 6 -> reset? But 6 > 3? Actually target=3, after resetting at j=2 (digit3) accum becomes 0, then add 0 -> accum=0, then add 6 -> accum=6 >3, so fail. So not success. Try i=2 sum=1+2+3=6, rest: 0,6 => accum 0, then 6 -> accum=6 reset, segments=2 success. Partition: "123","06"? sum 6 each. Yes.
Works.
Now consider scenario where we need to skip zeros in the middle but also might have segments that sum to target but with zeros at boundaries. Our algorithm handles that.
Thus solution is fine.
But there is another approach: since n small, we could try all possible number of segments k from 2 to n, then check if S/k is integer and then check if we can partition. That would be O(n^2) as well. But our method is simpler.
Potential pitfalls: Input might contain leading zeros in string, but that's fine.
We should ensure we read the second line correctly, perhaps using input().strip().
Now, what if the second line includes spaces? The problem statement says digits are printed without spaces, but some Codeforces problems might have spaces between digits? Actually they explicitly say "The second line contains n digits a_1 a_2 ... a_n (0 ≤ a_i ≤ 9) — the golden ticket. Digits are printed without spaces." So it's a string without spaces. So we can trust that.
Thus answer.
But just to be thorough, I'll also handle case where maybe they accidentally include spaces? Could use replace(" ", "") to remove spaces. But not necessary.
Now produce final solution.
I'll include comments for clarity.
Time to output final answer.
But the problem says "print "YES", otherwise print "NO" (both case insensitive)". So we output exactly "YES" or "NO" (uppercase typically). So use print("YES") etc.
Now think about a scenario where total_sum > 0 but first segment sum zero and later all zeros except one segment? Actually if first segment sum zero, then target=0. The rest must be partitioned into segments summing to 0. That would require all digits zero. So if rest are not all zero, impossible. So skipping target=0 is fine.
But what about scenario where first segment sum zero but later we can have segments that sum to zero because they consist of zeros, but there is also a non-zero segment? No, because target is zero, every segment must sum zero, so non-zero digits can't be in any segment. So impossible. So skip.
Thus algorithm is correct.
Now, one more nuance: Our check for accum > target breaks early, but consider digits can be zero, so accum will never exceed target if we have some zeros after accumulating close to target? Actually once accum > target, it's impossible to bring it down because digits are non-negative. So break.
Thus final solution.
Let's code.
Potential improvements: We can compute prefix sums to get target quickly. But we can compute on the fly.
Implementation details:
code
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
s = data[1] if len(data)>1 else ''
# In case digits are given as separate tokens? But problem says no spaces, so s is a string.
# However if they were given with spaces, data[1:] would be many tokens, but we need to join?
# We'll robustly handle both: if len(data)==2 and len(data[1])==n then that's the string.
# else, we might need to join data[1:] but that would be unusual.
# Let's parse safely: after reading first integer, the rest of the line (or next lines) contain digits possibly with spaces? But problem says without spaces, so simple.
# We'll assume the digits are given as a continuous string.
a = [int(ch) for ch in s[:n]] # ensure only first n digits considered
total = sum(a)
if total == 0:
print("YES")
return
for i in range(n-1):
target = sum(a[:i+1]) # sum of first segment
if target == 0:
continue
accum = 0
segments = 1
ok = True
for j in range(i+1, n):
accum += a[j]
if accum == target:
segments += 1
accum = 0
elif accum > target:
ok = False
break
if ok and accum == 0 and segments >= 2:
print("YES")
return
print("NO")
That should be fine.
But note that sum(a[:i+1]) each time is O(n) and we have O(n) outer loop, making O(n^2) total, which is fine. But we could compute prefix sums array to get target in O(1). But not needed.
Edge case: n=2, digits "10". total=1. i=0 target=1, then j=1 digit0->accum=0, loop ends, accum=0, segments=1 (since we didn't hit target). Condition segments>=2 false => fail. So NO. But is there partition? "1" and "0" sums 1 and 0 not equal => NO.
Now consider "1010"? n=4, total=2. i=0 target=1, rest:0,1,0 => accum:0, then 1 => reset segments=2; then 0 -> accum=0; end accum=0, segments>=2 => YES. Partition: "1","010"? Actually "010" sum=1, yes. Works.
Now consider "210201". n=6 digits:2,1,0,2,0,1 total=6. i=0 target=2 -> rest:1,0,2,0,1 => accum 1<2; then 0=>1; then 2=>3>2 break fail. i=1 target=3 -> rest:0,2,0,1 => accum0->0; then2=>2; then0=>2; then1=>3 reset segments=2; end accum=0 => success? Wait: after resetting at last digit? Let's simulate: j=2 index2? Actually indices: a[0]=2, a[1]=1, a[2]=0, a[3]=2, a[4]=0, a[5]=1. i=1 target= sum a[0..1]=3. Start j=2: a[2]=0 => accum=0; j=3: a[3]=2 => accum=2; j=4: a[4]=0 => accum=2; j=5: a[5]=1 => accum=3 => reset segments=2; end. At end accum=0. segments=2 => success. Partition: first two digits "21" sum=3, rest "0201"? Actually indices 2-5: 0,2,0,1 sum=3. Yes. So YES.
All good.
Thus answer.
One more nuance: If target > 0, but after finishing the scanning, accum may be 0 but segments count may be exactly 1 (i.e., we never reset) - that would mean the rest sum is 0? Actually if rest sum is 0 and target>0, then accum never reaches target, so not zero? Wait, if rest sum is 0, then all digits after i+1 are zeros. Then while scanning, accum stays 0 throughout. At the end accum=0, but we never reset because accum never equals target (target>0). So segments=1, accum=0, fails. This is correct because we need at least two segments and the rest (zeros) would have to be segmented into segments each summing to target>0, impossible.
Thus fine.
Now produce final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
# The ticket digits are given without spaces, as a single string.
s = data[1] if len(data) > 1 else ''
a = [int(ch) for ch in s[:n]] # consider only the first n characters
code
total = sum(a)
# If all digits are zero, any split into at least two parts works.
if total == 0:
print("YES")
return
# Try every possible first segment (must leave at least one digit for the rest).
for i in range(n - 1):
target = sum(a[:i + 1]) # sum of the first segment
if target == 0: # total > 0, so zeros at start cannot lead to a valid partition
continue
accum = 0
segments = 1 # we already have the first segment
ok = True
for j in range(i + 1, n):
accum += a[j]
if accum == target:
segments += 1
accum = 0
elif accum > target: # cannot recover because digits are non‑negative
ok = False
break
if ok and accum == 0 and segments >= 2:
print("YES")
return
print("NO")