It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, find their product. If it is turns to be too large, then the servers might have not enough time to assign tanks into teams and the whole game will collapse!
There are exactly n distinct countries in the world and the i-th country added ai tanks to the game. As the developers of the game are perfectionists, the number of tanks from each country is beautiful. A beautiful number, according to the developers, is such number that its decimal representation consists only of digits '1' and '0', moreover it contains at most one digit '1'. However, due to complaints from players, some number of tanks of one country was removed from the game, hence the number of tanks of this country may not remain beautiful.
Your task is to write the program that solves exactly the same problem in order to verify Gena's code correctness. Just in case.
Input
The first line of the input contains the number of countries n (1 ≤ n ≤ 100 000). The second line contains n non-negative integers ai without leading zeroes — the number of tanks of the i-th country.
It is guaranteed that the second line contains at least n - 1 beautiful numbers and the total length of all these number's representations doesn't exceed 100 000.
Output
Print a single number without leading zeroes — the product of the number of tanks presented by each country.
Examples
Input
3
5 10 1
Output
50
Input
4
1 1 10 11
Output
110
Input
5
0 3 1 100 1
Output
0
Note
In sample 1 numbers 10 and 1 are beautiful, number 5 is not not.
In sample 2 number 11 is not beautiful (contains two '1's), all others are beautiful.
In sample 3 number 3 is not beautiful, all others are beautiful.
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 solve: Given n up to 100,000, each a_i is a non-negative integer without leading zeros (except 0 itself maybe? "non-negative integers" but "without leading zeroes" so 0 is represented as "0"? Possibly yes. They guarantee that at least n-1 of them are beautiful numbers. Beautiful number: decimal representation consists only of digits '0' and '1', and it contains at most one digit '1'. That is, numbers like: 0? Wait: "consists only of digits '1' and '0', moreover it contains at most one digit '1'." So 0 would consist only of 0's? Actually 0 representation is "0", which consists of digit '0' only, and it contains zero '1's, which is at most one. So is 0 considered beautiful? The phrase "beautiful number, according to the developers, is such number that its decimal representation consists only of digits '1' and '0', moreover it contains at most one digit '1'." If it's 0, it consists only of '0', so yes, 0 qualifies. But careful: They said "without leading zeroes". For 0, they likely write "0". So 0 is a beautiful number (since it has 0 ones). However, note that they say "at least n-1 beautiful numbers". In sample 3: Input: 5 numbers: 0, 3, 1, 100, 1. They claim number 3 is not beautiful, all others are beautiful. So indeed they count 0 as beautiful. So 0 is beautiful.
We need to compute the product of all a_i. The numbers can be huge because the total length of all representations doesn't exceed 100,000. That means the numbers can be huge, up to 10^100000 essentially. However the product might be even larger; but we need to output it. But due to the special structure: there are many beautiful numbers which are either 0, 1, or 10^k? Actually beautiful number: digits only 0 and 1, at most one '1'. So possible forms: 0, 1, 10, 100, 1000, ..., also numbers like 0? But no leading zeros so it must be exactly "0" or "1" followed by some zeros? But what about "1000"? That's one '1' and rest zeros. Also "1" itself (one '1', no zeros). Also "0"? That's zero '1's and it's only "0". So basically each beautiful number is either 0, 1, or 10^k for k >= 0? 10^0 = 1, 10^1 = 10, 10^2 = 100, etc. So all beautiful numbers are powers of ten, plus zero. Zero is special.
Now we have exactly one number that might be not beautiful (or possibly more? Guarantee: at least n-1 are beautiful. That means there could be 0, 1, or more non-beautiful? It says "some number of tanks of one country was removed from the game, hence the number of tanks of this country may not remain beautiful." That suggests exactly one country's tanks number may not be beautiful? Actually: "some number of tanks of one country was removed from the game, hence the number of tanks of this country may not remain beautiful." So exactly one country might be non-beautiful? Could be more? The guarantee says: "It is guaranteed that the second line contains at least n - 1 beautiful numbers". So it's possible that more than one is non-beautiful if n is big? Actually if at least n-1 are beautiful, then at most one is non-beautiful. Because if you have 2 non-beautiful, then you'd have at most n-2 beautiful. So indeed exactly one could be non-beautiful or zero non-beautiful. So there is at most one number that is not a power of ten or zero.
Thus product of all numbers reduces to product of many powers of ten and possibly one non-beautiful number and possibly zero.
If any number is zero, product is zero. So trivial output "0".
Otherwise, we need to compute product = (product of all beautiful numbers) * (the non-beautiful number if exists). But beautiful numbers are either 1 or 10^k. Their product is 10^(sum of k_i) where k_i = number of trailing zeros (i.e., length-1 if the number is 1 followed by zeros). But careful: beautiful numbers include 1 (10^0) and 0 (but if zero present, product zero). Also includes maybe "0"? But if 0 appears and we treat as zero, product zero. So for non-zero case, all beautiful numbers are powers of ten (including 1). So their product is 10^S where S = sum over each beautiful number of (number of zeros) i.e., if the representation is "1" followed by m zeros (m >=0), then contribution is m. So total exponent S.
Now the product = (the non-beautiful number, if any, else 1) * 10^S.
If there is no non-beautiful number, then product is just 10^S. Output: "1" followed by S zeros? But careful: 10^S is 1 followed by S zeros. So that's straightforward.
If there is a non-beautiful number, say x (a positive integer, since zero case handled separately). Then product = x * 10^S. So we need to output the decimal representation of x multiplied by 10^S. That is simply x followed by S zeros? Actually yes, because multiplying by 10^S appends S zeros. However, caution: x could be something like "5", then product is "5" + "0"*S. That's fine. But if x itself ends with zeros? Then we might have trailing zeros from x as well, and then added zeros. But that's just concatenation: x's decimal digits, then S zeros. That's correct because 10^S multiplies x, shifting digits left by S. So final number is x * 10^S.
But wait: what if there is more than one non-beautiful? Guarantee says at least n-1 are beautiful, so at most one is non-beautiful. So we can treat exactly one non-beautiful.
Thus algorithm: read n, read list of strings? Since numbers can be huge (up to length 100k total), reading as strings is appropriate.
We need to identify which are beautiful. For each string s, check if it consists only of '0' and '1', and the count of '1' is <= 1. Also note: for 0, it's "0" -> count of '1' = 0, all chars are '0', so beautiful. For 1, "1" -> count 1, all chars '0' or '1'? yes, beautiful.
We also need to detect if any string is "0". If so, output "0". However careful: If there is a zero, product is zero regardless of other numbers. So we can immediately output 0.
If no zero, then we need to compute S = sum of trailing zeros counts for all beautiful numbers. And identify the non-beautiful number if any. But also, there could be the case that all are beautiful. Then product = 10^S. But is there any scenario where all are beautiful but some of them are not powers of ten? Actually all beautiful numbers are either 1 or 10^k, but also "0"? But we already handled zero. So all beautiful non-zero numbers are powers of ten. So yes.
But wait: There is also a possibility of beautiful numbers like "0"? That is zero, we handled. So for non-zero beautiful numbers, they are exactly "1" followed by some zeros. So for each such number s, its trailing zeros count = len(s)-1.
Thus S = sum (len(s)-1) over beautiful numbers that are not "1"? Actually also "1" has len-1=0, so fine.
Now, the non-beautiful number (if exists) is x. The product is x followed by S zeros.
But what about the possibility that the non-beautiful number is "1"? But "1" is beautiful, so not non-beautiful. So non-beautiful is >1 and not a power of ten.
Now, we also need to consider the case where there might be multiple non-beautiful? As argued, at most one. So fine.
However, there is subtlety: what if the non-beautiful number is something like "10"? That's beautiful actually (one '1', zeros). So not non-beautiful. So non-beautiful must have count of '1' >=2 or contain other digits.
Thus algorithm: parse input as strings.
Pseudo:
Read n.
Read list of strings arr (maybe split by spaces, but each number can be huge; we need to read the whole line and split? but total length <= 100k so safe to read line and split)
Initialize:
zero_found = False
S = 0
non_beautiful = None (string)
For each s in arr:
if s == "0":
zero_found = True
break? We can break because product zero, but must still read all? Actually we can read all anyway but we can break early after detection? But careful: the input line includes all numbers, we can read them all at once, then process. Since we already have them, we can loop and if find zero, output 0 and exit.
else:
Check if s is beautiful: count of '1' <=1 and all chars in '01'
if beautiful:
# len(s) -1 is number of zeros trailing
S += len(s) - 1
else:
# There should be at most one non_beautiful
if non_beautiful is not None:
# but guarantee says at most one, but just in case we could handle? But not needed.
# However if we see a second, perhaps it's a mistake but could treat as multiply? But guarantee says at least n-1 beautiful, so at most one non-beautiful. So we can trust.
non_beautiful = s
After loop
If zero_found: print 0; return.
If non_beautiful is None: # all beautiful
print "1" + "0"*S
Else:
print non_beautiful + "0"*S
But wait: Could there be a case where non_beautiful is not None and also there are beautiful numbers that are zero? We already handled zero separately. So fine.
Check sample 1: 5,10,1. 5 is not beautiful, 10 beautiful (S +=1), 1 beautiful (S+=0). non_beautiful="5". S=1. Output "5"+"0" = "50". Correct.
Sample2: 1,1,10,11. 1 beautiful (0), 1 beautiful (0), 10 beautiful (1), 11 non-beautiful? Check: 11 has two '1's -> not beautiful. So S =0+0+1=1. non_beautiful="11". Output "11"+"0" = "110". Correct.
Edge cases: All beautiful, no non-beautiful, and no zeros. Example: n=3, numbers 1,10,100. S=0+1+2=3, output "1"+"000" = "1000". Indeed product =110100=1000. Good.
But what if we have a beautiful number that is "0"? Then we output 0.
What about the case where non_beautiful number is something like "2"? Then S from beautiful numbers, output "2" followed by S zeros.
What about the case where non_beautiful number is "10...0"? That is actually beautiful, so not possible.
Thus solution seems trivial. However, must confirm: Are there any beautiful numbers that are not of form 10^k? For instance, "0"? That's zero. "1"? 10^0. "10"? 10^1. "100"? 10^2. So yes.
But wait: The definition: "consists only of digits '1' and '0', moreover it contains at most one digit '1'." So could there be a number like "0...0"? That is just zero? But for non-zero numbers, they cannot have leading zeros. So they must start with '1' because if they started with '0' they'd have leading zeros (not allowed) unless the number is zero itself. So non-zero beautiful numbers are exactly '1' followed by some (possibly zero) zeros. So yes.
Thus solution straightforward.
But there is nuance: The total length of all numbers' representations doesn't exceed 100,000. That includes non-beautiful numbers too. So we can read them as strings.
Our solution runs O(total length) and prints result. The result length may be huge: S could be up to sum of lengths of beautiful numbers minus count of beautiful numbers. Since total length of all numbers is <=100k, S could be up to about 100k? But also we add non_beautiful length. So output length can be up to S + len(non_beautiful) <= total length? Actually S is sum over beautiful of (len-1). Non-beautiful length <= total length as well. So total output length could be up to roughly total length of input numbers? Let's estimate: For each beautiful number, we effectively "drop the leading '1' and keep the zeros". The product becomes non_beautiful concatenated with all zeros from beautiful numbers. So output length = len(non_beautiful) + S. But S = sum_{beautiful} (len-1). Since there are many beautiful numbers, S could be large, possibly near 100k. Actually total length of all numbers = L_total <= 100k. Let B be set of beautiful numbers, N be non-beautiful (maybe none). Then L_total = sum_{b in B} len(b) + len(N). S = sum_{b in B} (len(b)-1) = sum len(b) - |B|. So output length = len(N) + sum len(b) - |B| = L_total - |B|. Since |B| is at least n-1, n can be up to 100k, so |B| can be up to 100k. So output length could be L_total - |B| <= 100k - (n-1) which could be as low as 0 if n ~ L_total? But L_total includes lengths; each beautiful number at least length 1 (for "1" or "0"? But 0 length? Actually 0 is length 1). So |B| can be up to 100k if each beautiful number is "1"? Then L_total = 100k, output length = L_total - |B| = 0? But product would be 1? Actually if all numbers are "1", then S=0, non_beautiful none, output "1". That's length 1. So formula output length = max(1, L_total - |B|?) Wait careful: if non_beautiful none, output "1" + zeros: "1" + "0"*S. S = sum len(b) - |B|. So output length = 1 + S. But S = sum len(b) - |B|. So output length = 1 + sum len(b) - |B| = (sum len(b) - |B|) + 1 = L_total - |B| + 1 (since no non_beautiful, len(N)=0). That could be 1 if sum len(b)=|B|, meaning each beautiful number is "1". Then output "1". So output length = 1. So formula works: length = (len(N) if any else 1) + S.
Now L_total <= 100k, and |B| can be large, but output length could still be up to L_total if |B| is small? Suppose there is one beautiful number of length L_total-1 and one non-beautiful length 1? Actually n>=1. Consider worst-case output length: we want as many zeros as possible. That would be when there are many beautiful numbers with many zeros, and non_beautiful length possibly small. For instance, all n-1 beautiful numbers are huge powers of ten, like each of length ~100k/(n-1). But there is also the non_beautiful. But sum lengths is limited to 100k. So S = sum(len(b)-1) = sum len(b) - (n-1). Since total length = len(N) + sum len(b) ≤ 100k. So output length = len(N) + sum len(b) - (n-1) = total length - (n-1). If n is 1, then output length = total length - 0 = total length (since non_beautiful? Actually n=1, there is exactly one number. It could be beautiful or not. If it's beautiful and not zero, then output is 10^S where S = len-1? Wait: n=1, if it's beautiful non-zero: s length L, then product = 10^(L-1), output "1" + (L-1) zeros, length = L. So output length = L = total length. So output length can be up to 100k. If it's non-beautiful, output = s, length = L, also up to 100k. So maximum output length is around 100k. That's manageable to output directly as a string.
Thus our solution is O(L_total) time and memory.
Now we just need to be careful with input parsing: Since numbers can be large, we read the second line as a string and split. But there may be leading/trailing spaces? Usually fine. Also there are n numbers, but the second line may contain many spaces? We'll just use split().
Edge Cases:
n=1, input "0": output "0". Good.
n=1, input "1": beautiful, S=0, output "1".
n=1, input "10": output "10"? Actually product = 10. According to algorithm: S = len-1 =1, non_beautiful none => output "1"+"0" = "10". Correct.
n=2, both beautiful, e.g., "10" and "10": S =1+1=2, output "100". Product =10*10=100. Good.
n=2, one beautiful "0", other anything: zero found, output "0". Good.
n=2, one non-beautiful "2", other beautiful "10": S=1, output "20". Product=20. Good.
But careful: what if the non-beautiful number itself is "0"? But "0" is beautiful, as per definition. Actually check: "0" consists of only '0', contains 0 ones => beautiful. So if we get a zero, it's beautiful, but we treat zero separately anyway. If we get a zero, product zero. So it's fine.
Now let's think about the guarantee: "the total length of all these number's representations doesn't exceed 100 000." So reading entire line as string of total length up to 100k plus spaces is fine.
Implementation details:
We'll read integer n via input(). Then read the next line via input().strip().split(). But careful: If the input line is too long? 100k is fine. However, sometimes the numbers might be split across multiple lines? The problem statement says "The second line contains n non-negative integers ...". So only one line. But to be safe, we could read until we have n numbers: maybe read the whole input after first line, concatenate, but simpler: after reading n, we can read the rest as a string with sys.stdin.read()? But I'll just use input() for the second line. In Python, input() reads one line, which can be up to 100k, that's fine. But if there are spaces, it's fine.
But note: n can be up to 100k, so the line will have many numbers separated by spaces, total length <= 100k, so reading with .split() yields list of strings.
Potential pitfalls: The number "0" might appear, and it's beautiful. But we treat it as zero.
Check if beautiful: For each s, we can check:
If s[0] == '0' and len(s) > 1: Not allowed because leading zeros, but input guarantee says without leading zeros, so s either "0" or starts with non-zero. So for beautiful we can simply: count = s.count('1'). If count <=1 and all(c in '01' for c in s): then beautiful. That's O(len) per string, total O(total length). So okay.
Alternative: To check all chars are '0' or '1', we can also do set(s) <= {'0','1'}.
But there might be a nuance: The beautiful number with at most one '1' could be "0" or "1" or "10...0". So the check works.
Now product handling: If zero found, print("0") and exit.
If not zero:
Initialize special_non_beautiful = None
S = 0
For each s:
if is_beautiful(s):
# add trailing zeros count
S += len(s) - 1
else:
# should be only one
if special_non_beautiful is not None:
# Actually according to problem, there will be at most one. So we could just set it, or handle error: but in case, we might treat both as non-beautiful and multiply? But they guarantee at most one, so we can ignore.
# However, if there are two, we could treat both as non-beautiful and multiply them, but that would break the product? Actually then the product would be (x1*x2)*10^S. But we would need to multiply x1 and x2, which might be large. But guarantee ensures it doesn't happen. So we can safely assert.
pass
special_non_beautiful = s
After loop:
if special_non_beautiful is None:
# all beautiful, product = 10^S
print("1" + "0"*S)
else:
print(special_non_beautiful + "0"*S)
But is there any corner case where special_non_beautiful is something like "1"? No, that is beautiful.
Also, what about the possibility that the beautiful numbers include "0"? Already zero_found handled earlier. So after zero_found false, we know no zeros. So beautiful numbers are non-zero powers of ten.
But careful: What if there is a beautiful number that is "0"? We already treat zero_found and exit. So okay.
Thus solution done.
But wait: The problem statement says: "There are exactly n distinct countries... the number of tanks from each country is beautiful. However, due to complaints from players, some number of tanks of one country was removed from the game, hence the number of tanks of this country may not remain beautiful." This implies that originally all numbers were beautiful, but then one country had some tanks removed, making its number possibly not beautiful. But the removal could also make it zero? Possibly. If they removed all tanks, it becomes 0. And 0 is beautiful as we determined. So the non-beautiful number could be any positive integer that is not a power of ten. But also note: Could the removal cause it to become something like "100...0"? That's still beautiful. So non-beautiful occurs when the original beautiful number had a '1' and zeros, and after removing some tanks, the number may become something like 99...? Not exactly. But anyway.
Thus algorithm stands.
But I think there is a catch: The product might be huge, but we output as string, which is fine.
One more nuance: The total length of all numbers is at most 100k, but the product length can be bigger than 100k? For example, suppose we have many beautiful numbers each of length 1? That is "1"s, product=1. That's fine. Suppose we have one beautiful number "10" (length2) and another "10" (length2) ... but total length limited. Could S be as large as 100k? For n=100k, if each beautiful number is "1", total length = 100k, S = sum(len-1)=0, product=1. That's small. To maximize S, we want many zeros, so we want beautiful numbers to be long. But total length limited, so if we have one beautiful number of length 100k-1 and one non-beautiful length 1 (like "2"), then total length = 100k, S = (100k-2)? Actually if beautiful number is "1" followed by many zeros, length L. Then S = L-1. And output length = len(non_beautiful) + S = 1 + (L-1) = L. So output length = L <= 100k. So maximum output length is at most 100k. So it's safe.
Thus we can simply output.
Double-check: If there is no non-beautiful and all beautiful, output length = 1 + S = 1 + (total length - n) because each beautiful contributes length L_i, and sum L_i = total length (since no non_beautiful). Then output length = total length - n + 1. Since total length <= 100k, and n >=1, output length <= 100k. So fine.
Implementation in Python:
def is_beautiful(s):
# count of '1' <=1 and all chars '0' or '1'
count_one = 0
for ch in s:
if ch == '1':
count_one += 1
if count_one > 1:
return False
elif ch != '0':
return False
return True
But careful: "0" passes: count_one=0, all chars '0', returns True. Good.
Alternatively, using set and count: return s.count('1') <= 1 and all(c in '01' for c in s). This is fine.
Now main:
import sys
def main():
data = sys.stdin.read().split()
if not data:
return
n = int(data[0])
numbers = data[1:1+n] # ensure we take exactly n, but data may have more? Actually exactly n numbers provided.
# However sometimes the input may have more lines, but we used read().split() which grabs all tokens.
# So numbers = data[1:] should have length at least n. But to be safe, slice to n.
# Because total tokens = n+1.
# But careful: if there are extra spaces, still tokens count = n+1.
# So numbers = data[1:] works.
# But ensure we have exactly n: numbers = data[1:1+n]
code
zero_found = False
S = 0
non_beautiful = None
for s in numbers:
if s == "0":
zero_found = True
break
if is_beautiful(s):
S += len(s) - 1
else:
# This should be the only non-beautiful
non_beautiful = s
if zero_found:
print("0")
return
if non_beautiful is None:
print("1" + "0"*S)
else:
print(non_beautiful + "0"*S)
if name == "main":
main()
That should work.
But I realize something: Could there be a case where the non-beautiful number is something like "000"? No, because input numbers have no leading zeroes, except "0". So non-beautiful numbers will not start with zero. So printing as is fine.
But wait: If non_beautiful is something like "100" but that's beautiful (since one '1' and zeros). So non-beautiful will not be like that.
Thus done.
But there is nuance: The problem statement says "the total length of all these number's representations doesn't exceed 100 000." It says "these number's" meaning "these numbers" from the second line? It says "the total length of all these number's representations doesn't exceed 100 000." So yes.
Thus solution passes.
Now, think about potential pitfalls: The line may be long, but reading with sys.stdin.read() is safe. Could there be multiple lines? The problem says "second line", but sometimes judges provide numbers on multiple lines if they are long? Usually they stick to statement, but it's safer to read entire input and split. So using sys.stdin.read() is better.
Now, could there be spaces at start/end? It's okay.
Now, check for n=1 and number "0": zero_found True -> prints "0". Good.
Now, is it possible that there is a non-beautiful number that is "0"? No, because zero is beautiful. So fine.
Now, if there is no non-beautiful and S=0, we print "1". That's correct.
Now, should we consider the case where the non-beautiful number is "1"? But "1" is beautiful. So fine.
All good.
But wait, there might be a subtlety: The definition of beautiful number includes numbers with at most one digit '1' and only digits '0' and '1'. That includes numbers like "0" and "1". But also includes numbers like "10", "100", etc. But does it include numbers like "01"? No because leading zeros not allowed.
Thus the product is simply concatenation of the non-beautiful number with zeros from beautiful numbers.
Thus solution seems too trivial, but that's intended.
Now, just to confirm with another example: Suppose n=3: numbers: 5, 10, 100. Non-beautiful=5, beautiful:10 (len=2 -> +1), 100 (len=3 -> +2) => S=3. Output "5"+"000" = "5000". Product =510100=5000. Yes.
Now, what about the case where the non-beautiful number is something like "12345678901234567890"? Then we output it followed by S zeros. That's fine.
Now, is there any chance that the non-beautiful number could be something like "000"? No.
Thus we can implement.
Time complexity O(total length) which is up to 100k, good.
Now, but we need to be careful about memory for "0"*S if S is huge like 100k, that's fine. In Python, "0"*100k creates a string of size 100k, okay.
Now, double-check: In the case where there is no non-beautiful, we output "1"+"0"*S. What if S = 0? Then "1". That's correct.
But what about the case where there is a non-beautiful number and S = 0? Then output just non_beautiful. Fine.
Thus solution seems correct.
But wait, I should consider the possibility that the non-beautiful number could be something like "10...0"? That is beautiful actually. So not.
One more nuance: The guarantee says "the second line contains at least n - 1 beautiful numbers". It doesn't explicitly say that there is at most one non-beautiful. However, by pigeonhole, if there are at least n-1 beautiful, then at most one non-beautiful because total count is n. So indeed at most one.
But what about the possibility that some beautiful numbers are not powers of ten? Could there be "0"? Yes. But if we have "0", product zero and we stop.
Now, potential tricky: If we have a beautiful number "0" and also a non-beautiful number, product zero anyway. So we can just output 0 when we see any "0". That's correct.
Thus solution stands.
Now, before final answer, let's consider if there is any tricky scenario where the product might be misrepresented if we just concatenate zeros. Suppose the non-beautiful number ends with zeros, like "20". Then product = "20" + "0"S = "2000...". That's correct because 2010^S = 20 * 10^S, which is 20 followed by S zeros. So that's fine. So it doesn't matter if non_beautiful has trailing zeros.
Thus solution is trivial: find if any zero; if not, find the one "ugly" number, and sum zeros counts from beautiful numbers, then output ugly + zeros or 1+zeros.
Thus the answer is.
But note: In the case where there are multiple non-beautiful numbers (should not happen), our code would only keep the last one (or overwrite). But guarantee ensures it's at most one. So fine.
We could also add a check: if we encounter a second non_beautiful, we could treat that as product of both? But not needed.
Now, the problem statement: "It is guaranteed that the second line contains at least n - 1 beautiful numbers and the total length of all these number's representations doesn't exceed 100 000." So we rely on that.
Thus answer is ready.
Now, for completeness, we can include an alternative: If we didn't find a non_beautiful, we output 10^S. If we found, output non_beautiful * 10^S by appending zeros.
One more check: Could there be a scenario where there is exactly one non-beautiful, but it's also zero? Already handled as zero.
Ok.
Now I'll produce final solution.
But also note: The problem says "Your task is to write the program that solves exactly the same problem in order to verify Gena's code correctness." So we need to output product as number without leading zeroes. Our method prints without leading zeros because non_beautiful has no leading zeros, and we append zeros after. Also "1"+"0"*S has no leading zeros because it starts with 1. And "0" is just "0". So good.
Thus final code.
Now, additional considerations: In Python, we might need to handle large input quickly. Using sys.stdin.read().split() is efficient.
Edge Cases: If n is 0? Not possible because 1 ≤ n.
If there are extra whitespace lines? The read().split() will ignore them.
Thus solution is ready.
But before final, let's test some random cases manually:
Case: n=4, numbers: 2, 3, 10, 100. Here 2 and 3 are non-beautiful? Actually at most one non-beautiful, but this would have two. Not allowed by guarantee. So such case won't appear.
Case: n=3, numbers: 101, 1, 1. 101 is not beautiful because it contains two '1's? Actually 101 has two '1's (and zeros), so not beautiful. So non_beautiful = "101". Beautiful: 1 and 1: S=0+0=0. Output "101". Product = 10111=101. Correct.
But wait: Check the definition of beautiful: "contains at most one digit '1'". For 100, it's one '1' and zeros, so yes.
Now, consider a number like "1": one '1', zero zeros, at most one '1' and only digits '0' and '1', so beautiful.
Now, consider "0": zero '1's, only '0's, beautiful.
All good.
Thus answer is final.
We should be careful: In the loop, we break when we find a zero. But there might be more numbers after zero? But we already read all numbers into list. So it's fine to break because we already determined answer is 0. No need to process rest.
Now, also need to consider that the non_beautiful could be huge, but we store it as string.
One more nuance: The sum S could be very large, but printing "0"*S may cause memory error if S is huge? S <= total length <= 100k, as argued. So safe.
But could S be greater than 100k? Suppose total length = 100k, n=1, and the only number is beautiful with length 100k => S = 99999, which is less than 100k. So max S is 99999. So safe.
But what about if there is a non_beautiful and many beautiful: S = total length - len(non_beautiful) - (number of beautiful) but number of beautiful = n-1, so S = total length - len(non_beautiful) - (n-1). Since n can be up to 100k, if n is large, S could be negative? That would be a problem. But we should confirm that S cannot be negative. For beautiful numbers, S = sum(len(b)-1) = sum len(b) - count_b. Since count_b = number of beautiful numbers. If all numbers are beautiful, count_b = n. Then S = total length - n. total length could be less than n? But each number has length at least 1. So total length >= n. So S >= 0. If there is a non_beautiful, then count_b = n-1. Then S = sum len(b) - (n-1). total length = len(non) + sum len(b). So S = total length - len(non) - (n-1). Since total length >= len(non) + (n-1) because each beautiful has length at least 1, so S >= 0. So S is non-negative.
Thus safe.
Now, implement is_beautiful efficiently:
def is_beautiful(s):
# return (s.count('1') <= 1) and all(c in '01' for c in s)
# But s.count('1') scans whole string, then all() scans again. But O(n) twice, still fine as total length is 100k. But we can combine.
count_one = 0
for ch in s:
if ch == '1':
count_one += 1
if count_one > 1:
return False
elif ch != '0':
return False
return True
That's efficient.
Now, about reading: sys.stdin.read().split() yields tokens. The first token is n, then n number tokens. We'll assert that we have at least n tokens. We'll slice numbers = tokens[1:1+n] to avoid extra tokens due to stray newlines? But if input is exactly as described, tokens[1:] will have n tokens. But it's safe to take first n after first.
Better: n = int(tokens[0]); numbers = tokens[1:]; assert len(numbers) >= n; numbers = numbers[:n]; This ensures we only process n numbers, ignoring any extra (unlikely).
We'll implement:
def main():
import sys
data = sys.stdin.read().split()
if not data:
return
n = int(data[0])
numbers = data[1:1+n] # ensures we have exactly n numbers
code
zero = False
S = 0
ugly = None
for s in numbers:
if s == "0":
zero = True
break
if is_beautiful(s):
S += len(s) - 1
else:
# should be at most one ugly
ugly = s
if zero:
print("0")
return
if ugly is None:
print("1" + "0"*S)
else:
print(ugly + "0"*S)
That's it.
Now, what about Python's recursion? Not needed.
Thus solution done.
But I realize that there is a nuance: In the problem "There are exactly n distinct countries and the i-th country added ai tanks to the game." They are distinct countries but that doesn't matter.
Now, we must ensure that we don't accidentally treat "0" as beautiful and add S? In our loop, we check if s == "0" first and set zero = True, break. That's correct. But what if zero appears later? We break on first zero. But if there are multiple numbers and one is zero, product is zero anyway. So break is fine. But we should be careful: If zero appears but we break early, we haven't processed the rest. That's fine because answer is zero. But we need to ensure we have read all numbers? Not needed.
But we used break, but we already have the list. It's fine.
Now, also need to consider that the non-beautiful number might be "0"? But our earlier condition s == "0" catches that and we treat it as zero. That's correct because "0" is beautiful, but product zero anyway. So that's fine.
Thus solution complete.
Now, I'll provide final answer with explanation.
But let's also think about potential alternative: Multiply all numbers? But obviously not possible due to size.
Thus answer is as above.
Now, final note: The problem statement says "Just in case." Possibly implying that the solution is trivial once you realize the pattern. So I'm confident.
Now, final code in Python. We'll include appropriate imports.
Edge Cases tested.
Now produce final answer. ```python
import sys
def is_beautiful(s: str) -> bool:
"""Check if a number string is beautiful:
only digits '0' and '1', and at most one '1'."""
count_one = 0
for ch in s:
if ch == '1':
count_one += 1
if count_one > 1:
return False
elif ch != '0':
return False
return True
def main() -> None:
data = sys.stdin.read().split()
if not data:
return
n = int(data[0])
numbers = data[1:1 + n] # exactly n number strings
code
zero_found = False
S = 0 # sum of trailing zeros of beautiful numbers
ugly = None # the (possible) non‑beautiful number
for s in numbers:
if s == "0": # zero makes the whole product zero
zero_found = True
break
if is_beautiful(s):
S += len(s) - 1 # number of zeros after the leading '1'
else:
ugly = s # at most one such number
if zero_found:
print("0")
return
if ugly is None:
print("1" + "0" * S) # all numbers are powers of ten
else:
print(ugly + "0" * S) # multiply the ugly number by 10^S