using System;
using System.Collections.Generic;
using System.Linq;



namespace CSharpSample01
{

    class HNode
    {
        public char ch;
        public int num;
        public bool is_leaf;
        public HNode par;
        public HNode left;
        public HNode right;
        public HNode(char c, int n, bool leaf)
        {
            ch = c;
            num = n;
            is_leaf = leaf;
            par = null;
            left = null;
            right = null;
        }
    }

    class Huffman
    {
        private List<HNode> sorted_node_list;
        readonly string S;
        Dictionary<char, string> code_dic;
        HNode root;

        public Huffman(string s)
        {
            S = s;           
            Initiate_sorted_node_list();
            CreateHuffman();
            Coding();
        }

        //node_listの初期化
        private void Initiate_sorted_node_list()
        {
            Dictionary<char, int> freq = new Dictionary<char, int>();
            sorted_node_list = new List<HNode>();

            foreach (var c in S)
            {
                if (freq.ContainsKey(c)) freq[c]++;
                else freq[c] = 1;
            }
            foreach(var s in freq.OrderBy(x => x.Value))
            {
                sorted_node_list.Add(new HNode(s.Key, s.Value, true));
            }
        }

        //node_listからハフマン木を作る
        private void CreateHuffman()
        {
            //foreach (var s in sorted_node_list) Console.Write($"{s.ch}:{s.num} ");
            //Console.WriteLine();
            HNode first = sorted_node_list.First();
            HNode n = new HNode('?', first.num, false);
            n.left = first;
            sorted_node_list.Remove(sorted_node_list.First());

            if(sorted_node_list.Count >= 1)
            {
                HNode r = sorted_node_list.First();
                n.right = r;
                n.num += r.num;
                sorted_node_list.Remove(sorted_node_list.First());
            }

            sorted_node_list.Add(n);
       
            while (sorted_node_list.Count >= 2)
            {
                sorted_node_list = sorted_node_list.OrderBy(x => x.num).ToList();

                //foreach (var s in sorted_node_list) Console.Write($"{s.ch}:{s.num} ");
                //Console.WriteLine();

                HNode left = sorted_node_list.First();
                sorted_node_list.Remove(sorted_node_list.First());
                HNode right = sorted_node_list.First();
                sorted_node_list.Remove(sorted_node_list.First());

                HNode joint = new HNode('?', left.num + right.num, false);
                joint.left = left;
                joint.right = right;

                sorted_node_list.Add(joint);
            }

            root = sorted_node_list.First();
            sorted_node_list.Remove(sorted_node_list.First());
        }

        //完成したハフマン木から文字列S内の文字を符号化
        //左を0右を1とする
        private void Coding()
        {
            code_dic = new Dictionary<char, string>();
            Preorder(root, "");
        }

        //行きがけ順で符号化
        private void Preorder(HNode cur, string code)
        {
            if (cur == null) return;

            if(cur.right == null && cur.left == null)
            {
                //Console.WriteLine($"{cur.ch}:{code}");
                code_dic[cur.ch] = code;
                return;
            }

            Preorder(cur.left, code + "0");
            Preorder(cur.right, code + "1");
        }
        
        //引数chの符号を返す
        public string GetCode(char ch)
        {
            if (!code_dic.ContainsKey(ch)) return "";
            return code_dic[ch];
        }

    }

    
    class Program
    {
        
        static void Main(string[] args)
        {
            string S = Console.ReadLine();

            Huffman h = new Huffman(S);
            
            long cnt = 0;
            foreach(var c in S)
            {
                //Console.WriteLine($"{c}:{h.GetCode(c)}");
                cnt += (long)h.GetCode(c).Length;
            }

            Console.WriteLine(cnt);
            
        }
    }

}


Measured memory_bytes: 18480