python3 - <<'PY' from collections import defaultdict def decomp(a,b,c): if a<=0 or b<=0 or c<=0: return [] if a==1 and b==1 and c==1: return [(0,1)] D2=[] for eps in (0,1): a2=(a-eps+1)//2 if a2<=0: continue for eta in (0,1): b2=(b-eta+1)//2 if b2<=0: continue for th in (0,1): c2=(c-th+1)//2 if c2<=0: continue sub=decomp(a2,b2,c2) sh=eps+eta+th for q,L in sub: D2.append((sh+2*q,L)) C=defaultdict(int) for s,L in D2: C[(s,L)]+=1 starts=sorted(set(s for s,L in D2)) out=[] for s in starts: lens=sorted([L for (ss,L) in C if ss==s and C[(ss,L)]>0], reverse=True) for L in lens: cnt=C[(s,L)] for _ in range(cnt): if C[(s+1,L)]>0: C[(s+1,L)]-=1 out.append((s,2*L)) elif C[(s+1,L-1)]>0: C[(s+1,L-1)]-=1 out.append((s,2*L-1)) else: print('FAIL',s,L,D2) return None for L in set(l for (ss,l) in C if ss==s): if C[(s,L)]!=0: print('LEFTOVER',s,L,C[(s,L)]) return out res=decomp(5,6,9) print('blocks',len(res),res) n=[] W=[] for r in range(1,19): nr=0 wr=0 for s,L in res: if L==r: nr+=1 wr+=(s+1)**4 n.append(nr) W.append(wr) print('n=',n) print('W=',W) PY