結果

問題 No.527 ナップサック容量問題
コンテスト
ユーザー Common Lisp
提出日時 2024-11-14 21:05:08
言語 Common Lisp
(sbcl 2.6.3)
コンパイル:
sbclc _filename_
実行:
sbcl --script Main.fasl
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 981 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,030 ms
コンパイル使用メモリ 32,000 KB
実行使用メモリ 22,528 KB
最終ジャッジ日時 2026-05-10 05:24:59
合計ジャッジ時間 3,241 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます
コンパイルメッセージ
; compiling file "/home/judge/data/code/Main.lisp" (written 10 MAY 2026 05:24:47 AM):

; wrote /home/judge/data/code/Main.fasl
; compilation finished in 0:00:00.029

ソースコード

diff #
raw source code

(defun main (&rest argv)
  (declare (ignorable argv))
  (let* ((n (read))
         (v (make-array n :element-type 'integer))
         (w (make-array n :element-type 'integer))
         (s 0)
         (dp (make-array 101010 :element-type 'integer :initial-element 0)))
    (dotimes (i n)
      (let* ((vi (read))
             (wi (read)))
        (setf (aref v i) vi
              (aref w i) wi
              s (+ s wi))))
    (let* ((v-max (read)))
      (dotimes (i n)
        (loop for j from (1+ s) downto (aref w i) do
              (setf (aref dp j) (max (aref dp j) (+ (aref dp (- j (aref w i))) (aref v i))))))
      (dotimes (i s)
        (when (= v-max (aref dp (1+ i)))
              (format t "~d~%" (1+ i))
              (return)))
      (if (= v-max (aref dp (1+ s)))
          (format t "inf~%")
          (loop for i from s downto 1 do
                (when (= v-max (aref dp i))
                      (format t "~d~%" i)
                      (return)))))))

(main)
0