# Python program for OEIS A374144
# Michael S. Branicky, Jun 29 2024

# A374144 a(n) is the smallest number which can be represented as the sum of two distinct nonzero n-gonal numbers in exactly n ways, or -1 if no such number exists.		0
data = [81, 1105, 205427, 483031, 9402323, 6232341]

# (Python)
import heapq
from itertools import islice

def f(n, i): # ith n-gonal number
    return i*((n-2)*i - (n-4))//2

def a(n): # generator of terms
    f1, f2 = f(n, 1), f(n, 2)
    p = v = f1+f2
    h, nextcount = [(v, 1, 2)], 3
    oldv = ways = highways = 0
    while True:
        (v, s, l) = heapq.heappop(h)
        if v == oldv: ways += 1
        else:
            if ways == n:
                return oldv 
            ways = 1
        if v >= p:
            p = f1 + f(n, nextcount)
            heapq.heappush(h, (p, 1, nextcount))
            nextcount += 1
        oldv = v
        s += 1; l += 1
        heapq.heappush(h, (f(n, s)+f(n, l), s, l))

from time import time
time0 = time()

alst = []
for n in range(3, 10001):
    an = a(n)
    alst.append(an)
    print(n, an, len(str(alst))-2, time()-time0)
    print("   ", alst)
    print("   ", data)