import qualified Data.IntMap.Strict as M import Data.List import Data.Bits mapKeysValue f g = M.fromDistinctAscList . map (\(k,v) -> (f k, g v)) . M.toList main = do n <- readLn let takeFellow = reverse . takeWhile (/=(-1)) . reverse fs <- concat . map (takeFellow . map read . words) . lines <$> getContents print (idolUnit n fs) idolUnit n fs = (snd . M.findMax) (foldl' dp (M.singleton 0 0) units) where units = zip [(bit i, bit j) | i<-[0..n-1], j<-[0..n-1], i mod k bi == mod (complement 0) bi) $ M.unionWith max im $ mapKeysValue (.|. ab) (+f) im' where ab = bi .|. bj im' = M.filterWithKey (\k _ -> k .&. ab == 0) im