package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
)

var rdr = bufio.NewReaderSize(os.Stdin, 100000)

func readLine() string {
	buf := make([]byte, 0, 100000)
	for {
		l, p, e := rdr.ReadLine()
		if e != nil {
			panic(e)
		}
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}

// An Item is something we manage in a priority queue.
type Item struct {
	value    rune // The value of the item; arbitrary.
	priority int  // The priority of the item in the queue.
	// The index is needed by update and is maintained by the heap.Interface methods.
	index int // The index of the item in the heap.
	left  *Item
	right *Item
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq[i].priority < pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

// update modifies the priority and value of an Item in the queue.
func (pq *PriorityQueue) update(item *Item, value rune, priority int) {
	item.value = value
	item.priority = priority
	heap.Fix(pq, item.index)
}

func counter(str string) map[rune]int {
	m := map[rune]int{}
	for _, c := range str {
		if _, ok := m[c]; ok {
			m[c] = m[c] + 1
		} else {
			m[c] = 1
		}
	}
	return m
}

func makeQueue(items map[rune]int) PriorityQueue {
	pq := make(PriorityQueue, len(items))
	i := 0
	for value, priority := range items {
		pq[i] = &Item{
			value:    value,
			priority: priority,
			index:    i,
			left:     nil,
			right:    nil,
		}
		i++
	}
	return pq

}

func (item *Item) toString() string {
	return "{" + string(item.value) + ", " + fmt.Sprint(item.priority) + "}"
}

func walk(item *Item, code string, m map[rune]string) {
	if item.left != nil {
		walk(item.left, code+"0", m)
	}
	if item.right != nil {
		walk(item.right, code+"1", m)
	}
	if item.value != '-' {
		m[item.value] = code
	}
	return
}

func huffman(pq PriorityQueue) *Item {
	heap.Init(&pq)

	for pq.Len() > 1 {
		item := &Item{
			value:    '-',
			priority: 0,
			index:    -1,
			left:     nil,
			right:    nil,
		}
		left := heap.Pop(&pq).(*Item)
		right := heap.Pop(&pq).(*Item)
		item.left = left
		item.right = right
		item.priority = right.priority + left.priority
		pq.Push(item)
	}
	return heap.Pop(&pq).(*Item)
}

func anser(item *Item) {

}

// This example creates a PriorityQueue with some items, adds and manipulates an item,
// and then removes the items in priority order.
func main() {
	str := readLine()
	// Some items and their priorities.
	items := counter(str)
	pq := makeQueue(items)
	item := huffman(pq)

	codeMap := map[rune]string{}
	walk(item, "", codeMap)

	sum := 0
	for k, v := range codeMap {
		sum += items[k] * len(v)
	}
	if len(items) != 1 {
		fmt.Println(sum)
	} else {
		fmt.Println(len(str))
	}
	// Create a priority queue, put the items in it, and
	// establish the priority queue (heap) invariants.
}



Measured memory_bytes: 1444