This question evaluates parsing and string-processing skills, proficiency with data structures for aggregating element counts, and understanding of algorithmic time complexity.

Given a string representing a chemical formula, parse it and return the total count of each element. The formula may include element symbols (uppercase letter followed by lowercase letters), integers for counts, nested parentheses with multipliers, and multi-digit numbers. Return a canonical output by concatenating element names in lexicographic order followed by their counts (omit the count when it equals 1). For example, input "K4(ON(SO 3)2)3" should output "K4N3O14S6". Implement an algorithm that runs in O(n) time using a stack or equivalent and handles inputs up to length 1e5.