using System; using System.Linq; using System.Collections.Generic; public class Program { public void Proc() { Reader.IsDebug = false; long ans = GetAns(long.Parse(Reader.ReadLine())); Console.WriteLine(ans); } private Dictionary dic = new Dictionary(); private long GetAns(long num) { if(dic.ContainsKey(num)) { return dic[num]; } long ans = 0; string numstr = num.ToString(); if(numstr.Length < 3) { return 0; } for(int i=0; inumstr.Skip(i+1).Count(b=>b==a)>1 && a != numstr[i]).ToArray(); numList.ToList().ForEach(a=>{ long newNum = long.Parse(numstr[i].ToString() + a + a); List tmp = numstr.ToList(); tmp.RemoveAt(numstr.IndexOf(a, i)); tmp.RemoveAt(numstr.IndexOf(a, i)); tmp.RemoveAt(i); long ret = 0; if(tmp.Count > 0) { ret = this.GetAns(long.Parse(string.Join(string.Empty, tmp))); } ans = Math.Max(ans, ret + newNum); }); } dic[num] = ans; return ans; } public class Reader { public static bool IsDebug = true; private static System.IO.StringReader SReader; private static string InitText = @" 2718281828 "; public static string ReadLine() { if(IsDebug) { if(SReader == null) { SReader = new System.IO.StringReader(InitText.Trim()); } return SReader.ReadLine(); } else { return Console.ReadLine(); } } } public static void Main(string[] args) { Program prg = new Program(); prg.Proc(); } }