(declaim (inline println)) (defun println (obj &optional (stream *standard-output*)) (let ((*read-default-float-format* 'double-float)) (prog1 obj (princ obj stream) (terpri stream)))) (defun ok (xs) (sb-int:named-let rec ((xs xs) (c #\a)) (or (null xs) (and (char<= c (first xs)) (rec (rest xs) (first xs)))))) (defun get-l-and-r (xs) (when (ok xs) (list (length xs) (first (sort (copy-seq xs) #'char<)) (first (sort (copy-seq xs) #'char>))))) (define-modify-macro maxf (var) max) (defun char->int (c) (- (char-code c) (char-code #\a))) (defun main () (let* ((n (read)) (ss (sort (remove-if #'null (mapcar #'get-l-and-r (loop repeat n collect (concatenate 'list (read-line))))) #'char< :key #'second))) (let ((dp (make-array 26 :initial-element 0))) (loop for (cnt l r) in ss do (maxf (aref dp (char->int r)) (+ (aref dp (char->int l)) cnt))) (println (reduce #'max dp))))) #-swank (main)