Working kernel version of min cost table

Only works for width < 1024