#K66327. List Compression
List Compression
List Compression
You are given a list of integers. Your task is to compress the list by grouping consecutive sequences of the same integer. For each group of consecutive identical integers, calculate the sum of the group. If the group contains more than one element, output the number followed by the group sum enclosed in parentheses, i.e., n(sum). If the integer appears on its own (i.e. the group length is one), leave it unchanged.
For example, if the input is [1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4]
, then the output should be [1(2), 2(6), 3(12), 4(8)]
.
The solution should read input from stdin and output the result on stdout. All formulas, if any, must be in LaTeX format.
inputFormat
The first line contains an integer \(n\) representing the number of integers in the list. The second line contains \(n\) space-separated integers.
outputFormat
Output the compressed list as a sequence of elements separated by a space. For each group of consecutive numbers that appear more than once, output the element in the format n(sum), where \(n\) is the integer and \(sum\) is the sum of that group. For individual numbers, simply output the number.
## sample11
1 1 2 2 2 3 3 3 3 4 4
1(2) 2(6) 3(12) 4(8)