using System.Linq; using System.Collections.Generic; using System; public static class combi { public static IEnumerable> Comb(this IEnumerable items, int r) { if (r == 0) { yield return Enumerable.Empty(); } else { var i = 1; foreach (var x in items) { var xs = items.Skip(i); foreach (var c in Comb(xs, r - 1)) yield return c.Before(x); i++; } } } public static IEnumerable Before(this IEnumerable items, T first) { yield return first; foreach (var i in items) yield return i; } } public class P { public int score { get; set; } public int[] a { get; set; } } public class Hello { public static void Main() { var s = Console.ReadLine().Trim(); var sL = s.Length; if (sL <= 2) { Console.WriteLine(0); goto end; } var a = new int[sL]; var w = new P { a = new int[sL] }; for (int i = 0; i < sL; i++) w.a[i] = int.Parse(s[i].ToString()); var ans = new HashSet(); rec(w, ans); if (ans.Count() == 0) Console.WriteLine(0); else Console.WriteLine(ans.Max()); end:; } public static void rec(P p, HashSet ans) { var aL = p.a.Length; if (aL <= 2 && p.score != 0) { ans.Add(p.score); return; } ans.Add(p.score); foreach (var x in Enumerable.Range(0, aL).Comb(3)) { var w = x.ToArray(); var c = checkCWW(p.a, w); if (c > 0) { ans.Add(c); if (aL - 3 >= 1) { var w2 = new P { a = new int[aL - 3], score = p.score + c }; var pt = 0; for (int i = 0; i < aL; i++) if (!w.Contains(i)) w2.a[pt++] = p.a[i]; rec(w2, ans); } else ans.Add(p.score + c); } } } public static int checkCWW(int[] a, int[] b) { if (a[b[0]] == 0) return -1; if (a[b[0]] == a[b[1]]) return -1; if (a[b[1]] == a[b[2]]) return 100 * a[b[0]] + 10 * a[b[1]] + a[b[2]]; return -1; } }