import Data.List
import Data.Maybe
import qualified Data.IntMap.Strict as M
import qualified Data.ByteString.Char8 as B
 
readInt = fst . fromJust . B.readInt

apply n f x = foldr ($) x (replicate n f)
 
main = do
 n <- readLn
 as <- map readInt . B.words <$> B.getLine
 let (as1,as2) = splitAt n as
 let q1 = M.fromListWith (+) (zip as1 (repeat 1))
 let q2 = M.fromListWith (+) (zip as2 (repeat 1))
 print (n3 n q1 as2 q2)
 
n3 n q1 as2 q2 = maximum (fsearch n q1 as2 q2)
 
fsearch n q1 as q2 = do
 k <- [0..n]
 let (rh,rl) = transfer (n-k) k q1 as q2
 return (rh - rl)

pred' v = if v > 1 then Just (pred v) else Nothing

transfer n' 0 q1 _ q2 = (sumq q1, sumq (apply n' (M.updateMax pred') q2))
 where sumq = sum . map (uncurry (*)) . M.toList
 
transfer n' k q1 (a:as) q2 = transfer n' (k-1) (M.insertWith (+) a 1 q1') as q2'
 where
  q1' = M.updateMin pred' q1
  q2' = M.update pred' a q2
