We need to check if we can delete some digits (maybe none) from the given decimal string (length up to 100) to obtain a number (no leading zeroes, except that the resulting number can be "0"? Actually note: "forms a non-negative integer, doesn't have leading zeroes". So if the number is 0, that's allowed (as in example input 10 output 0). Usually "0" has no leading zeroes. So we can have result "0". But can we have result like "00"? No, because that would have leading zero. So only allowed if the result string is either a single '0' or a non-zero number without leading zeros.
We need to find any subsequence (maintaining order) that is divisible by 8. Since divisibility by 8 depends only on last three digits (in decimal). Actually, a number is divisible by 8 if its last three digits (in base 10) form a number divisible by 8. For numbers with fewer than 3 digits, we can just check directly: a one-digit number divisible by 8 => 0,8; two-digit number divisible by 8 => from 00 to 99 divisible by 8, but must obey no leading zeros. However, for subsequence we can pick any digits, so we can aim to produce a number with at most 3 digits that is divisible by 8. Because any larger number can be trimmed to its last three digits? Actually, if we have a number that ends with three digits divisible by 8, the whole number is divisible by 8. But we might not need more than 3 digits; we could just pick those three digits (or less if they form a number divisible by 8). So the problem reduces to finding a subsequence of the given digit string that is either a one-digit number (0 or 8), or a two-digit number divisible by 8 (like 16,24,..,96, but also 00? but leading zeros not allowed except 0 alone; "00" would be invalid because leading zero; "08" also invalid because leading zero. So two-digit number must not start with '0' unless it's "0"? Actually a two-digit number can't have leading zero. So the first digit cannot be '0'. So valid two-digit numbers are from 10 to 99, but only those divisible by 8: 16,24,32,40,48,56,64,72,80,88,96. Note: 00,08, etc are not allowed as they start with 0. Also 0 itself is allowed (single digit 0). Similarly for three-digit numbers: the first digit cannot be '0' (unless the whole number is exactly "0"? but three-digit can't have leading zero, so first digit must be 1-9). But if we pick three digits as a subsequence, they might include leading zeros if the first chosen digit is '0', which would be invalid if we interpret as number with leading zero. So we must ensure that the subsequence we output does not have a leading zero, unless it's the single digit "0". So we need to find a subsequence of digits that is divisible by 8 and satisfies the leading-zero constraint.
Given that length is up to 100, we can search for any subsequence of length up to 3 that meets the conditions? But is it sufficient to only consider subsequences of length at most 3? Let's think: Suppose there is a longer subsequence that is divisible by 8 but no subsequence of length ≤ 3 exists? Could that happen? For example, consider the number 1000. Subsequence "1000" is divisible by 8 (since 000 divisible by 8). But we can also pick "0" or "00"? "0" is allowed (single digit 0). So there is a short one. What about number 1002? 1002 mod 8 = 2? Actually 1002 mod 8 = 2? 8*125=1000, remainder 2. So 1002 is not divisible by 8. But could there be a longer subsequence (like picking digits 1,0,0,2 maybe reorder? No we can't reorder. Subsequence: we can delete some digits. So possible numbers: 100, 102, 100, 12, 10, 2, etc. Check if any divisible by 8: 100 mod8=4; 102 mod8=6; 10 mod8=2; 12 mod8=4; 2 mod8=2; 0 mod8=0? Wait there is digit 0? Digits: '1','0','0','2' so we can pick '0' as a single digit, so "0" works. So yes.
What about a number that has no single digit 0 or 8, no valid two-digit, no valid three-digit (with proper leading digit) but has a longer subsequence whose last three digits are divisible by 8? Example: digits: "1 1 6"? That's three digits already. But if the first digit is 0 then three-digit with leading zero invalid, but maybe we can include extra digits before to avoid leading zero? Actually if the first digit of the subsequence is 0, that's leading zero, invalid (except if entire number is "0"). So if we have a subsequence starting with 0, it's invalid. So we could perhaps take a longer subsequence where the first digit is non-zero, and the last three digits form a number divisible by 8. But if such a longer subsequence exists, then the subsequence formed by only those last three digits (or perhaps fewer if those three start with zero) might be invalid due to leading zeros. For example, suppose we have digits: "2 0 4". "204" is divisible by 8? 204 mod8 = 4? Actually 8*25=200, 204=200+4 => 4. So not divisible. But say "1 0 0 8"? 1008 mod8=0. The three-digit ending "008" is divisible by 8, but "008" starts with 0, invalid if taken alone. However, if we take the whole subsequence "1008", the first digit is '1' non-zero, so it's valid. So there is a valid longer subsequence despite the last three digits alone being invalid due to leading zero. But is there always a subsequence of length ≤ 3 that is valid if any valid subsequence exists? Not necessarily. Consider string "1008". The valid subsequences divisible by 8: "0" (digit 0 exists? Yes there is a '0' at second or third position) => "0" works, so length 1 works. But to make a case where no length 1 or 2 or 3 works but a longer works, we need a string that has no '0' or '8' as a digit? Actually any '0' would give a length-1 solution. So to avoid length-1 solution, there must be no digit '0' or '8'. So digits only from {1,2,3,4,5,6,7,9}. Also length-2 solution: two-digit numbers divisible by 8 and with first digit non-zero and second digit maybe anything. The allowed two-digit numbers divisible by 8 are: 16,24,32,40,48,56,64,72,80,88,96. Among these, some contain '0' (40,80) or '8' (48,88) or '0' again. So if we avoid digits 0 and 8 entirely, the only possible two-digit numbers divisible by 8 are: 16,24,32,56,64,72,96. These use digits {1,6,2,4,3,5,6,7,9}. So if the string contains two digits in order forming one of these pairs, we could have a length-2 solution. So to avoid that, we need to not have any of these ordered pairs as subsequence.
Length-3 solutions: Three-digit numbers divisible by 8, with first digit non-zero. The set of three-digit numbers divisible by 8 from 100 to 999. Many possibilities. But we could consider if there is a string with no single digit 0 or 8, no valid two-digit subsequence, no valid three-digit subsequence, yet there is a longer subsequence divisible by 8. Is that possible? Let's analyze.
We need to consider that any number divisible by 8 has its last three digits (in decimal) determining divisibility. So if we have a subsequence of length >3, we can consider its last three digits. Those three digits (as a string of three digits, possibly with leading zeros within the three) when considered as a three-digit number (with possible leading zeros) are divisible by 8. However, the subsequence formed by just those three digits may start with zero, making it invalid as a standalone number because of leading zero. But if we include at least one non-zero digit before them, the whole number is valid. So the existence of a valid longer subsequence implies that either there exists a valid subsequence of length ≤ 3 (maybe the last three digits themselves, if they start with non-zero) OR the last three digits include leading zeros but there is a non-zero digit before them. In such a case, the subsequence consisting of that non-zero digit plus the three digits? Actually, could we have a valid subsequence of length 4 that is necessary because the three-digit suffix starts with zero. But maybe we can drop the leading zero(s) from the suffix and still have a number divisible by 8? For instance, if the suffix is "008", we could just take "8" as a single digit? But that would be valid if '8' is present somewhere. But maybe '8' is not present? In "1008", there is '8' at the end, so single digit 8 works. But suppose we have "200016"? That has '0','0','1','6' at the end? Actually, consider "200016": digits: 2,0,0,0,1,6. The subsequence "200016" itself is divisible by 8? 200016 mod8: 825002=200016? 250028=200016, yes divisible. Its last three digits are "016" which is divisible by 8 (since 16 is divisible by 8). But "016" as a three-digit number starts with 0, so it's invalid alone. Are there any shorter valid subsequences? Single digits: there is '0' present? Yes, there are zeros. So "0" works. So length-1 exists.
What if we avoid any zero or eight entirely? Let's try to construct a string with digits only from {1,2,3,4,5,6,7,9} and no subsequence of length 1 or 2 that is divisible by 8, and no subsequence of length 3 (with first digit non-zero) divisible by 8, yet there is a longer subsequence divisible by 8. Since any longer subsequence's last three digits are some three digits from the set, but if they contain a zero or eight, that would be a length-1 or length-2? Actually zero and eight are not allowed in the digit set. So the three-digit suffix would consist only of digits from {1,2,3,4,5,6,7,9}. If that suffix is divisible by 8 and its first digit is non-zero (it is non-zero because digits are 1-9), then that suffix alone is a valid three-digit subsequence (since we can pick just those three digits). So a longer subsequence would be unnecessary because the three-digit suffix itself is valid. But could the three-digit suffix start with a zero? Not possible if digits are only 1-9. So the suffix would be a valid three-digit number. Thus, if there is a valid longer subsequence, then its last three digits (as a subsequence) form a three-digit number divisible by 8 with first digit non-zero, which is itself a valid subsequence of length 3. However, we must ensure that those three digits appear in order in the original string. Since they are part of the subsequence, they appear in order. So indeed, if there exists any subsequence (of any length) that is divisible by 8 and does not have leading zeros (except the single 0 case), then either it is of length 1, 2, or 3, or if longer, we can truncate it to its last three digits, but we need to check that truncation does not introduce a leading zero issue. As argued, if the longer subsequence is valid (no leading zeros), its first digit is non-zero. The last three digits, when taken alone, might start with zero, making that truncation invalid. Example: "1008" valid longer, but last three digits "008" start with zero, invalid. However, in that example, there is a single digit '8' present, which is a valid length-1 solution. So maybe we can always find a solution of length at most 3? Let's test if there exists a case where the only valid subsequence(s) are longer and any truncation to last three yields an invalid leading zero, and also there is no single digit 0 or 8, no two-digit divisible by 8 with non-zero first digit, and no three-digit divisible by 8 with non-zero first digit (since the three-digit suffix of the longer subsequence starts with 0). Could there be such a case? That would require that any longer valid subsequence must have its last three digits start with 0, and there is no digit 0 or 8? Wait, if the last three digits start with 0, that means among the three digits, the first is '0'. So the longer subsequence contains a '0' digit. If the original string contains any '0', then the single digit '0' is a valid subsequence (since "0" is allowed). So if there is a zero anywhere, we have length-1 solution. So for the longer subsequence to be the only possible solution, the string must contain no '0' digits? But then the last three digits can't start with 0 because 0 is not present. So any three-digit suffix must start with non-zero. So truncation yields a valid length-3 subsequence. Thus, if there is any valid subsequence of length >=3, and the string contains no '0', then we can take its last three digits as a valid length-3 subsequence (since first digit of those three is non-zero). But wait: what if the longer subsequence has length exactly 3? That's already length-3. If length >3 and no zeros exist, the last three digits are from digits 1-9, so they start with non-zero, and they are a subsequence (we can select just them). So yes, that yields a valid length-3 solution.
Thus, if there is any solution, there must be a solution of length at most 3. Additionally, if there is a solution of length 0? Not allowed; must contain at least one digit.
Therefore, we can simply check for the existence of a subsequence of length 1, 2, or 3 that is divisible by 8 and respects the no-leading-zero rule. Since the length is small (100), we can brute-force all subsequences of length up to 3. There are O(n^3) possibilities, with n up to 100, that's at most 1e6, which is fine. Alternatively, we can precompute all numbers divisible by 8 up to 1000 and check if any appears as a subsequence with the proper leading condition.
But careful: For length 2 or 3, we must ensure that the subsequence does not have leading zero. For length 1, "0" is allowed, "8" allowed. For length 2, first digit cannot be '0'. For length 3, first digit cannot be '0'.
Also note that we can also have a number with more than 3 digits, but as argued we can find a smaller one if exists. But there might be a case where only solution is longer and we cannot reduce to 3 because of leading zeros and absence of zeros? Actually we argued that if zeros are absent then reduction works. But if zeros are present, we have a length-1 solution "0". Wait: If zeros are present, "0" is a valid solution. So indeed, if the original string contains any zero, answer is always "YES" with "0". But is that always true? Check: string "101"? contains zero, so "0" is a subsequence? We can pick the zero digit. Yes. So always YES if there is any '0'? But wait, what about constraints: the result must be a non-negative integer, doesn't have leading zeroes. "0" is allowed. So yes, any string that contains at least one '0' yields answer "YES" with "0". However, check example: Input "10" output "YES" and "0". That's consistent. Input "111111" has no zero, output "NO". So indeed if any '0' exists, we can output "0". But careful: is it always allowed to output "0"? Yes, because 0 is divisible by 8. So we can simply check for existence of '0' and output "YES\n0". But wait: what about the possibility that the only zero is at the very beginning? For example, input "0" is valid (non-negative integer without leading zeroes). That's okay. Input "01"? Not allowed because input doesn't have leading zeroes, so first digit is not zero unless the number is exactly 0. So the input itself won't have leading zeros. But it could contain zero inside, like "101". So we can pick that zero. So if the string contains '0', we can always output "0". But is there any catch? Could there be a case where the string contains '0' but we are forced to output something else? No, any answer is acceptable. So we can do that.
But before jumping, note: The problem asks to remove some digits (possibly none) to get a number divisible by 8. If the number itself is divisible by 8, we could output it as is. But "0" is simpler. So indeed, presence of any zero gives a trivial solution. However, consider input "8". Contains '8', so we could output "8". Also contains no zero, but we could output "8". So that also gives a length-1 solution. So if there is an '8', we could output "8". So the presence of either '0' or '8' yields a one-digit solution. So we can check for '0' first, then for '8'? Actually order doesn't matter, but we can just search for one-digit solutions: if any digit is '0' or '8', output that digit.
Now if no digit 0 or 8, we need to check for two-digit subsequences that are divisible by 8 and first digit not '0' (which is automatically true because digits are 1-9 except 0,8, so first digit is not 0). But we also need to ensure that the two-digit number is divisible by 8. Since we don't have 0 or 8, the allowed two-digit numbers divisible by 8 from the list: 16,24,32,56,64,72,96. (40,48,80,88 contain 0 or 8, not allowed because digits must be from set without 0/8? Wait: if the string has no 0 or 8, then those numbers cannot be formed anyway, but we could still have a two-digit number like 40 if we have digits 4 and 0, but we said no zero present, so can't. So only the ones without 0 or 8 are possible. But note: 56, for example, uses digits 5 and 6, both not 0/8. So if we have '5' and later '6', we can get 56. So check for those pairs.
If no two-digit, check three-digit subsequences (first digit non-zero) divisible by 8. Since no zeros present, all digits are from {1,2,3,4,5,6,7,9}. So we can iterate over all triples i<j<k and form number = (100d[i] + 10d[j] + d[k]) and check divisibility by 8. If any found, output that triple as string.
If none found, output "NO".
But we must also consider the possibility that the original number might itself be divisible by 8 but we might have missed because we didn't check longer sequences? But as argued, if the number is divisible by 8 and has no leading zeros (input guaranteed no leading zeros), then it is itself a valid subsequence (by removing zero digits? Actually we don't need to remove any). So that would be a valid solution. Would it be captured by our search of length up to 3? Not necessarily: if the number is large, e.g., "1000" is divisible by 8, but we would find a single digit '0' (since there is zero), so we would output "0". That's okay. But if the number is divisible by 8 but contains no zero and no eight? Example: "16" contains digits 1 and 6, both not 0/8, and "16" is divisible by 8? 16 mod8=0, yes. Our two-digit check would find it. "24" similar. Three-digit like "112"? 112 mod8=0, but digits 1,1,2, no 0/8. So three-digit check would find it if we consider all triples. But if the number is longer, e.g., "1112"? 1112 mod8? 8139=1112, yes divisible. But it contains digit '2', '1's. No 0/8. We might find a subsequence of length up to 3: The last three digits "112" are divisible by 8? Actually "112" is divisible by 8? 814=112, yes. Are those three digits present as a subsequence? In "1112", we have digits: positions:1:1,2:1,3:1,4:2. Can we pick three digits that form "112"? Yes: pick first '1' (pos1), second '1'? Actually we need "1,1,2". We can pick pos1 (1), pos2 (1), pos4 (2) => 112. That's a valid three-digit subsequence with first digit non-zero (1). So it's captured. So indeed any number divisible by 8 will have a three-digit suffix (perhaps with leading zeros) divisible by 8. If the number contains no zero, then that suffix's first digit is non-zero, so it's a valid three-digit subsequence. But what if the number contains zeros but no 0/8? That's impossible because zero is 0. If it contains zeros, then we have length-1 solution. So fine.
Thus algorithm:
- Read string s.
- If s contains '0', output YES and 0. (But careful: if s is something like "0", that's fine.)
Else if s contains '8', output YES and 8.
- Else, for all i<j (two-digit subsequences):
num = (s[i]-'0')*10 + (s[j]-'0')
if num % 8 == 0: output YES and s[i]s[j].
- Else, for all i<j<k (three-digit subsequences):
num = (s[i]-'0')*100 + (s[j]-'0')*10 + (s[k]-'0')
if num % 8 == 0: output YES and s[i]s[j]s[k].
- Else output NO.
But need to be careful: For two-digit, ensure that the two-digit number is at least 10? Actually it could be 00? But we already handled no zero, so first digit can't be zero anyway. But what about "08"? Not possible because no zero? Actually if there is zero, we would have already output "0". So fine.
But is it possible that a valid two-digit number like "16" exists but we skip because we output "0" earlier? We output "0" only if there is a zero digit. But if there is a zero, then "0" is a valid answer, and it's simpler. So it's okay to output "0". But must ensure that "0" is indeed a subsequence: picking any zero works. So yes.
Thus the algorithm is straightforward.
But there is nuance: The problem statement: "After the removing, it is forbidden to rearrange the digits." We are not rearranging. Subsequence preserves order.
We also need to output the resulting number exactly as the subsequence, not as integer (so with potential leading zeros? But we must not have leading zeros. Our selection for two and three-digit ensures first digit is not '0' because we only consider those from digits set without zero? Actually we are not explicitly checking that first digit != '0' for two and three-digit. But since we only go into those steps if there is no zero digit at all? Wait: step2: we output "0" if s contains any '0'. So if we reach step3 (two-digit search), that means s does NOT contain any '0'. Therefore all digits are from 1-9 except maybe 8? But we also output for '8' earlier. So after step2, we know s has no '0' and no '8'. So all digits are from {1,2,3,4,5,6,7,9}. So any subsequence we pick will have first digit non-zero automatically. So we don't need to separately check leading zero condition. However, there is a corner case: what if s contains '0' but we still might have a two-digit solution that is better? Not required; any solution is fine. So we can output "0". That's fine.
But what about the possibility that s contains '0' but the only zero is at the very beginning? Input "0" is allowed. Input like "101" has zeros inside. So we can pick zero. So it's fine.
Thus the algorithm is correct.
But wait: there is another nuance: The number 0 is divisible by 8. However, if the input contains zero but we output "0", we must ensure we are allowed to pick a subsequence that results in "0". According to the problem: "the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8." The number 0 satisfies that (it's a single digit, no leading zero). So yes.
Now, what about the case when the input itself is "0"? It contains '0', we output YES and 0.
But what if the input is something like "000"? Not allowed because representation doesn't contain leading zeroes. So input length at least 1 and first digit is not '0' unless number is exactly "0". So we are safe.
Thus solution is simple.
But wait, we need to consider if there could be a solution that is a single digit '0' but the string contains '0'? Yes, always. So presence of any zero gives a solution.
But could there be a case where the string contains zero but the only zero is the first digit and the number is like "0"? That's fine. Could there be a number like "10"? Contains zero, we output 0. That matches example.
Now what about string that doesn't contain 0 or 8, but contains a two-digit subsequence divisible by 8, we output that. Example: "16" would output "16"? But does 16 divisible by 8? 16 mod 8 = 0, yes. So we output "16". But our algorithm would find "0"? No zero, so go to step3 and find "16". That's fine.
Now what about three-digit subsequence like "112" we find.
But is it always sufficient to check only up to three digits? Let's double-check the proof: If there is any valid subsequence (call it t) that is divisible by 8 and has no leading zeros (except possibly being "0" itself). If t length >= 4, then consider its last three digits. Those three digits form a number divisible by 8. If the first digit of those three is not '0', then those three digits alone are a valid subsequence (since they appear in order, and the first digit is non-zero, so no leading zeros). If the first digit of those three is '0', then the original subsequence t contains a '0' in that position. However, does the existence of a '0' in t guarantee that the original string has a '0'? Yes, because t is a subsequence. So the original string contains a '0'. Therefore we would have found a '0' in step 1 and output "0". So either we already have a solution, or we can reduce to a three-digit subsequence. Thus indeed checking length 1 (0 or 8) and length 2 and 3 suffices.
But careful: The case where t itself is "0"? That's length 1. So covered.
Therefore, algorithm works.
Now implementation details: We'll read s as string. Let n = s.length().
We'll first loop over characters; if any char == '0', output YES and 0, return.
Else if any char == '8', output YES and 8, return.
Then for two-digit: for i in 0..n-2, for j in i+1..n-1, compute num = (s[i]-'0')*10 + (s[j]-'0'); if num%8==0, output YES and s[i]s[j] as string, return.
Then for three-digit: for i in 0..n-3, for j in i+1..n-2, for k in j+1..n-1, compute num = (s[i]-'0')*100 + (s[j]-'0')*10 + (s[k]-'0'); if num%8==0, output YES and s[i]s[j]s[k], return.
If none, output NO.
But note: There is a possibility that a two-digit solution might exist but we output earlier for '0' or '8'. Since we already output for '0' if any zero exists, that is correct. But what if there is no zero but there is an '8'? We output "8". That's also fine. So we don't need to check further.
But we must consider: Could there be a case where s contains '0' but the only zero is at the very end and we output "0", but maybe we cannot pick that zero? Of course we can pick it. So fine.
Now edge: Input length 1. Example: "3". No 0, no 8. Then we check two-digit? loops won't run. three-digit? not run. Output NO. That's correct because 3 mod8=3.
Input "8": contains '8', output YES 8.
Input "0": contains '0', output YES 0.
Now input like "100": Contains '0', output 0. But also 100 divisible by 8? 100 mod8=4, not divisible. But we output 0, which is valid. So answer is YES with 0.
But is there any scenario where output 0 might be considered invalid because the problem expects at least one digit and no leading zeroes; 0 is okay.
Thus solution is trivial.
But wait, we must be cautious: The problem statement says: "Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8." So 0 is allowed as a single digit. However, what about the case when the input number is something like "100" and we output "0". That is indeed removing some digits (the '1' and the first '0' maybe) leaving just one '0'. So it's fine.
Now, the algorithm above runs O(n^3) in worst-case n=100 => 1e6 iterations, fine.
But we can also optimize using DP or precomputation, but not necessary.
One nuance: The two-digit numbers we consider: are all two-digit numbers divisible by 8? But we must ensure they are at least 10? Actually a two-digit number means it has two digits. If the first digit is non-zero, it's at least 10. So yes. But what about the number "00"? That's not allowed. So fine.
But is it possible that a two-digit number like "08" is considered? We wouldn't because we only consider if first digit is '0'? Since we already eliminated zeros, first digit cannot be 0. So no.
Thus algorithm stands.
Now let's test some cases manually.
Case: "9": no 0/8, no 2-digit, no 3-digit => NO.
Case: "16": digits:1,6. no 0/8 => go to 2-digit, i=0 j=1: num=16%8=0 => output "16". Works.
Case: "24": output "24".
Case: "32": output "32".
Case: "40": digits:4,0 => has zero => output "0". Could also output "40"? But 40 mod8=0, and first digit non-zero, but we output "0". That's okay. The answer may be any.
Case: "48": has '8' => output "8". Also 48 divisible by 8, but 8 is shorter.
Case: "56": output "56".
Case: "64": output "64".
Case: "72": output "72".
Case: "80": zero => output "0". (80 mod8=0 but we output 0.)
Case: "88": has '8' => output "8".
Case: "96": output "96".
Now three-digit: "112": digits 1,1,2 no 0/8 => two-digit? check 11%8=3,12%8=4,12%8=4? Actually two-digit pairs: (1,1)->11%8=3, (1,2)->12%8=4, (1,2 from first and third) ->12%8=4, none divisible. So go three-digit: 112%8=0 => output "112". Works.
But what about "104"? Contains zero => output "0". But 104 mod8=0, but 0 is okay.
But what about "120"? Contains zero => output "0". 120 mod8=0? 8*15=120, yes, but we output 0, fine.
Now consider a tricky case: "2008". Contains zero => output "0". But also "8" is present, so could output "8". Both fine.
Now consider a case with no 0 or 8 but still no 2-digit or 3-digit subsequence divisible by 8, but maybe longer subsequence works. For instance, can we construct a string of digits from {1,2,3,4,5,6,7,9} that has no subsequence of length 1,2,3 divisible by 8, but has a subsequence of length 4 or more divisible by 8? According to our earlier reasoning, if a longer subsequence exists, then its last three digits (which are from the same set) would be a valid 3-digit solution, because the first digit of those three cannot be zero. But could it be that those three digits, while divisible by 8, do not appear as a subsequence in the original string? Wait, they do appear as a subsequence because they are part of the longer subsequence; they appear in order. So yes, they form a subsequence. So if the longer subsequence exists, then the three-digit suffix is also a subsequence. So there would be a 3-digit solution. So it's impossible for a longer solution to exist without a 1/2/3-digit solution, provided we handle zeros correctly. So our search is exhaustive.
Thus solution stands.
Implementation note: When outputting the result, we must output exactly the subsequence string, not as integer (to preserve leading zeros? But we ensure no leading zeros except single zero, so it's fine). We'll construct a string from the selected characters.
Now, complexity O(n^3) is fine. But we can also do smarter: precompute all numbers from 0 to 999 that are divisible by 8, and for each check if it exists as subsequence (respecting leading zero constraints). Since n is 100, even checking each number by scanning the string is O(1000 * n) = 100k, which is also fine. But simpler to just triple loop.
But careful: When we output a two-digit subsequence, we need to ensure that the two-digit number is at least 10, i.e., first digit not zero. Since no zeros present in this branch, it's fine. But if we wanted to be extra safe we could add a check: if s[i] != '0'. But it's not needed.
Now we must also handle the case where the input is "0". Then step1 sees '0' and outputs "0". That's correct.
Now, what about input "00"? Not allowed because leading zeroes not allowed.
Thus done.
But wait, I recall that Codeforces problem "C. Division by Eight" (or similar) often requires to find a subsequence divisible by 8. The typical solution indeed checks for 1-digit, 2-digit, 3-digit. Some solutions also check for numbers with more than 3 digits, but it's proven sufficient.
Thus we can implement.
Now consider testing:
Input: "3454" from example. s = "3454". Contains '0'? No. Contains '8'? No. Two-digit? Check pairs: 34%8=2, 35? Actually digits: 3,4,5,4. i=0 j=1: 34%8=2; i=0 j=2: 35%8=3; i=0 j=3: 34%8=2; i=1 j=2: 45%8=5; i=1 j=3: 44%8=4; i=2 j=3: 54%8=6. None. Three-digit: triples: (3,4,5)=345%8=1? 8*43=344, remainder 1. (3,4,4)=344%8=0. So output "344". That matches example.
Input: "10": contains '0', output "0". Example says output "YES" and "0". Yes.
Input: "111111": no 0/8, check two-digit? 11%8=3, etc none. Three-digit: 111%8=7, 111%8=7, etc all 111? Actually any triple of ones gives 111%8=7. So none. Output NO. Matches.
Thus algorithm works.
Now, we should consider if there is any subtlety: The result must be a non-negative integer and must not have leading zeroes. Our three-digit selection ensures first digit not zero because we are in branch where there is no zero at all. But what about two-digit selection? Same. So fine.
But wait, what if the string contains a zero but we did not output "0"? We do output "0". That's fine.
But what if the string contains zeros but also has only zeros and maybe we output "0"? Example: "0" output "0". That's fine. For "00"? not allowed.
Now, check if there is any case where a valid subsequence could be "0" but we might be forced to not output it because maybe the problem expects a number with at least one digit and non-negative, which "0" qualifies. So it's fine.
Thus answer.
Now implementation steps:
- Read string s.
- if s.find('0') != string::npos: print YES, then "0". Return.
- else if s.find('8') != string::npos: print YES, then "8". Return.
- else:
n = s.length()
// Two-digit
for i in range(n):
for j in range(i+1,n):
num = (int(s[i])*10 + int(s[j]))
if num % 8 == 0:
print YES and s[i]s[j]; return
// Three-digit
for i in range(n):
for j in range(i+1,n):
for k in range(j+1,n):
num = (int(s[i])*100 + int(s[j])*10 + int(s[k]))
if num % 8 == 0:
print YES and s[i]s[j]s[k]; return
// else NO
print NO
But note: The two-digit check might be redundant because the three-digit check covers everything? But two-digit is needed because three-digit may not exist but two-digit exists. Also two-digit numbers like 16 can be found but also as three-digit like 016? But 016 not allowed because first digit zero. Since we have no zeros, 016 cannot appear as a three-digit subsequence anyway (if the first digit is 0, but we have no zeros). So we need separate two-digit.
Also, note: Could there be a two-digit number that is divisible by 8 but first digit is zero? That would be like 08, but that's essentially 8. But we already handled 8 as single digit. So not needed.
Thus code.
But one more nuance: The input length can be up to 100, so triple nested loops: 100^3 = 1,000,000, each doing a mod operation. That's fine in C++ and Python (though Python might be okay for 1e6). But careful: in Python, 1e6 operations is trivial. So fine.
But we could optimize: Precompute all numbers divisible by 8 from 0 to 999, then for each check if it's a subsequence with the appropriate leading zero constraints. That may be simpler to code? But triple loops are straightforward.
Now, be careful about string concatenation: for two-digit, output s[i] + s[j] as string. For three-digit, s[i] + s[j] + s[k].
Now, one more thought: Are there any numbers divisible by 8 that are 2-digit but have leading zero? Not relevant.
Now, confirm that we didn't miss the case where the number itself (maybe longer) is divisible by 8 but contains zeros? We output "0" which is valid but might not be the only answer; we output any answer, so it's fine.
But there is a subtlety: Suppose the input is "8" (single digit). According to our code, we first check for '0'? Not present. Then check for '8'? present, output "8". Good.
Suppose input is "888" (contains '8'), output "8". That's okay.
Suppose input is "080"? Not allowed due to leading zero.
Now, suppose input is "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" (many zeros). We output "0". That's valid.
All good.
But is there any case where the input contains '0' but we cannot output "0" because we must output a number with at least one digit? "0" is one digit. So it's fine.
Now, confirm: Is it allowed to remove all digits? No, must contain at least one digit. So picking a single '0' is allowed.
Thus final answer.
Now implement in Python:
def solve():
import sys
s = sys.stdin.readline().strip()
if '0' in s:
print("YES")
print("0")
return
if '8' in s:
print("YES")
print("8")
return
n = len(s)
# two-digit
for i in range(n):
for j in range(i+1, n):
num = (ord(s[i]) - 48) * 10 + (ord(s[j]) - 48)
if num % 8 == 0:
print("YES")
print(s[i] + s[j])
return
# three-digit
for i in range(n):
for j in range(i+1, n):
for k in range(j+1, n):
num = (ord(s[i]) - 48) * 100 + (ord(s[j]) - 48) * 10 + (ord(s[k]) - 48)
if num % 8 == 0:
print("YES")
print(s[i] + s[j] + s[k])
return
print("NO")
Test with examples.
But wait: The two-digit check: what about the number "00"? Not possible because no zero present. But if s contains zero we would have output earlier. So fine.
Now, think about the three-digit check: what if a three-digit number like "000" is divisible by 8 (0 divisible), but we never get there because zero would have triggered earlier. So fine.
Now, consider s = "2008": we output "0" immediately. That's acceptable.
Now, is there any scenario where we output "0" but maybe the problem expects a positive integer? No, it says non-negative integer; 0 is non-negative.
Thus answer is correct.
Now we need to ensure we handle the case where s length is 1 and it is '0' or '8'. Already covered.
Now, think about s = "9". No 0/8. Loops skip. Output NO.
Now, s = "12": two-digit 12%8=4, no. Output NO? But 12 is not divisible by 8. So correct.
Now s = "16": two-digit 16%8=0, output "16". That's valid.
Now s = "24": similar.
Now s = "32": similar.
Now s = "40": contains 0 -> output "0". That's fine.
Now s = "48": contains 8 -> output "8".
Now s = "56": output "56".
Now s = "64": output "64".
Now s = "72": output "72".
Now s = "80": output "0". Could also output "80"? but we output "0".
Now s = "88": output "8".
Now s = "96": output "96".
Now s = "104": contains 0 -> "0".
Now s = "112": no 0/8, two-digit? check (1,1)->11%8=3, (1,2)->12%8=4, (1,2) from first and third ->12%8=4. No. three-digit 112%8=0, output "112".
Now s = "120": contains 0 -> "0".
Now s = "200": contains 0 -> "0".
Now s = "208": contains 0 and 8? 0 present, so output "0". But maybe expected "208"? Both acceptable.
Now s = "216": contains no 0? Actually digit '2','1','6' no 0/8. Two-digit? 21%8=5, 26%8=2, 16%8=0? Check subsequence: positions: 1:'2',2:'1',3:'6'. We have 1 and 6? (j=2, k=3) gives "16"? Yes, digits at positions 2 and 3: '1','6' => 16%8=0. So two-digit solution exists. Our algorithm will find that and output "16". That's valid and divisible by 8. But is "16" a subsequence? Yes (remove first digit). So answer "YES" with "16". That's fine.
Now, s = "1000": contains 0 -> "0". Good.
Thus correct.
Now, let's consider possible pitfalls:
-
The two-digit loops should consider all pairs, but the order matters: i < j. That's correct.
-
For three-digit loops i<j<k.
-
The mod operations: we compute integer num from digits.
-
We output as string by concatenating characters.
Now, is there any case where the two-digit number is something like "08"? That would be num=8, divisible by 8. But if s contains '0' and '8', we would have output "0" earlier (since zero exists). So we don't get to two-digit. If s contains '0' but no '8'? Actually zero exists, we output "0". So we wouldn't output "08". That's fine because "08" has leading zero. But if s contains no zero? Then '0' not present, so "08" can't be formed. So safe.
Now, but what about s containing zero but we still might have a two-digit number like "80"? That would be num=80%8=0. But we output "0" earlier. So okay.
Thus algorithm is fine.
Now, we need to think about time complexity: triple loops up to 100^3 = 1,000,000, plus double loops ~5000, so fine.
Now, let's also consider a more rigorous proof:
Lemma: If there exists a subsequence t (|t|>=1) divisible by 8 and t[0]!='0' (or t="0"), then either t contains a digit '0' or '8' (=> single digit solution), or |t|<=3, or we can take last three digits. But the last three digits might start with '0', then t contains '0' => single digit solution. So indeed.
Thus our search is complete.
Now, potential improvement: Since we are writing code, we can directly loop through all numbers from 0 to 999 that are divisible by 8 and check if they appear as a subsequence with no leading zeros (except for 0 itself). That might be easier:
- if 0 in s: print YES 0.
- else for each num from 8 to 999 step 8:
convert to string with length 1,2,3 (without leading zeros, but we can include leading zeros? Actually we must avoid leading zeros. So we only consider numbers that when represented in decimal have no leading zeros, i.e., for 1-digit: 8; 2-digit: 10-99; 3-digit: 100-999. But also numbers like 0 handled separately.
Check if the string representation appears as a subsequence in s. If yes, output it.
But then we need to also handle numbers like 8 (one-digit), 16, etc. But note: two-digit numbers like 08 are not considered because they have leading zero. So it's fine.
But we already have a simpler triple loop.
Thus final solution in Python.
But we need to consider input reading: there may be newline.
Now, I will produce final answer.
One more nuance: The problem says "remove some of the digits (possibly not remove any digit at all)" so if the original number is divisible by 8, it's valid. But our algorithm might output a shorter subsequence. That's acceptable because we may output any. So okay.
Now, test on a custom case: s = "9"100 (all 9's). No 0/8. Check two-digit: 99%8=3, no. Three-digit: 999%8=7? 8124=992 remainder 7, so not divisible. But we should check all triples? All triples are 999, so no. So output NO. That's correct.
Now test s = "1256". Digits:1,2,5,6. Contains no 0/8. Two-digit: check pairs: 12%8=4,15%8=7,16%8=0? 16%8=0, yes (positions 1 and 4? Actually digits: 1(1),2(2),5(3),6(4). Pair (1,4) gives 16? digit1=1, digit4=6 => 16. So two-digit solution exists => output "16". That's correct.
Now test s = "1258". Contains '8' => output "8". That's okay. Could also output "8" or "0"? No zero. So fine.
Now test s = "1008" -> zero -> "0". Could output "8" as well, but we output "0". Fine.
Now, consider s = "2002". Contains zero => output "0". But 2002 mod8? 8*250=2000, remainder 2, not divisible. So output "0" is valid.
Now, s = "8888888888" -> has 8 -> output "8". Fine.
Now, s = "0" -> output "0".
Now, s = "8" -> output "8".
Now, s = "16" -> output "16". But is "16" divisible by 8? Yes. Output "YES" and "16". Good.
Now, s = "24" -> "24". Good.
Now, s = "32" -> "32". Good.
Now, s = "40" -> output "0". But some might expect "40". Both acceptable.
Thus final.
Now, I'll produce final answer.
But before finishing, I recall a Codeforces problem "C. Division by Eight" where the solution is exactly this. However, there is a nuance: In that problem, the input number can have up to 100 digits, and the solution is to check subsequences of length up to 3. Many solutions use DP to find any subsequence divisible by 8, but the simpler approach works.
One potential pitfall: The problem statement says "the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes". It doesn't say the result must be positive; zero is allowed.
Thus final.
Now, I'll write final solution in Python.
But I'll also consider if the input string may be huge (100 digits) but we use triple loops; that's fine.
Now, let's write the code.
Potential improvement: we can early break when found.
Now, ensure we use sys.stdout.write or print.
Now, test with sample.
I'll code.
We should also consider the case where there is a two-digit number like "00"? Not needed.
Now, final answer.
Note: There is a possibility that the input contains a digit '0' but we output "0", but perhaps the answer expects a number with more than one digit because they want a non-zero number? No, it's fine.
Thus final solution.
Now, writing answer. ```python
import sys
def solve() -> None:
s = sys.stdin.readline().strip()
# If there is a '0', we can always take it
if '0' in s:
print("YES")
print("0")
return
# If there is an '8', take it
if '8' in s:
print("YES")
print("8")
return
n = len(s)
# Check all 2‑digit subsequences
for i in range(n):
for j in range(i + 1, n):
num = (ord(s[i]) - 48) * 10 + (ord(s[j]) - 48)
if num % 8 == 0:
print("YES")
print(s[i] + s[j])
return
# Check all 3‑digit subsequences
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
num = (ord(s[i]) - 48) * 100 + (ord(s[j]) - 48) * 10 + (ord(s[k]) - 48)
if num % 8 == 0:
print("YES")
print(s[i] + s[j] + s[k])
return
# No suitable subsequence found
print("NO")
if name == "main":
solve()