;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam))
  #-swank (set-dispatch-macro-character #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil (values) t))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(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

(defun main ()
  (let* ((n (read))
         (ga (read))
         (sa (read))
         (ba (read))
         (gb (read))
         (sb (read))
         (bb (read))
         (res n))
    (declare (uint16 n ga sa ba gb sb bb))
    (when (and (>= ga gb) (>= sa sb) (>= ba bb))
      (rotatef ga gb)
      (rotatef sa sb)
      (rotatef ba bb))
    (when (and (>= ga gb) (<= sa sb) (>= ba bb))
      (rotatef sa ga)
      (rotatef sb gb))
    (when (and (>= ga gb) (>= sa sb) (<= ba bb))
      (rotatef ba ga)
      (rotatef bb gb))
    (when (and (>= ga gb) (<= sa sb) (<= ba bb))
      (rotatef ga ba)
      (rotatef gb bb))
    (when (and (<= ga gb) (>= sa sb) (<= ba bb))
      (rotatef sa ba)
      (rotatef sb bb))
    (cond ((and (<= ga gb) (<= sa sb) (<= ba bb))
           (loop for gold from 0 to 5000
                 do (loop for silver from 0 to 5000
                          for mid-nuts = (- n (+ (* gold ga) (* silver sa)))
                          when (>= mid-nuts 0)
                          do (multiple-value-bind (bronze rest-nuts) (floor mid-nuts ba)
                               (let ((final-nuts (+ (* gold gb)
                                                    (* silver sb)
                                                    (* bronze bb)
                                                    rest-nuts)))
                                 (setf res (max res final-nuts)))))))
          ((or (and (<= ga gb) (>= sa sb) (>= ba bb)))
           (let ((mid-nuts (+ (mod n ga) (* gb (floor n ga)))))
             (loop for silver from 0
                   for rest-nuts = (- mid-nuts (* silver sb))
                   while (>= rest-nuts 0)
                   do (let* ((bronze (floor rest-nuts bb))
                             (final-nuts (+ (* silver sa)
                                            (* bronze ba)
                                            (mod rest-nuts bb))))
                        (setf res (max res final-nuts))))))
          ((or (and (<= ga gb) (<= sa sb) (>= ba bb)))
           (let ((mid-nuts (loop with res = 0
                                 for gold from 0
                                 for rest-nuts = (- n (* gold ga))
                                 while (>= rest-nuts 0)
                                 do (let ((silver (floor rest-nuts sa)))
                                      (setf res (max res (+ (* gold gb)
                                                            (* silver sb)
                                                            (mod rest-nuts sa)))))
                                 finally (return res))))
             (setf res (max res (+ (mod mid-nuts bb) (* ba (floor mid-nuts bb)))))))
          (t (error "Huh?")))
    (println res)))

#-swank(main)


;; For Test
#+swank
(defun io-equal (in-string out-string &optional (func #'main))
  "Passes IN-STRING to *STANDARD-INPUT*, executes FUNC, and checks the string
output to *STANDARD-OUTPUT* is equal to OUT-STRING."
  (labels ((ensure-last-lf (s)
             (if (and (> (length s) 0)
                      (eql (char s (- (length s) 1)) #\Linefeed))
                 s
                 (uiop:strcat s uiop:+lf+))))
    (equal (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 func)))))))

#+swank
(defun get-clipbrd ()
  (with-output-to-string (out)
    (run-program "C:/msys64/usr/bin/cat.exe" '("/dev/clipboard") :output out)))

#+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*))
  (let ((*standard-output* out))
    (etypecase thing
      (null ; Runs #'MAIN with the string on clipboard
       (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 ; Runs #'MAIN with the string in a text file
       (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 "23
1 1 1
2 1 1
"
    "46
")))


Measured memory_bytes: 22628