root = {}
root['a'] = {'b':2, 'c':2}
root['b'] = {'goal': 3}
root['c'] = {'d':1}
root['d'] = {'goal':1}
# a - b -goal
# 0 2 3 sum 5
# a - c - d goal
# 0 2 1 1 sum 4
for k1, v1 in root['a'].items():
l1 = 0 + v1
for k2, v2 in root[k1].items():
l2 = l1 + v2
if 'goal' == k2:
print("{} - {} - {} ({})".format('a', k1,k2, l2))
break
for k3, v3 in root[k2].items():
l3 = l2 + v3
print("{} - {} - {} - {} ({})".format('a', k1,k2,k3, l3))
cm9vdCA9IHt9CnJvb3RbJ2EnXSA9IHsnYic6MiwgJ2MnOjJ9CnJvb3RbJ2InXSA9IHsnZ29hbCc6IDN9CnJvb3RbJ2MnXSA9IHsnZCc6MX0Kcm9vdFsnZCddID0geydnb2FsJzoxfQoKCiMgYSAtIGIgIC1nb2FsCiMgMCAyIDMgc3VtIDUKIyBhIC0gYyAtIGQgZ29hbAojIDAgMiAxIDEgc3VtIDQKCmZvciBrMSwgdjEgaW4gcm9vdFsnYSddLml0ZW1zKCk6CiAgICBsMSA9IDAgKyB2MQogICAgZm9yIGsyLCB2MiBpbiByb290W2sxXS5pdGVtcygpOgogICAgICAgIGwyID0gbDEgKyB2MgogICAgICAgIGlmICdnb2FsJyA9PSBrMjoKICAgICAgICAgICAgcHJpbnQoInt9IC0ge30gLSB7fSAoe30pIi5mb3JtYXQoJ2EnLCBrMSxrMiwgbDIpKQogICAgICAgICAgICBicmVhawogICAgICAgIGZvciBrMywgdjMgaW4gcm9vdFtrMl0uaXRlbXMoKToKICAgICAgICAgICAgbDMgPSBsMiArIHYzCiAgICAgICAgICAgIHByaW50KCJ7fSAtIHt9IC0ge30gLSB7fSAoe30pIi5mb3JtYXQoJ2EnLCBrMSxrMixrMywgbDMpKQo=