import times, strutils, sequtils, math, algorithm, tables, sets, lists, intsets import critbits, future, strformat, deques template `max=`(x,y) = x = max(x,y) template `min=`(x,y) = x = min(x,y) template `mod=`(x,y) = x = x mod y template scan2 = (scan(), scan()) template scan3 = (scan(), scan()) let read* = iterator: string {.closure.} = while true: (for s in stdin.readLine.split: yield s) proc scan(): int = read().parseInt proc scanf(): float = read().parseFloat proc toInt(c:char): int = return int(c) - int('0') proc solveOld():int= var n = scan() x = newseqwith(n,newseqwith(n,1==scan())) a = newseqwith(n,scan()) result = int.high for i in 0..<2^n: #echo i.toBin(n) var sorry = 0 tx = x for j in 0.. 0: sorry += a[j] for k in 0.. 0: sorry += a[j] for k in 0.. 0: tx[k] -= (1 shl (n-j-1)) #echo i.toBin(n)," sorry:",sorry #echo "===" #echo tx.mapIt(it.toBin(n)).join("\n") #echo "=>" var update = true while update: update = false for j in 0.. 0: update=true tx[k] -= (1 shl (n-j-1)) #echo tx.mapIt(it.toBin(n)).join("\n") #echo "" if tx.allIt(it==0): result.min=sorry echo solve()