Problem:
Huffman Coding

We want to encode a given string $S$ to a binary string. Each alphabet character in $S$ should be mapped to a different variable-length code and the code must not be a prefix of others.

Huffman coding is known as one of ways to obtain a code table for such encoding.

For example, we consider that appearance frequencycies of each alphabet character in $S$ are as follows:

a

b

c

d

e

f

freq.

45

13

12

16

9

5

code

0

101

100

111

1101

1100

We can obtain "Huffman codes" (the third row of the table) using Huffman's algorithm.

The algorithm finds a full binary tree called "Huffman tree" as shown in the following figure.

To obtain a Huffman tree, for each alphabet character, we first prepare a node which has no parents and a frequency (weight) of the alphabet character. Then, we repeat the following steps:

Choose two nodes, $x$ and $y$, which has no parents and the smallest weights.

Create a new node $z$ whose weight is the sum of $x$'s and $y$'s.

Add edge linking $z$ to $x$ with label $0$ and to $y$ with label $1$. Then $z$ become their parent.

If there is only one node without a parent, which is the root, finish the algorithm. Otherwise, go to step 1.

Finally, we can find a "Huffman code" in a path from the root to leaves.

Task

Given a string $S$, output the length of a binary string obtained by using Huffman coding for $S$.

Input

$S$

A string $S$ is given in a line.

Output

Print the length of a binary string in a line.

Constraints

$1 \le |S| \le 10^5$

$S$ consists of lowercase English letters.

Sample Input 1

abca

Sample Output 1

6

Sample Input 2

aaabbcccdeeeffg

Sample Output 2

41

Sample Input 3

z

Sample Output 3

1

Solution in Ruby (judge verdict: Accepted)

#!/usr/bin/ruby
OutputMode=:AIZU #:NEWICK(CODEIQ debug),:CODEEVAL,:AIZU,:CODEIQ

CODES=OutputMode==:CODEEVAL||OutputMode==:AIZU ? ['0','1'] : ['r','g','b']
WIDTH=CODES.size
def dfs(str,code) #assign code
	r=[]
	c=i=0
	while str[i,1]!=')'
		if str[i,1]=='('
			r+=dfs(str[i+1..-1],code+CODES[c])
			x=1
			while x>0
				x+=1 if str[i+1,1]=='('
				x-=1 if str[i+1,1]==')'
				i+=1
			end
		else
			r+=[str[i,1]+(OutputMode==:CODEEVAL||OutputMode==:AIZU ? ': ' : ':')+code+CODES[c]]
		end
		c+=1
		i+=1
	end
	return r
end

def huffman(h)
	#build huffman tree
	a=h.map{|k,v|[v,k]}.sort
	if a.size==1
		a=[[1,'('+a[0][1]+')']]
	else
		if (h.size-WIDTH)%(WIDTH-1)>0
			b=a.shift(WIDTH-(h.size-WIDTH)%(WIDTH-1)).transpose
			a=(a+[[b[0].reduce(:+),'('+b[1]*(OutputMode==:NEWICK ? ',' : '')+')']]).sort
		end
		while a.size>1
			b=a.shift(WIDTH).transpose
			raise if b[0].size!=WIDTH
			a=(a+[[b[0].reduce(:+),'('+b[1]*(OutputMode==:NEWICK ? ',' : '')+')']]).sort
		end
	end
	# a => [[9371, "((O(((Q(XZ)J)VB)YW)I)(AT(U(PFK)L))(E(DRS)(N(GMC)H)))"]]
	if OutputMode==:NEWICK
		# ((O,(((Q,(X,Z),J),V,B),Y,W),I),(A,T,(U,(P,F,K),L)),(E,(D,R,S),(N,(G,M,C),H)));
		puts a[0][1]+';'
		exit
	end
	r=dfs(a[0][1][1..-1],'')
	#r.sort_by{|e|e.split(':')[1].size}.each{|e|
	if OutputMode==:CODEEVAL
		puts r.sort*'; '+';'
	elsif OutputMode==:AIZU
		p r.map{|e|x,y=e.split(': ');h[x]*y.size}.reduce(:+)
	else
		puts r.sort
	end
end

if OutputMode==:CODEEVAL||OutputMode==:AIZU
	$<.each{|e|
		h=Hash.new(0)
		e.chomp.chars{|c|h[c]+=1}
		huffman(h)
	}
elsif OutputMode==:NEWICK||OutputMode==:CODEIQ
	h={}
	DATA.each{|e|
		s,n=e.split
		h[s]=n.to_i
	}
	huffman(h)
end