import Data.Array

propagate::Array Int Int->Int->Array Int Int
propagate ar item_v = let el = elems ar in
                      let nexter = (\i-> (maximum.(\x->0:x).(filter (\x-> (mod x 10) == i))) ([e+item_v|e<-el]++el) ) in
                      listArray (0,9) [nexter i |i<-[0..9]]

solver::[Int]->Int
solver xs = let ini = listArray (0,9) (take 10 (repeat 0)) in
            let dat= foldl propagate ini xs in
            maximum [dat!i|i<-[1..9]]

n_get::Int -> IO [Int]
n_get 0 =return []
n_get n = do
  dc<-getLine
  rest<-n_get (n-1)
  return ((read dc):rest)

main::IO()
main=do
  nc<-getLine
  let n=read nc ::Int
  dat<-n_get n
  print (solver dat)