結果

問題 No.527 ナップサック容量問題
コンテスト
ユーザー Common Lisp
提出日時 2024-11-14 21:05:08
言語 Common Lisp
(sbcl 2.6.9)
コンパイル:
sbclc _filename_
実行:
sbcl --script Main.fasl
結果
AC  
実行時間 61 ms / 2,000 ms
+ 451µs
コード長 981 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 945 ms
コンパイル使用メモリ 38,500 KB
実行使用メモリ 20,480 KB
最終ジャッジ日時 2026-10-14 00:59:51
合計ジャッジ時間 4,103 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます
コンパイルメッセージ
; compiling file "/home/judge/data/code/Main.lisp" (written 14 OCT 2026 12:59:46 AM):

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

ソースコード

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