https://www.acmicpc.net/problem/20164
import sys
def solution():
read = sys.stdin.readline
num = read().rstrip()
print(rec(num, 1), rec(num, 0))
def rec(num, is_min):
count = get_count(num)
if len(num) == 1:
return count
elif len(num) == 2:
num = int(num[0]) + int(num[1])
return count + rec(str(num), is_min)
else:
result = float('-inf')
if is_min:
result = float('inf')
for s1 in range(1, len(num)-1):
for s2 in range(s1+1, len(num)):
num1 = int(num[:s1])
num2 = int(num[s1:s2])
num3 = int(num[s2:len(num)])
if is_min:
result = min(result, rec(str(num1 + num2 + num3), is_min))
else:
result = max(result, rec(str(num1 + num2 + num3), is_min))
return count + result
def get_count(num):
count = 0
for i in range(len(num)):
if int(num[i]) % 2 == 1:
count += 1
return count
solution()