mkList :: Int -> [Int] -> [[Int]]
mkList n x
  | null x = [[]]
  | otherwise = take n x : mkList n (drop n x)

main = getContents >>= mapM_ print . reverse . tail . init . map sum . (\(_:y:xs) -> mkList y (zipWith (*) (cycle $ take y $ reverse xs) (reverse xs))) . map read . words