python3 <<'PY' import math a=0.25; b=1.0/12 def Aval(y1,y2,q,r): alpha=2*a+2*b-y1-y2; mu1=a+b-y1; nu1=a+b; q2=alpha-q if q<=0 or q2<=0: return -1 lo=max(0.0,q-(alpha-mu1)); hi=min(q,mu1) if rhi+1e-12: return -1 den=r*q2/q-(mu1-r) if abs(den)<1e-14: return -1 c=(a*q2-(mu1-r)*nu1)/den if c<-1e-9 or c>q+1e-9 or c>nu1+1e-9 or nu1-c>q2+1e-9: return -1 r2=mu1-r; c2=nu1-c return (math.sqrt(max(0,a*r*c))+math.sqrt(max(0,b*r*(q-c)))+math.sqrt(max(0,b*(q-r)*c))+math.sqrt(max(0,a*(q-r)*(q-c)))+math.sqrt(max(0,a*r2*c2))+math.sqrt(max(0,b*r2*(q2-c2)))+math.sqrt(max(0,b*(q2-r2)*c2))+math.sqrt(max(0,a*(q2-r2)*(q2-c2)))) def best2(y1,y2,N=40): alpha=2*a+2*b-y1-y2; mu1=a+b-y1; mu2=alpha-mu1 best=-1 for iq in range(1,N): q=alpha*iq/N lo=max(0.0,q-mu2); hi=min(q,mu1) for ir in range(N+1): r=lo+(hi-lo)*ir/N v=Aval(y1,y2,q,r) if v>best: best=v return best def bc(y1,y2): best=-1 for j in range(201): y3=a*j/200; beta=a+b-y3 B=math.sqrt(beta)*(b+math.sqrt(max(0,a*(a-y3)))) gam=y1+y2+y3 C=math.sqrt(gam)*(math.sqrt(max(0,b*y1))+math.sqrt(max(0,a*y2))+math.sqrt(max(0,a*y3))) if B+C>best: best=B+C return best best=-1; arg=None N=30 for i in range(N+1): y1=b*i/N for j in range(N+1): y2=a*j/N A=best2(y1,y2,40) tot=A+bc(y1,y2) if tot>best: best=tot; arg=(y1,y2,A,tot) print("best",best,arg,"cost",1-best) PY