# subset_sum 
# Fig. 49.8  p.187

import math
from grays import *

N = 16  # non-squares

nis = [0]*N  # non-integer squares
print("nis[] entries")
for j in range(N): 
  nis[j] = math.sqrt(j+1 + round( math.sqrt(j+1)))
  # print(f"{j:2d} : {nis[j]:.3f}")

grayGen = grayTransY(N)

goal = (10+sum(nis))/2
minW = 1
wghts = 0
i = 0
while i < N-1: # reduce comparisons
  i = next(grayGen)
  wghts += nis[i]
  nis[i] = -nis[i] # -ve means used in weights
  rd = round(goal - wghts)  
  nWeight = abs((goal - wghts) - rd)
  if nWeight <= minW:
    if (rd >= 0) and (rd <= 10): # 0 to 10
      for j in range(N):
        print(int(nis[j] < 0),end ='') 
        # 0 (not used) or 1 (used)
      minW = nWeight
      print(f"; min == {minW:.11f}")
