A foundry stamps each cast part with a numeric serial code. A digit-compacting stamp can be applied to any part any number of times (including zero); each application replaces that part's current serial code with the sum of its decimal digits. After using the stamp as many times as desired on as many parts as desired, determine the smallest serial code that can appear anywhere in the batch.
Line 1: an integer n — the number of parts in the batch.
Line 2: n space-separated integers — the original serial code of each part.
A single integer: the smallest serial code achievable anywhere in the batch.
Example 1
Input
4 15 22 100 7
Expected
1
Explanation
Compacting 15 gives 1+5=6, compacting 22 gives 2+2=4, compacting 100 gives 1+0+0=1 (already single-digit, stamping stops), and 7 is already single-digit. The smallest value achievable across the batch is 1, from the part originally coded 100.
Example 2
Input
1 38
Expected
2
Explanation
Compacting 38 gives 3+8=11, which is still two digits, so compacting again gives 1+1=2. Since 2 is a single digit the stamp has no further effect, so the smallest achievable code is 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →