Submission #9651057


Source Code Expand

;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           ;; enclose the form with VALUES to avoid being captured by LOOP macro
           #\# #\> (lambda (s c p) (declare (ignore c p)) `(values ,(read s nil nil t)))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS
(macrolet ((def (name fname)
             `(define-modify-macro ,name (new-value) ,fname)))
  (def minf min)
  (def maxf max)
  (def mulf *)
  (def divf /)
  (def iorf logior)
  (def xorf logxor)
  (def andf logand))

(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  "NOTE: cannot read -2^62"
  (declare #.OPT)
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setf minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48)
                              (* 10 (the (integer 0 #.(floor most-positive-fixnum 10))
                                         result))))
              (return (if minus (- result) result))))))))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defconstant +inf+ 10001)

(defun main ()
  (declare #.OPT
           (inline sort))
  (let* ((n (read))
         (ws (make-array n :element-type 'uint31))
         (ss (make-array n :element-type 'uint31))
         (vs (make-array n :element-type 'uint31))
         (ords (make-array n :element-type 'uint31))
         (dp (make-array (+ +inf+ 1) :element-type 'fixnum :initial-element -1)))
    (dotimes (i n)
      (setf (aref ws i) (read-fixnum)
            (aref ss i) (read-fixnum)
            (aref vs i) (read-fixnum)
            (aref ords i) i))
    (setq ords (sort ords #'> :key (lambda (i) (+ (aref ws i) (aref ss i)))))
    (setf (aref dp +inf+) 0)
    (dotimes (x n)
      (let* ((ord (aref ords x))
             (w (aref ws ord))
             (s (aref ss ord))
             (v (aref vs ord)))
        (dotimes (y +inf+)
          (when (and (/= (aref dp y) -1)
                     (>= y w))
            (maxf (aref dp (min (- y w) s))
                  (+ (aref dp y) v))))
        (maxf (aref dp s)
              (+ (aref dp +inf+) v))))
    (println
     (loop for y to +inf+
           maximize (aref dp y)))))

#-swank (main)

;;;
;;; Test and benchmark
;;;

#+swank
(defun io-equal (in-string out-string &key (function #'main) (test #'equal))
  "Passes IN-STRING to *STANDARD-INPUT*, executes FUNCTION, and returns true if
the string output to *STANDARD-OUTPUT* is equal to OUT-STRING."
  (labels ((ensure-last-lf (s)
             (if (eql (uiop:last-char s) #\Linefeed)
                 s
                 (uiop:strcat s uiop:+lf+))))
    (funcall test
             (ensure-last-lf out-string)
             (with-output-to-string (out)
               (let ((*standard-output* out))
                 (with-input-from-string (*standard-input* (ensure-last-lf in-string))
                   (funcall function)))))))

#+swank
(defun get-clipbrd ()
  (with-output-to-string (out)
    ;; (run-program "C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe" '("get-clipboard") :output out)
    (run-program "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)))

#+swank (defparameter *this-pathname* (uiop:current-lisp-file-pathname))
#+swank (defparameter *dat-pathname* (uiop:merge-pathnames* "test.dat" *this-pathname*))

#+swank
(defun run (&optional thing (out *standard-output*))
  "THING := null | string | symbol | pathname

null: run #'MAIN using the text on clipboard as input.
string: run #'MAIN using the string as input.
symbol: alias of FIVEAM:RUN!.
pathname: run #'MAIN using the text file as input."
  (let ((*standard-output* out))
    (etypecase thing
      (null
       (with-input-from-string (*standard-input* (delete #\Return (get-clipbrd)))
         (main)))
      (string
       (with-input-from-string (*standard-input* (delete #\Return thing))
         (main)))
      (symbol (5am:run! thing))
      (pathname
       (with-open-file (*standard-input* thing)
         (main))))))

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "")))

#+swank
(defun bench (&optional (out (make-broadcast-stream)))
  (time (run *dat-pathname* out)))

;; To run: (5am:run! :sample)
#+swank
(it.bese.fiveam:test :sample
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "3
2 2 20
2 1 30
3 1 40
"
    "50
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "4
1 2 10
3 1 10
2 4 10
1 6 10
"
    "40
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "5
1 10000 1000000000
1 10000 1000000000
1 10000 1000000000
1 10000 1000000000
1 10000 1000000000
"
    "5000000000
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "8
9 5 7
6 2 7
5 7 3
7 8 8
1 9 6
3 3 3
4 1 7
4 5 5
"
    "22
")))

Submission Info

Submission Time
Task X - Tower
User sansaqua
Language Common Lisp (SBCL 1.1.14)
Score 100
Code Size 6735 Byte
Status AC
Exec Time 160 ms
Memory 25192 KiB

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 25
Set Name Test Cases
All 0_00, 0_01, 0_02, 0_03, 1_00, 1_01, 1_02, 1_03, 1_04, 1_05, 1_06, 1_07, 1_08, 1_09, 1_10, 1_11, 1_12, 1_13, 1_14, 1_15, 1_16, 1_17, 1_18, 1_19, 1_20
Case Name Status Exec Time Memory
0_00 AC 130 ms 25184 KiB
0_01 AC 130 ms 25188 KiB
0_02 AC 129 ms 25188 KiB
0_03 AC 130 ms 25188 KiB
1_00 AC 130 ms 25188 KiB
1_01 AC 137 ms 25188 KiB
1_02 AC 138 ms 25188 KiB
1_03 AC 145 ms 25188 KiB
1_04 AC 137 ms 25188 KiB
1_05 AC 136 ms 25188 KiB
1_06 AC 137 ms 25188 KiB
1_07 AC 137 ms 25184 KiB
1_08 AC 137 ms 25184 KiB
1_09 AC 138 ms 25184 KiB
1_10 AC 140 ms 25188 KiB
1_11 AC 150 ms 25192 KiB
1_12 AC 140 ms 25188 KiB
1_13 AC 141 ms 25188 KiB
1_14 AC 143 ms 25184 KiB
1_15 AC 154 ms 25184 KiB
1_16 AC 158 ms 25184 KiB
1_17 AC 160 ms 25188 KiB
1_18 AC 159 ms 25184 KiB
1_19 AC 156 ms 25184 KiB
1_20 AC 150 ms 25188 KiB