A research vault stores a collection of n sealed capsules, each stamped with a positive integer energy code. Every capsule belongs to a charge tier equal to the largest digit that appears anywhere in its code (for example, the code 1084 contains digits 1, 0, 8, 4, so its tier is 8). Two capsules can be fused together only if they belong to the same charge tier, and fusing two capsules releases combined energy equal to the sum of their two codes. Determine the maximum combined energy obtainable by fusing any single pair of capsules that share a tier. If no two capsules in the vault share a tier, fusion is impossible.
n -- the number of capsules.n space-separated positive integers -- the energy code of each capsule.Print a single integer: the maximum combined energy achievable by fusing two capsules that share a charge tier, or -1 if no such pair exists.
Example 1
Input
5 112 25 45 51 76
Expected
96
Explanation
Charge tiers: 112 -> tier 2, 25 -> tier 5, 45 -> tier 5, 51 -> tier 5, 76 -> tier 7. Tier 5 has three members (25, 45, 51); the two largest are 51 and 45, giving 96. No other tier has two members, so the answer is 96.
Example 2
Input
4 1 2 3 4
Expected
-1
Explanation
Each single-digit code is its own tier (1, 2, 3, 4 respectively), so every tier has exactly one member. Since no tier has two or more capsules, no fusion is possible and the answer is -1.
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 →