A = tuple([int(s) for s in input()]) from functools import lru_cache @lru_cache(maxsize=1000000) def f(X): N = len(X) if N < 3: return 0 if N % 3 == 1: ret = 0 for i in range(N): Y = list(X) Y.pop(i) ret = max(ret, f(tuple(Y))) return ret if N % 3 == 2: ret = 0 for i in range(N - 1): for j in range(i + 1, N): Y = list(X) Y.pop(j) Y.pop(i) ret = max(ret, f(tuple(Y))) return ret ret = 0 Y1 = list(X) Y1.pop(0) for i in range(1, N - 1): Y2 = Y1[:] Y2.pop(i - 1) for j in range(i + 1, N): Y3 = Y2[:] Y3.pop(j - 2) tmp = 0 if X[0] != 0 and X[0] != X[i] and X[i] == X[j]: tmp += X[0] * 100 + X[i] * 10 + X[i] tmp += f(tuple(Y3)) ret = max(ret, tmp) return ret print(f(A))