We need to read a problem: Given array A of n elements (n up to 100). We need to split it into contiguous subarrays (segments) such that each segment's sum is non-zero. If it's impossible, print NO. Otherwise print YES, k and then for each segment its left and right indices (1-indexed) with contiguous covering of whole array.
Observations: The condition is that each segment sum != 0. So we need to partition the array into contiguous segments with each segment's sum ≠ 0.
If the total sum of the whole array is not zero, we can take the whole array as one segment (k=1, [1,n]) because sum !=0. That's always a valid solution.
The only problematic case is when total sum is zero. Then we cannot have a single segment. But maybe we can split into multiple segments such that each segment sum is non-zero. The question: is it always possible to split an array with total sum zero into segments each with non-zero sum? Not always: consider array with all zeros: any subarray sum is zero, so impossible. Or array with sum zero but all elements zero? That's a special case. Also consider array with zeros but also non-zero elements might still allow a split? Let's examine.
We need each segment sum ≠ 0. So if the whole sum is zero, we need at least two segments. The condition is that each segment sum !=0. We need to find a split.
General approach: Since n is small (≤100), we can try to find a valid split by greedy or DP. But we can also reason about necessary conditions: If there exists any non-zero element, we can perhaps split around it.
Think: If total sum is zero and at least one non-zero element exists, can we always partition into exactly two segments such that both sums are non-zero? Let's try.
Suppose total sum = 0. If we take a prefix ending at i (1 ≤ i < n) such that prefix sum != 0, then the suffix sum = total - prefix sum = -prefix sum, which is also != 0 if prefix sum != 0. So any prefix with non-zero sum yields a valid two-segment split, provided that the suffix (i+1..n) also has non-zero sum (which automatically holds because total sum is zero, suffix sum = -prefix sum). However, we need both segments sums non-zero. So we need to find an index i (1 <= i < n) such that sum(1..i) != 0. Does such an index always exist if total sum zero and array not all zeros? Not necessarily: Consider array: [2, -2] => total sum 0. Prefix sum at i=1: sum=2 ≠0 -> good. So split works. Another: [0, 1, -1] => total sum 0. Prefix sums: i=1: 0; i=2: 1 (≠0) -> good. So works. What about array: [0,0,0]? All zeros, no non-zero prefix sum. That's the case we cannot split because any subarray sum is zero. But if array contains some non-zero elements, can it happen that all prefixes sums are zero? Suppose we have array with first element non-zero? Then prefix sum at i=1 is non-zero (since element non-zero). So if a1 ≠ 0, we have a valid split at i=1. So the only tricky case is when a1 = 0, and maybe subsequent prefixes until we hit a non-zero? Let's consider: a1=0, a2=0,... but eventually there is non-zero element. At the first index j where a_j != 0, the prefix sum up to j includes that non-zero element, but also earlier zeros, so sum = a_j (non-zero) if all previous zeros. So prefix sum at j would be non-zero. So indeed, if there exists any non-zero element, then the smallest index j where a_j != 0, the prefix sum up to j is non-zero. So we can split at j. However, careful: what if the non-zero element is such that the prefix sum becomes zero? But if a_j is non-zero, and all previous are zeros, the prefix sum = a_j ≠ 0. So it's non-zero. So we can always split at that index j, unless j=n (i.e., last element is first non-zero). But if j=n and a_n != 0 and total sum = 0, then all previous elements sum to -a_n, which may be non-zero? But total sum zero implies sum(1..n-1) = -a_n. Since a_n != 0, sum(1..n-1) != 0. Then we can split at n-1? But need a prefix sum non-zero. At n-1, sum(1..n-1) = -a_n ≠ 0. So that works as long as n-1 >=1 i.e., n>1. If n=1 and the only element is zero, total sum zero and cannot split because only one segment possible. But if n=1 and element non-zero, total sum non-zero => can use single segment, so okay.
Hence, it seems that if total sum is zero and the array contains at least one non-zero element, we can always split into two segments: [1..i] and [i+1..n] for some i < n where prefix sum !=0. This works because we can choose i = smallest index such that prefix sum != 0 (which exists because some element non-zero). However, we must ensure that i < n, i.e., we are not taking i = n. If the smallest index with non-zero prefix sum is n, that would mean prefix sum at n is total sum = 0? Actually total sum is zero, so prefix sum at n = 0. So the smallest index with non-zero prefix sum cannot be n because total sum is zero. But could the prefix sum at all indices 1..n-1 be zero and only total sum zero? That would imply sum(1..i)=0 for all i < n. If sum(1..i)=0 for all i<n, then by induction, all elements must be zero? Let's check: sum(1..1)=0 => a1=0. sum(1..2)=0 => a1+a2=0 => a2=0. So indeed, if sum(1..i)=0 for all i from 1 to n-1, then all a1,...,a_{n-1}=0. Then total sum zero implies a_n=0 as well. So all zeros. So if there's any non-zero element, there exists some i < n with prefix sum !=0. So we can split.
Thus, the answer is YES if and only if the array is not entirely zeros? Let's verify: If total sum !=0, answer YES with one segment. If total sum = 0, answer YES iff there exists at least one non-zero element (so we can split into two). But wait, is splitting into two always enough? Yes, we produce two segments: first segment up to i, second from i+1 to n. Both sums non-zero as argued. So answer would be YES unless the entire array is all zeros (i.e., every element is zero). However, consider array like [0, 0, 0, 0] - indeed impossible. Also consider array like [0, 0]? total sum zero, all zero => impossible. Also maybe array like [1, -1, 0]? total sum zero, non-zero elements exist. Smallest i with prefix sum !=0? i=1 sum=1 => split at 1 => segments: [1,1] sum=1, [2,3] sum = -1+0 = -1 ≠0 => valid. So yes.
But what about array like [2, -3, 1]? total sum=0, non-zero elements. i=1 sum=2 -> split => segments: [1,1] sum=2, [2,3] sum=-3+1=-2 -> valid.
What about array where a non-zero element appears but the first prefix non-zero might be at i= n? Not possible as argued.
Thus, it seems the condition for YES is: not (all elements are zero). Because if all zeros, impossible; else possible.
But check constraints: n up to 100, but also values could be negative and positive. So the only NO case is when all elements are zero. Wait, is there any case where not all zero but still impossible? Let's attempt to think: what if we have zero total sum and we split into two, both sums become non-zero. That's fine. However, what about the requirement "the sum of elements in each of the new arrays is not zero"? That's it. So we can always produce a two-segment split if total sum zero and array not all zero. But we must also ensure that the second segment is not empty? Since i < n, second segment non-empty. So it's valid.
But what if total sum zero and we produce two segments but the second segment sum is zero because maybe prefix sum not zero but suffix sum might accidentally be zero? Actually if total sum = 0 and prefix sum = P ≠ 0, then suffix sum = -P ≠ 0. So both non-zero. So it's safe.
Thus, algorithm: Compute total sum. If total sum != 0: output YES, k=1, [1 n]. Else: check if all elements zero. If yes, output NO. Else: find first index i (1 ≤ i < n) such that prefix sum != 0 (or simply find the first non-zero element and use that to split? But need prefix sum !=0. If the first non-zero is at position j, then prefix sum up to j is non-zero, so i = j works as long as j < n. If j = n (i.e., only last element non-zero and others zero), then prefix sum at n-1 = sum of first n-1 zeros = 0, so not non-zero. But we need i < n with non-zero prefix sum. In that case, we can use i = n-1? But prefix sum at n-1 = 0, so not. However we could split at some other i? Let's examine: array: [0,0,...,0, x] where x != 0 and n>1. Total sum = x != 0? Actually if others zeros and x !=0, total sum = x, not zero. So total sum non-zero => we would have taken single segment, so no issue. So total sum zero case with non-zero elements cannot have only last element non-zero because total sum would be non-zero. For total sum zero, if there is a non-zero element, there must be at least two non-zero elements or one non-zero and some combination such that sum zero. For [0,0,..,x] with x≠0, total sum = x ≠0, not zero. So zero total sum implies that the sum of all non-zero elements balances to zero. So there will be at least one non-zero element not at the very end? Not necessarily: consider array: [0,0,...,0, 5, -5] with last two non-zero. The first non-zero occurs at n-1 (5). Prefix sum up to n-1 = 5 ≠0, i = n-1 works. So i = n-1 is < n, good. Consider [0,0,...,0, a, b] where a+b=0. If a !=0, then prefix up to n-1 = a !=0, good. So we can always take i = position of the first non-zero element? But if the first non-zero element appears at position p, prefix sum up to p is sum of zeros + a_p = a_p ≠ 0. But we need i < n. Could p = n? That would mean first non-zero at last element, but then total sum = a_n (non-zero) because all previous zeros. So total sum not zero, so not in zero total sum case. So in zero total sum case, first non-zero p satisfies p ≤ n-1? Actually p could be n if there were negative numbers before? Wait: If the first non-zero appears at n, that means a_n ≠ 0 and a_1..a_{n-1}=0. Then total sum = a_n ≠ 0. So indeed, in zero total sum case, first non-zero p must be ≤ n-1 because if p=n, total sum would be non-zero. So okay.
Thus, we can find any i where prefix sum != 0. Simple approach: iterate i from 1 to n-1, compute prefix sum, if prefix sum != 0, then we can split at i. Since not all elements are zero, there must be such i.
Alternatively, we could split into multiple segments: e.g., if total sum zero, we can break at each non-zero element individually? But that's extra. But the simplest is two segments.
Edge case: n=1, total sum zero means the only element is zero. Then we cannot split (only one segment sum zero). So output NO. If n=1 and element non-zero, total sum !=0 => output YES with one segment.
Thus, the problem is trivial: answer NO iff all a_i == 0. Otherwise YES with either one segment if total sum != 0, else two segments with split at some i where prefix sum != 0.
Let's verify with given examples:
Example 1: n=3, A = [1,2,-3]. Total sum = 0? 1+2-3=0. Not all zeros. Need i with prefix sum !=0. i=1 sum=1, so split at 1 => segments: [1,1] sum=1, [2,3] sum=2-3=-1. That matches sample output: YES, 2, 1 2? Wait sample output:
YES
2
1 2
3 3
They split at i=2: [1,2] sum=3 (non-zero) and [3,3] sum=-3 (non-zero). Both fine. So any split works.
Example 2: n=8, A = [9,-12,3,4,-4,-10,7,3]. Compute total sum? 9-12=-3, -3+3=0, 0+4=4, 4-4=0, 0-10=-10, -10+7=-3, -3+3=0 => total sum 0. Not all zero. They gave split: [1,2] sum=-3? 9-12=-3 non-zero, and [3,8] sum = rest? Let's compute: 3+4-4-10+7+3 = 3? 3+4=7,7-4=3,3-10=-7,-7+7=0,0+3=3? Actually: 3+4=7, 7-4=3, 3-10=-7, -7+7=0, 0+3=3 => sum=3 non-zero. So valid. So they split at i=2. Another i could work.
Example 3: n=1, [0] => NO.
Example 4: n=4, [1,2,3,-5] => total sum 1+2+3-5=1, non-zero => one segment. Sample output says YES, 4, with each element separately. That's also valid but not necessary. So any answer is accepted. So our solution with one segment would be valid.
Thus, we can implement a straightforward solution.
But wait: The problem statement includes: "If there are multiple answers, print any of them." So our approach is fine.
Double-check: Is there any scenario where total sum != 0 but splitting into one segment is not allowed because the segment sum must be non-zero? That's fine; it's non-zero because total sum !=0. So single segment works.
But what about the case total sum zero but array not all zeros, but also we need to ensure that each segment sum is not zero. Our two-segment split works.
However, consider the case where total sum zero, but we cannot split into two because we need to ensure both segments sums are not zero. We argued it's always possible. But let's test a borderline: n=2, A=[0,0] all zeros => NO. A=[0,5,-5]? n=3, total sum 0. i=2 sum=5 => split: [1,2] sum=5, [3,3] sum=-5 => works. A=[1,-1] => i=1 sum=1 => split: [1,1] sum=1, [2,2] sum=-1 => works. A=[1,-2,1] => total sum 0. i=1 sum=1 => works.
What about A = [2, -1, -1]? total sum 0. i=1 sum=2 => works. i=2 sum=1 => also works. So yes.
Thus, algorithm:
- Read n, array a[1..n].
- If all a[i] == 0: print "NO".
- Else:
- Compute total sum = sum(a).
- If total sum != 0:
- print "YES", 1, and "1 n"
- Else: // total sum == 0 but not all zero
- Find the smallest i (1 <= i < n) such that prefix sum != 0.
- Actually we can compute prefix sum cumulatively and break.
- Then print "YES", 2, then "1 i", "i+1 n".
But wait: Could there be a scenario where the prefix sum is zero for all i < n, but not all elements are zero? We argued that would imply all zeros. Let's prove: Suppose for all i from 1 to n-1, sum_{1..i} = 0. Then a1 = 0. For i=2, a1+a2=0 => a2=0. By induction, all a1..a_{n-1}=0. Then total sum = a_n = 0 because total sum = 0? Actually total sum = sum_{1..n} = sum_{1..n-1}+a_n = 0 + a_n = total sum (which is given as zero). So a_n = 0. Thus all zeros. So indeed.
So implementation is safe.
But maybe we can even produce a more straightforward solution: if all zeros, NO; else YES, with k = number of non-zero segments? But easier: if total sum != 0: k=1, else: find any i where prefix sum != 0 and output two segments.
We need to be careful about index handling: i must be between 1 and n-1. So we need to guarantee existence. If total sum zero and not all zeros, such i exists as argued.
Now, what about the scenario where total sum zero and there exists an i such that prefix sum != 0, but the second segment sum is zero? As argued, it's -prefix sum, so non-zero.
Thus, the solution is correct.
But is there any nuance about the output format? We need to print "YES" then k then each line with li ri.
Edge Cases:
- n=1, a1 !=0: print YES, 1, 1 1.
- n=1, a1 =0: NO.
Let's test with n=2, a=[0,0] => NO.
n=2, a=[0,1] => total sum=1 !=0 => YES, 1, 1 2.
n=2, a=[1,-1] => total sum 0, not all zeros => find i: prefix sum at i=1 =1 !=0 => output YES, 2, 1 1, 2 2.
Now, we should consider whether we need to split into more than two segments if the problem expects any? But as per statement, any answer is acceptable.
Thus, solution is trivial.
But wait: The problem is from Codeforces? Possibly it's more tricky: The condition "sum of elements in each new arrays is not zero" must hold for each new array. The splitting can be into any number of arrays. So indeed our solution works.
I suspect there is a catch: The array may have zeros, and we might need to avoid zero-sum segments even if we split into two? But as argued, it's safe.
Let's double-check by constructing a potential counterexample: Suppose total sum = 0 and we choose i such that prefix sum != 0, but the second segment sum may accidentally be zero if total sum is not zero? But total sum is zero, so second sum = -prefix sum, non-zero. So safe.
Thus, the only impossible case is all zeros.
But there is also a possible nuance: The output requires that l1 = 1 and rk = n and ri + 1 = li+1 (i.e., segments are consecutive and cover whole array). That's satisfied.
Thus, solution is easy.
But maybe the problem originally had a larger constraint or required printing minimal k? But not.
Given that n is only 100, even if we wanted to produce a split into many segments (like each non-zero element separate) we could, but our solution is fine.
Implementation in Python:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
a = [int(next(it)) for _ in range(n)]
# Check if all zeros
if all(x == 0 for x in a):
print("NO")
return
total = sum(a)
if total != 0:
print("YES")
print(1)
print(1, n)
else:
# Find index i (0-based) where prefix sum != 0
prefix = 0
for i in range(n):
prefix += a[i]
if prefix != 0 and i < n-1: # i+1 is the split point? Actually we need split after index i (1-indexed). If we are at 0-index i, the first segment is [1, i+1] if we split after i. Condition: i+1 < n because need at least one element in second segment.
split = i+1 # 1-indexed position of end of first segment
print("YES")
print(2)
print(1, split)
print(split+1, n)
return
# According to logic, we should always find such split; but if not (shouldn't happen), fallback:
# But maybe we need to handle case where prefix sum !=0 only at the last index? That can't happen because total sum zero => prefix at last =0.
# So unreachable.
# Just in case, we can split at first non-zero element's position: find first non-zero index j (0-based). Then split at j+1 if j+1 < n else? but j+1 should be < n as argued.
for j in range(n):
if a[j] != 0 and j < n-1:
print("YES")
print(2)
print(1, j+1)
print(j+2, n)
return
But we can simplify: In the else (total zero and not all zero), we can simply find the first index where a[i] != 0, let pos = i+1. Since total zero, pos <= n-1? Actually if a[n-1] !=0 and all before zeros, then pos = n? Wait index i 0-based, if a[n-1] !=0, then pos = n (since i+1 = n) but then we would split after the last element? That would make second segment empty. So we need to ensure pos < n. Let's check: In total zero scenario, if the first non-zero is at last element (index n-1, 1-indexed n), then all previous zeros sum to 0, so total = a[n-1] != 0, contradiction. So first non-zero cannot be at position n. So pos < n. So we can split at pos. But careful: The sum of the first segment up to pos includes the non-zero element, so it's non-zero. The second segment from pos+1 to n sum = - (first sum) (since total zero) thus non-zero. So that works.
But is it possible that the first non-zero is at position n-1? That yields pos = n-1 (1-indexed). Then first segment sum = a_{n-1} (non-zero) because previous zeros. Then second segment is a_n alone. But a_n must be such that total sum zero: a_{n-1} + a_n = 0 => a_n = -a_{n-1} (non-zero). So second segment sum is non-zero. So valid. So splitting at first non-zero always works, as long as that index is not n. So we can do:
if total == 0:
# find first i where a[i] != 0, then split at i (1-indexed) i.e., first segment [1, i]
for i in range(n):
if a[i] != 0:
split = i+1 # 1-indexed
break
print("YES")
print(2)
print(1, split)
print(split+1, n)
But wait, what if the first non-zero index i is such that split = n? As argued, impossible. But we can add assert split < n.
However, consider an array where first non-zero is at i=0 (first element) but total sum zero? Example: [2, -2]. first non-zero is at i=0, split=1, then first segment [1,1] sum=2, second [2,2] sum=-2, good. So works.
Thus, we can skip computing prefix sum and just find first non-zero.
But is there any case where first non-zero is at position n? Could total sum be zero? Suppose array: [0,0,...,0, x] with x != 0, total sum = x != 0, not zero. So indeed not zero. So safe.
Thus, the simplest: if all zeros -> NO else YES. Then if sum != 0 -> k=1, else -> k=2, split at first non-zero.
But caution: What if total sum zero but first non-zero index yields a split where first segment sum is zero? That cannot happen because if first non-zero is at index i (1-indexed), then all elements before are zero, so segment sum = a_i != 0. So okay.
Thus, final solution:
- if all elements are 0: print "NO"
- else:
- if sum != 0: print "YES\n1\n1 n"
- else: find first index i (1-indexed) such that a_i != 0. Output: YES\n2\n1 i\n{i+1} n
But note: we need to ensure i < n. Since sum == 0 and not all zero, i must be < n? As argued, if i = n, then sum would be a_n (non-zero) because all previous zeros, so sum != 0. So i cannot be n. So okay.
Check with n=2, a = [0,0]? Already handled by all zero -> NO.
Now, what about array like [0, 1, -1]? first non-zero at i=2 (1-indexed). split=2 -> first segment [1,2] sum = 0+1=1? Wait, segment includes a1=0 and a2=1 => sum=1 non-zero; second segment [3,3] sum=-1 non-zero. Good.
Thus, correct.
Thus, solution is O(n).
But wait, the problem says "Help Lesha!" The examples output sometimes use more than 2 segments, but that's fine.
But I'm thinking: Is there any scenario where total sum != 0 but the whole array sum is non-zero, but maybe we still need to split? No, condition is just each new array sum not zero, so whole array qualifies.
Thus, answer.
Now, we should produce code.
But let's not forget: The input constraints allow negative numbers.
Thus, implement in a function.
Potential pitfall: Input may contain multiple test cases? The problem statement doesn't indicate multiple test cases. So just one.
Now, to be safe, we can read from sys.stdin.
Write code:
import sys
def main():
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
a = list(map(int, data[1:1+n]))
if all(x == 0 for x in a):
print("NO")
return
total = sum(a)
if total != 0:
print("YES")
print(1)
print(1, n)
else:
# find first non-zero index (1-indexed)
for i in range(n):
if a[i] != 0:
split = i + 1
break
print("YES")
print(2)
print(1, split)
print(split + 1, n)
if name == "main":
main()
But is it possible that split+1 > n? That would be if split = n, but as argued, can't happen because total sum would be non-zero. However, to be absolutely safe, we can add an if split == n: then we cannot split into two segments; but then total sum would be a[n] which is non-zero, contradiction. But maybe due to floating? No. So safe.
Let's test with some custom cases:
Case: n=3, [0,0,0] -> NO.
Case: n=3, [0,0,5] -> total sum=5 !=0 -> YES 1 segment. Good.
Case: n=3, [5,0,-5] -> total sum=0, first non-zero at i=1 -> split=1 -> segments: [1,1] sum=5, [2,3] sum=0-5=-5 -> valid. Good.
Case: n=2, [5,-5] -> total sum 0, split=1 -> segments: [1,1] sum=5, [2,2] sum=-5 -> valid.
Now, but consider the output format: For each segment, we print "li ri". Should be space-separated.
Also ensure we print newline after each line.
Now, double-check if there is any scenario where we need to output more than 2 segments because maybe the problem expects minimal number of segments? It says "If there are multiple answers, print any of them." So it's fine.
Thus, solution is trivial.
But wait, reading some Codeforces problem "Lesha and array splitting" maybe there's a nuance: The condition "the sum of elements in each of the new arrays is not zero" must hold, but also we need to ensure that we split into "several, possibly one, new arrays". That's it.
Thus, answer.
Given the simplicity, I'm confident.
Thus, answer: Provide explanation and code.
Now, produce final message. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
a = list(map(int, data[1:1 + n]))
# if all elements are zero -> impossible
if all(x == 0 for x in a):
print("NO")
return
total = sum(a)
if total != 0:
# one segment is enough
print("YES")
print(1)
print(1, n)
else:
# find the first non‑zero element
for i in range(n):
if a[i] != 0:
split = i + 1 # 1‑based index where first segment ends
break
print("YES")
print(2)
print(1, split)
print(split + 1, n)
if name == "main":
solve()