N=10**7 res=0 a=[] s=290797 while N>=0: a.append((s%64)+1) s=(s*s)%50515093 N-=1 while len(a)>=3 and a[-2] <= a[-3] and a[-2] <= a[-1]: res+=a[-2]*a[-2] a.append(a.pop()-a.pop()+a.pop()) print res+sum(a[i]*a[i] for i in range(len(a)-1,-1,-2))