(defconstant +inf+ 987654321987654321) (defun main (&rest argv) (declare (ignorable argv)) (let* ((n (read)) (m (make-array n :element-type 'integer)) (2^n (ash 1 n)) (dp1 (make-array 2^n :initial-element +inf+)) (dp2 (make-array 2^n :initial-element -1))) (setf (aref dp1 0) 0) (setf (aref dp2 0) 0) (dotimes (i n) (setf (aref m i) (read))) (dotimes (i 2^n) (dotimes (j n) (when (zerop (logand i (ash 1 j))) (let ((k (logior i (ash 1 j)))) (when (= -1 (aref dp2 k)) (setf (aref dp2 k) (+ (aref dp2 i) (aref m j)))) (setf (aref dp1 k) (min (aref dp1 k) (+ (aref dp1 i) (max 0 (- (aref m j) (mod (aref dp2 i) 1000)))))))))) (format t "~d~%" (aref dp1 (1- 2^n))))) (main)