;; -*- 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))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(declaim (inline fast-read-char))
(defun fast-read-char (stream)
  (declare #-swank (sb-kernel:ansi-stream stream)
           (inline read-byte))
  #+swank (read-char stream nil #\Newline) ; on SLIME
  #-swank (code-char (read-byte stream nil #.(char-code #\Newline))))

(defstruct (queue (:constructor make-queue
                    (&optional list &aux (tail (last list)))))
  (list nil :type list)
  (tail nil :type (or null (cons t null))))

(declaim (inline enqueue))
(defun enqueue (obj queue)
  (symbol-macrolet ((list (queue-list queue))
                    (tail (queue-tail queue)))
    (if (null list)
        (setf tail (list obj)
              list tail)
        (setf (cdr tail) (list obj)
              tail (cdr tail))))
  queue)

(declaim (inline dequeue))
(defun dequeue (queue)
  (pop (queue-list queue)))

(declaim (inline queue-empty-p))
(defun queue-empty-p (queue)
  (null (queue-list queue)))

(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 ()
  (declare #.OPT)
  (let* ((h (read))
         (w (read))
         (plan (make-array '(1000 1000) :element-type 'uint16 :initial-element 0))
         ;; i|j
         (q (make-queue)))
    (declare (uint32 h w))
    (dotimes (i h)
      (dotimes (j w)
        (if (char= #\# (fast-read-char *standard-input*))
            (enqueue (encode i j) q)
            (setf (aref plan i j) #xffff)))
      (fast-read-char *standard-input*))
    (labels ((frob (next-d next-i next-j)
               (declare (uint32 next-d next-i next-j))
               (when (= #xffff (aref plan next-i next-j))
                 (enqueue (+ next-i (ash next-j 16)) q)
                 (setf (aref plan next-i next-j) next-d))))
      (declare (inline frob))
      (loop until (queue-empty-p q)
            for node of-type uint32 = (dequeue q)
            for i = (logand #.(ldb (byte 16 0) -1) node)
            for j = (ash node -16)
            for d = (aref plan i j)
            do (when (< 0 i)
                 (frob (+ 1 d) (- i 1) j))
               (when (< i (- h 1))
                 (frob (+ 1 d) (+ i 1) j))
               (when (< 0 j)
                 (frob (+ 1 d) i (- j 1)))
               (when (< j (- w 1))
                 (frob (+ 1 d) i (+ j 1)))))
    (println (reduce #'max (array-storage-vector plan)))))

#-swank(main)


;; -*- 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))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(declaim (inline fast-read-char))
(defun fast-read-char (stream)
  (declare #-swank (sb-kernel:ansi-stream stream)
           (inline read-byte))
  #+swank (read-char stream nil #\Newline) ; on SLIME
  #-swank (code-char (read-byte stream nil #.(char-code #\Newline))))

(defstruct (queue (:constructor make-queue
                    (&optional list &aux (tail (last list)))))
  (list nil :type list)
  (tail nil :type (or null (cons t null))))

(declaim (inline enqueue))
(defun enqueue (obj queue)
  (symbol-macrolet ((list (queue-list queue))
                    (tail (queue-tail queue)))
    (if (null list)
        (setf tail (list obj)
              list tail)
        (setf (cdr tail) (list obj)
              tail (cdr tail))))
  queue)

(declaim (inline dequeue))
(defun dequeue (queue)
  (pop (queue-list queue)))

(declaim (inline queue-empty-p))
(defun queue-empty-p (queue)
  (null (queue-list queue)))

(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

(declaim (inline encode))
(defun encode (i j)
  (dpb j (byte 16 16) i))

(defun main ()
  (declare #.OPT)
  (let* ((h (read))
         (w (read))
         (plan (make-array '(1000 1000) :element-type 'uint16 :initial-element 0))
         ;; i|j
         (q (make-queue)))
    (declare (uint32 h w))
    (dotimes (i h)
      (dotimes (j w)
        (if (char= #\# (fast-read-char *standard-input*))
            (enqueue (encode i j) q)
            (setf (aref plan i j) #xffff)))
      (fast-read-char *standard-input*))
    (labels ((frob (next-d next-i next-j)
               (declare (fixnum next-d next-i next-j))
               (when (= #xffff (aref plan next-i next-j))
                 (enqueue (encode next-i next-j) q)
                 (setf (aref plan next-i next-j) next-d))))
      (declare (inline frob))
      (loop until (queue-empty-p q)
            for node of-type uint32 = (dequeue q)
            for i = (logand #.(ldb (byte 16 0) -1) node)
            for j = (ash node -16)
            for d = (aref plan i j)
            do (when (< 0 i)
                 (frob (+ 1 d) (- i 1) j))
               (when (< i (- h 1))
                 (frob (+ 1 d) (+ i 1) j))
               (when (< 0 j)
                 (frob (+ 1 d) i (- j 1)))
               (when (< j (- w 1))
                 (frob (+ 1 d) i (+ j 1)))))
    (println (reduce #'max (array-storage-vector plan)))))

#-swank(main)

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 300 points

Problem Statement

You are given a grid of squares with H horizontal rows and W vertical columns, where each square is painted white or black.
HW characters from A_{11} to A_{HW} represent the colors of the squares.
A_{ij} is # if the square at the i-th row from the top and the j-th column from the left is black, and A_{ij} is . if that square is white.

We will repeatedly perform the following operation until all the squares are black:

Every white square that shares a side with a black square, becomes black.

Find the number of operations that will be performed.
The initial grid has at least one black square.

Constraints

1 \leq H,W \leq 1000

A_{ij} is # or ..

The given grid has at least one black square.

Input

Input is given from Standard Input in the following format:

H W
A_{11}A_{12}...A_{1W}
:
A_{H1}A_{H2}...A_{HW}

Output

Print the number of operations that will be performed.

Sample Input 1

3 3
...
.#.
...

Sample Output 1

2

After one operation, all but the corners of the grid will be black. After one more operation, all the squares will be black.

Sample Input 2

6 6
..#..#
......
#..#..
......
.#....
....#.

Sample Output 2

3

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


;; -*- 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))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(defmacro buffered-read-line (&optional (buffer-size 30) (in '*standard-input*) (term-char #\Space))
  "Reads ASCII inputs and returns two values: the string and the end
position. Note that the returned string will be reused if this form is executed
more than once.

This macro calls READ-BYTE to read characters though it calls READ-CHAR instead
on SLIME because SLIME's IO is not bivalent."
  (let ((buffer (gensym))
        (character (gensym))
        (idx (gensym))
        (stream (gensym)))
    `(let* ((,stream ,in)
            (,buffer (load-time-value (make-string ,buffer-size :element-type 'base-char))))
       (declare (simple-base-string ,buffer)
                (inline read-byte)
                #+swank (sb-kernel:ansi-stream ,stream))
       (loop for ,character of-type base-char =
                ,(if (member :swank *features*)
                     `(read-char ,stream nil #\Newline) ; on SLIME
                     `(code-char (read-byte ,stream nil #.(char-code #\Newline))))
             for ,idx from 0
             until (char= ,character #\Newline)
             do (setf (schar ,buffer ,idx) ,character)
             finally (when (< ,idx ,buffer-size)
                       (setf (schar ,buffer ,idx) ,term-char))
                     (return (values ,buffer ,idx))))))

(defstruct (queue (:constructor make-queue
                    (&optional list &aux (tail (last list)))))
  (list nil :type list)
  (tail nil :type (or null (cons t null))))

(declaim (inline enqueue))
(defun enqueue (obj queue)
  (symbol-macrolet ((list (queue-list queue))
                    (tail (queue-tail queue)))
    (if (null list)
        (setf tail (list obj)
              list tail)
        (setf (cdr tail) (list obj)
              tail (cdr tail))))
  queue)

(declaim (inline dequeue))
(defun dequeue (queue)
  (pop (queue-list queue)))

(declaim (inline queue-empty-p))
(defun queue-empty-p (queue)
  (null (queue-list queue)))

(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

(declaim (inline encode))
(defun encode (i j)
  (dpb j (byte 16 16) i))

(defun main ()
  (declare #.OPT)
  (let* ((h (read))
         (w (read))
         (plan (make-array (list h w) :element-type 'uint16 :initial-element #xffff))
         ;; i|j|dist
         (q (make-queue)))
    (declare (uint32 h w))
    (dotimes (i h)
      (let ((line (buffered-read-line 1000)))
        (dotimes (j w)
          (when (char= #\# (aref line j))
            (setf (aref plan i j) 0)
            (enqueue (encode i j) q)))))
    (labels ((frob (next-d next-i next-j)
               (declare (fixnum next-d next-i next-j))
               (when (= #xffff (aref plan next-i next-j))
                 (enqueue (encode next-i next-j) q)
                 (setf (aref plan next-i next-j) next-d))))
      (declare (inline frob))
      (loop until (queue-empty-p q)
            for node of-type uint32 = (dequeue q)
            for i = (ldb (byte 16 0) node)
            for j = (ldb (byte 16 16) node)
            for d = (aref plan i j)
            do (when (< 0 i)
                 (frob (+ 1 d) (- i 1) j))
               (when (< i (- h 1))
                 (frob (+ 1 d) (+ i 1) j))
               (when (< 0 j)
                 (frob (+ 1 d) i (- j 1)))
               (when (< j (- w 1))
                 (frob (+ 1 d) i (+ j 1)))))
    (println (reduce #'max (array-storage-vector plan)))))

#-swank(main)


;; -*- 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 (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(defstruct (queue (:constructor make-queue
                    (&optional list &aux (tail (last list)))))
  (list nil :type list)
  (tail nil :type (or null (cons t null))))

(declaim (inline enqueue))
(defun enqueue (obj queue)
  (symbol-macrolet ((list (queue-list queue))
                    (tail (queue-tail queue)))
    (if (null list)
        (setf tail (list obj)
              list tail)
        (setf (cdr tail) (list obj)
              tail (cdr tail))))
  queue)

(declaim (inline dequeue))
(defun dequeue (queue)
  (pop (queue-list queue)))

(declaim (inline queue-empty-p))
(defun queue-empty-p (queue)
  (null (queue-list queue)))

(declaim (inline read-line-into))
(defun read-line-into (buffer-string &key (in *standard-input*) (terminate-char #\Space))
  (declare (simple-base-string buffer-string))
  (loop for c of-type base-char =
           #-swank (code-char (read-byte in nil #.(char-code #\Newline)))
           #+swank (read-char in nil #\Newline)
        for idx from 0
        until (char= c #\Newline)
        do (setf (schar buffer-string idx) c)
        finally (when (< idx (length buffer-string))
                  (setf (schar buffer-string idx) terminate-char))
                (return (values buffer-string idx))))

(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

(declaim (inline encode decode))
(defun encode (d i j)
  (dpb j (byte 16 32) (dpb i (byte 16 16) d)))

(defun main ()
  (declare #.OPT)
  (let* ((h (read))
         (w (read))
         (plan (make-array (list h w) :element-type 'uint16 :initial-element #xffff))
         (line (make-string w :element-type 'base-char))
         ;; i|j|dist
         (q (make-queue)))
    (declare (uint32 h w))
    (dotimes (i h)
      (read-line-into line)
      (dotimes (j w)
        (when (char= #\# (aref line j))
          (setf (aref plan i j) 0)
          (enqueue (encode 0 i j) q))))
    (labels ((frob (next-d next-i next-j)
               (declare (fixnum next-d next-i next-j))
               (when (= #xffff (aref plan next-i next-j))
                 (enqueue (encode next-d next-i next-j) q)
                 (setf (aref plan next-i next-j) next-d))))
      (declare (inline frob))
      (loop until (queue-empty-p q)
            for node of-type uint62 = (dequeue q)
            for d = (ldb (byte 16 0) node)
            for i = (ldb (byte 16 16) node)
            for j = (ldb (byte 16 32) node)
            do (when (< 0 i)
                 (frob (+ 1 d) (- i 1) j))
               (when (< i (- h 1))
                 (frob (+ 1 d) (+ i 1) j))
               (when (< 0 j)
                 (frob (+ 1 d) i (- j 1)))
               (when (< j (- w 1))
                 (frob (+ 1 d) i (+ j 1)))))
    (println (reduce #'max (array-storage-vector plan)))))

#-swank(main)


;; -*- 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 (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
;; -*- coding:utf-8 -*-

(defstruct (queue (:constructor make-queue
                    (&optional list &aux (tail (last list)))))
  (list nil :type list)
  (tail nil :type (or null (cons t null))))

(declaim (inline enqueue))
(defun enqueue (obj queue)
  (symbol-macrolet ((list (queue-list queue))
                    (tail (queue-tail queue)))
    (if (null list)
        (setf tail (list obj)
              list tail)
        (setf (cdr tail) (list obj)
              tail (cdr tail))))
  queue)

(declaim (inline dequeue))
(defun dequeue (queue)
  (pop (queue-list queue)))

(declaim (inline queue-empty-p))
(defun queue-empty-p (queue)
  (null (queue-list queue)))

(declaim (inline read-line-into))
(defun read-line-into (buffer-string &key (in *standard-input*) (terminate-char #\Space))
  (declare (simple-base-string buffer-string))
  (loop for c of-type base-char =
           #-swank (code-char (read-byte in nil #.(char-code #\Newline)))
           #+swank (read-char in nil #\Newline)
        for idx from 0
        until (char= c #\Newline)
        do (setf (schar buffer-string idx) c)
        finally (when (< idx (length buffer-string))
                  (setf (schar buffer-string idx) terminate-char))
                (return (values buffer-string idx))))

(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 ()
  (declare #.OPT)
  (let* ((h (read))
         (w (read))
         (plan (make-array (list h w) :element-type 'uint16 :initial-element #xffff))
         (line (make-string w :element-type 'base-char))
         ;; (dist i . j)
         (q (make-queue)))
    (declare (uint32 h w))
    (dotimes (i h)
      (read-line-into line)
      (dotimes (j w)
        (when (char= #\# (aref line j))
          (setf (aref plan i j) 0)
          (enqueue (list* 0 i j) q))))
    (labels ((frob (next-d next-i next-j)
               (declare (fixnum next-d next-i next-j))
               (when (= #xffff (aref plan next-i next-j))
                 (enqueue (list* next-d next-i next-j) q)
                 (setf (aref plan next-i next-j) next-d))))
      (declare (inline frob))
      (loop until (queue-empty-p q)
            for (d i . j) of-type (uint32 uint32 . uint32) = (dequeue q)
            do (when (< 0 i)
                 (frob (+ 1 d) (- i 1) j))
               (when (< i (- h 1))
                 (frob (+ 1 d) (+ i 1) j))
               (when (< 0 j)
                 (frob (+ 1 d) i (- j 1)))
               (when (< j (- w 1))
                 (frob (+ 1 d) i (+ j 1)))))
    (println (reduce #'max (array-storage-vector plan)))))

#-swank(main)


;; -*- 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 (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
;; -*- coding:utf-8 -*-

(defstruct (queue (:constructor make-queue
                    (&optional list &aux (tail (last list)))))
  (list nil :type list)
  (tail nil :type (or null (cons t null))))

(declaim (inline enqueue))
(defun enqueue (obj queue)
  (symbol-macrolet ((list (queue-list queue))
                    (tail (queue-tail queue)))
    (if (null list)
        (setf tail (list obj)
              list tail)
        (setf (cdr tail) (list obj)
              tail (cdr tail))))
  queue)

(declaim (inline dequeue))
(defun dequeue (queue)
  (pop (queue-list queue)))

(declaim (inline queue-empty-p))
(defun queue-empty-p (queue)
  (null (queue-list queue)))

(declaim (inline read-line-into))
(defun read-line-into (buffer-string &key (in *standard-input*) (terminate-char #\Space))
  (declare (simple-base-string buffer-string))
  (loop for c of-type base-char =
           #-swank (code-char (read-byte in nil #.(char-code #\Newline)))
           #+swank (read-char in nil #\Newline)
        for idx from 0
        until (char= c #\Newline)
        do (setf (schar buffer-string idx) c)
        finally (when (< idx (length buffer-string))
                  (setf (schar buffer-string idx) terminate-char))
                (return (values buffer-string idx))))

(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 ()
  (declare #.OPT)
  (let* ((h (read))
         (w (read))
         (plan (make-array (list h w) :element-type 'uint16 :initial-element #xffff))
         (line (make-string w :element-type 'base-char))
         ;; (dist i . j)
         (q (make-queue)))
    (declare (uint32 h w))
    (dotimes (i h)
      (read-line-into line)
      (dotimes (j w)
        (when (char= #\# (aref line j))
          (setf (aref plan i j) 0)
          (enqueue (list* 0 i j) q))))
    (labels ((frob (next-d next-i next-j)
               (declare (fixnum next-d next-i next-j))
               (when (= #xffff (aref plan next-i next-j))
                 (enqueue (list* next-d next-i next-j) q)
                 (setf (aref plan next-i next-j) next-d))))
      (loop until (queue-empty-p q)
            for (d i . j) of-type (uint32 uint32 . uint32) = (dequeue q)
            do (when (< 0 i)
                 (frob (+ 1 d) (- i 1) j))
               (when (< i (- h 1))
                 (frob (+ 1 d) (+ i 1) j))
               (when (< 0 j)
                 (frob (+ 1 d) i (- j 1)))
               (when (< j (- w 1))
                 (frob (+ 1 d) i (+ j 1)))))
    (println (reduce #'max (array-storage-vector plan)))))

#-swank(main)


;; -*- 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 (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
;; -*- coding:utf-8 -*-

(defstruct (queue (:constructor make-queue
                    (&optional list &aux (tail (last list)))))
  (list nil :type list)
  (tail nil :type (or null (cons t null))))

(declaim (inline enqueue))
(defun enqueue (obj queue)
  (symbol-macrolet ((list (queue-list queue))
                    (tail (queue-tail queue)))
    (if (null list)
        (setf tail (list obj)
              list tail)
        (setf (cdr tail) (list obj)
              tail (cdr tail))))
  queue)

(declaim (inline dequeue))
(defun dequeue (queue)
  (pop (queue-list queue)))

(declaim (inline queue-empty-p))
(defun queue-empty-p (queue)
  (null (queue-list queue)))

(declaim (inline enqueue-front))
(defun enqueue-front (obj queue)
  (symbol-macrolet ((list (queue-list queue))
                    (tail (queue-tail queue)))
    (if (null list)
        (setf tail (list obj)
              list tail)
        (push obj list))
    queue))

(declaim (inline read-line-into))
(defun read-line-into (buffer-string &key (in *standard-input*) (terminate-char #\Space))
  (declare (simple-base-string buffer-string))
  (loop for c of-type base-char =
           #-swank (code-char (read-byte in nil #.(char-code #\Newline)))
           #+swank (read-char in nil #\Newline)
        for idx from 0
        until (char= c #\Newline)
        do (setf (schar buffer-string idx) c)
        finally (when (< idx (length buffer-string))
                  (setf (schar buffer-string idx) terminate-char))
                (return (values buffer-string idx))))

(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 ()
  (declare #.OPT)
  (let* ((h (read))
         (w (read))
         (plan (make-array (list h w) :element-type 'uint16 :initial-element #xffff))
         (line (make-string w :element-type 'base-char))
         ;; (dist i . j)
         (q (make-queue)))
    (declare (uint32 h w))
    (dotimes (i h)
      (read-line-into line)
      (dotimes (j w)
        (when (char= #\# (aref line j))
          (setf (aref plan i j) 0)
          (enqueue (list* 0 i j) q))))
    (labels ((frob (next-d next-i next-j)
               (declare (fixnum next-d next-i next-j))
               (when (and (<= 0 next-i (- h 1))
                          (<= 0 next-j (- w 1))
                          (= #xffff (aref plan next-i next-j)))
                 (enqueue (list* next-d next-i next-j) q)
                 (setf (aref plan next-i next-j) next-d))))
      (loop until (queue-empty-p q)
            for (d i . j) of-type (uint32 uint32 . uint32) = (dequeue q)
            do (frob (+ 1 d) (- i 1) j)
               (frob (+ 1 d) (+ i 1) j)
               (frob (+ 1 d) i (- j 1))
               (frob (+ 1 d) i (+ j 1))))
    (println (reduce #'max (array-storage-vector plan)))))

#-swank(main)


(defparameter *h* (read))
(defparameter *w* (read))

(defun black-white-positions (h w)
  (let ((black-positions '())
        (white-positions '()))
    (dotimes (i h)
      (let ((row (remove #\ (coerce (read-line) 'list))))
        (dotimes (j w)
          (let ((a_ij (nth j row)))
            (if (char= #\# a_ij)
                (push (cons i j) black-positions)
                (push (cons i j) white-positions))))))
    (values black-positions
            white-positions)))

(multiple-value-bind (b-poss w-poss)
  (black-white-positions *h* *w*)
  (defparameter *black-positions* b-poss)
  (defparameter *white-positions* w-poss))

(defun f (b-poss w-poss)
  (reduce #'max
          (mapcar (lambda (w-pos)
                    (reduce #'min
                            (mapcar (lambda (b-pos) (+ (abs (- (car b-pos) (car w-pos)))
                                                       (abs (- (cdr b-pos) (cdr w-pos)))))
                                    b-poss)))
                  w-poss)))

(princ (f *black-positions* *white-positions*))


;; -*- 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 (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
;; -*- coding:utf-8 -*-

(defstruct (queue (:constructor make-queue
                    (&optional list &aux (tail (last list)))))
  (list nil :type list)
  (tail nil :type (or null (cons t null))))

(declaim (inline enqueue))
(defun enqueue (obj queue)
  (symbol-macrolet ((list (queue-list queue))
                    (tail (queue-tail queue)))
    (if (null list)
        (setf tail (list obj)
              list tail)
        (setf (cdr tail) (list obj)
              tail (cdr tail))))
  queue)

(declaim (inline dequeue))
(defun dequeue (queue)
  (pop (queue-list queue)))

(declaim (inline queue-empty-p))
(defun queue-empty-p (queue)
  (null (queue-list queue)))

(declaim (inline enqueue-front))
(defun enqueue-front (obj queue)
  (symbol-macrolet ((list (queue-list queue))
                    (tail (queue-tail queue)))
    (if (null list)
        (setf tail (list obj)
              list tail)
        (push obj list))
    queue))

(declaim (inline read-line-into))
(defun read-line-into (buffer-string &key (in *standard-input*) (terminate-char #\Space))
  (declare (simple-base-string buffer-string))
  (loop for c of-type base-char =
           #-swank (code-char (read-byte in nil #.(char-code #\Newline)))
           #+swank (read-char in nil #\Newline)
        for idx from 0
        until (char= c #\Newline)
        do (setf (schar buffer-string idx) c)
        finally (when (< idx (length buffer-string))
                  (setf (schar buffer-string idx) terminate-char))
                (return (values buffer-string idx))))

(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* ((h (read))
         (w (read))
         (plan (make-array (list h w) :element-type 'uint16 :initial-element #xffff))
         (line (make-string w :element-type 'base-char))
         ;; (dist i . j)
         (q (make-queue)))
    (declare (uint32 h w))
    (dotimes (i h)
      (read-line-into line)
      (dotimes (j w)
        (when (char= #\# (aref line j))
          (setf (aref plan i j) 0)
          (enqueue (list* 0 i j) q))))
    (labels ((frob (next-d next-i next-j)
               (declare (fixnum next-d next-i next-j))
               (when (and (<= 0 next-i (- h 1))
                          (<= 0 next-j (- w 1))
                          (= #xffff (aref plan next-i next-j)))
                 (enqueue (list* next-d next-i next-j) q)
                 (setf (aref plan next-i next-j) next-d))))
      (loop until (queue-empty-p q)
            for (d i . j) = (dequeue q)
            do (frob (+ 1 d) (- i 1) j)
               (frob (+ 1 d) (+ i 1) j)
               (frob (+ 1 d) i (- j 1))
               (frob (+ 1 d) i (+ j 1))))
    (println (reduce #'max (array-storage-vector plan)))))

#-swank(main)


(defun main ()
  (let* ((n (read))
         (a (make-array 0 :element-type 'integer
                          :adjustable t
                          :fill-pointer 0))
         (g (make-array (+ n 2) :element-type 'integer
                                :initial-element 0
                                :adjustable t))
         (gr (make-array (+ n 2) :element-type 'integer
                                 :initial-element 0
                                 :adjustable t))
         (ans 0))
    (dotimes (i n)
      (vector-push-extend (read) a))
    (setf (aref g 0) (aref a 0))
    (setf (aref gr (1+ n)) (aref a (1- n)))
    (loop for i from 1 upto n
          do (setf (aref g i) (gcd (aref g (1- i)) (aref a (1- i))))
             (setf (aref gr (1+ (- n i))) (gcd (aref gr (- n i -2)) (aref a (- n i)))))
;;    (format t "~A~%~A~%~A~%" a g gr)
    (loop for i from 2 below n
          do (setf ans (max ans (gcd (aref g (1- i)) (aref gr (1+ i))))))
    (format t "~A~%" (max ans (aref g (1- n)) (aref gr 2)))))

(main)

Score : 300 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on the blackboard.

You will choose one of them and replace it with an integer of your choice between 1 and 10^9 (inclusive), possibly the same as the integer originally written.

Find the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Output

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Sample Input 1

3
7 6 8

Sample Output 1

2

If we replace 7 with 4, the greatest common divisor of the three integers on the blackboard will be 2, which is the maximum possible value.

Sample Input 2

3
12 15 18

Sample Output 2

6

Sample Input 3

2
1000000000 1000000000

Sample Output 3

1000000000

We can replace an integer with itself.


(defun main ()
  (let* ((n (read))
         (a (make-array 0 :element-type 'integer
                          :adjustable t
                          :fill-pointer 0))
         (g (make-array (+ n 2) :element-type 'integer
                                :initial-element 0
                                :adjustable t))
         (gr (make-array (+ n 2) :element-type 'integer
                                 :initial-element 0
                                 :adjustable t))
         (ans 0))
    (dotimes (i n)
      (vector-push-extend (read) a))
    (setf (aref g 0) (aref a 0))
    (setf (aref gr (1+ n)) (aref a (1- n)))
    (loop for i from 1 upto n
          do (setf (aref g i) (gcd (aref g (1- i)) (aref a (1- i))))
             (setf (aref gr (1+ (- n i))) (gcd (aref gr (1+ i)) (aref a (- n i)))))
;;    (format t "~A~%~A~%~A~%" a g gr)
    (loop for i from 2 below n
          do (setf ans (max ans (gcd (aref g (1- i)) (aref gr (1+ i))))))
    (format t "~A~%" (max ans (aref g (1- n)) (aref gr 2)))))

(main)


;; -*- 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 (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (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
                                  ;; (return-from read-fixnum 0)
                                  (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) (the fixnum (* result 10))))
              (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

(declaim (ftype (function * (values (array (integer 0 #.most-positive-fixnum) (*))))
                enum-divisors))
(defun enum-divisors (x)
  "Enumerates divisors of X in ascending order"
  (declare #.OPT (fixnum x))
  (setf x (abs x))
  (let* ((sqrt (isqrt x))
         (res (make-array (round (expt x 0.33d0))
                          :element-type '(integer 0 #.most-positive-fixnum)
                          :fill-pointer 0)))
    (loop for i from 1 to sqrt
          do (multiple-value-bind (quot rem) (floor x i)
               (when (zerop rem)
                 (vector-push-extend i res)
                 (unless (= i quot)
                   (vector-push-extend quot res)))))
    res))

(declaim (inline unique-merge))
(defun unique-merge (element-type vec1 vec2 predicate)
  (declare (function predicate))
  (let ((res (make-array 0
                         :element-type element-type
                         :fill-pointer 0)))
    (let ((i1 1)
          (i2 0)
          (pos -1))
      (declare ((integer -1 #.most-positive-fixnum) i1 i2 pos))
      (loop (if (= i1 (length vec1))
                (loop (when (= i2 (length vec2))
                        (return-from unique-merge res))
                      (when (or (= -1 pos)
                                (funcall predicate (aref res pos) (aref vec2 i2)))
                        (vector-push-extend (aref vec2 i2) res)
                        (incf pos))
                      (incf i2))
                (when (= i2 (length vec2))
                  (loop (when (= i1 (length vec1))
                          (return-from unique-merge res))
                        (when (or (= -1 pos)
                                  (funcall predicate (aref res pos) (aref vec1 i1)))
                          (vector-push-extend (aref vec1 i1) res)
                          (incf pos))
                        (incf i1))))
            (if (funcall predicate (aref vec1 i1) (aref vec2 i2))
                (progn
                  (when (or (= -1 pos)
                            (funcall predicate (aref res pos) (aref vec1 i1)))
                    (vector-push-extend (aref vec1 i1) res)
                    (incf pos))
                  (incf i1))
                (progn
                  (when (or (= -1 pos)
                            (funcall predicate (aref res pos) (aref vec2 i2)))
                    (vector-push-extend (aref vec2 i2) res)
                    (incf pos))
                  (incf i2)))))))

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (as (make-array n :element-type 'uint32)))
    (declare (uint32 n))
    (dotimes (i n)
      (setf (aref as i) (read-fixnum)))
    (let* ((div1 (sort (enum-divisors (aref as 0)) #'>))
           (div2 (sort (enum-divisors (aref as 1)) #'>))
           (div (unique-merge 'uint32 div1 div2
                              (lambda (x y)
                                (declare (uint32 x y))
                                (> x y)))))
      (declare ((array uint32 (*)) div))
      (loop for d across div
            when (>= (loop for a of-type uint32 across as count (zerop (mod a d)))
                     (- n 1))
            do (println d)
               (return-from main)
            finally (error "Huh?")))))

#-swank(main)


;; -*- 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 (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (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
                                  ;; (return-from read-fixnum 0)
                                  (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) (the fixnum (* result 10))))
              (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

(declaim (ftype (function * (values (array (integer 0 #.most-positive-fixnum) (*))))
                enum-divisors))
(defun enum-divisors (x)
  "Enumerates divisors of X in ascending order"
  (declare #.OPT (fixnum x))
  (setf x (abs x))
  (let* ((sqrt (isqrt x))
         (res (make-array sqrt
                          :element-type '(integer 0 #.most-positive-fixnum)
                          :fill-pointer 0)))
    (loop for i from 1 to sqrt
          do (multiple-value-bind (quot rem) (floor x i)
               (when (zerop rem)
                 (vector-push-extend i res)
                 (unless (= i quot)
                   (vector-push-extend quot res)))))
    res))

(declaim (inline unique-merge))
(defun unique-merge (element-type vec1 vec2 predicate)
  (declare (function predicate))
  (let ((res (make-array 0
                         :element-type element-type
                         :fill-pointer 0)))
    (let ((i1 1)
          (i2 0)
          (pos -1))
      (declare ((integer -1 #.most-positive-fixnum) i1 i2 pos))
      (loop (if (= i1 (length vec1))
                (loop (when (= i2 (length vec2))
                        (return-from unique-merge res))
                      (when (or (= -1 pos)
                                (funcall predicate (aref res pos) (aref vec2 i2)))
                        (vector-push-extend (aref vec2 i2) res)
                        (incf pos))
                      (incf i2))
                (when (= i2 (length vec2))
                  (loop (when (= i1 (length vec1))
                          (return-from unique-merge res))
                        (when (or (= -1 pos)
                                  (funcall predicate (aref res pos) (aref vec1 i1)))
                          (vector-push-extend (aref vec1 i1) res)
                          (incf pos))
                        (incf i1))))
            (if (funcall predicate (aref vec1 i1) (aref vec2 i2))
                (progn
                  (when (or (= -1 pos)
                            (funcall predicate (aref res pos) (aref vec1 i1)))
                    (vector-push-extend (aref vec1 i1) res)
                    (incf pos))
                  (incf i1))
                (progn
                  (when (or (= -1 pos)
                            (funcall predicate (aref res pos) (aref vec2 i2)))
                    (vector-push-extend (aref vec2 i2) res)
                    (incf pos))
                  (incf i2)))))))

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (as (make-array n :element-type 'uint32)))
    (declare (uint32 n))
    (dotimes (i n)
      (setf (aref as i) (read-fixnum)))
    (let* ((div1 (sort (enum-divisors (aref as 0)) #'>))
           (div2 (sort (enum-divisors (aref as 1)) #'>))
           (div (unique-merge 'uint32 div1 div2
                              (lambda (x y)
                                (declare (uint32 x y))
                                (> x y)))))
      (declare ((array uint32 (*)) div))
      (loop for d across div
            when (>= (loop for a of-type uint32 across as count (zerop (mod a d)))
                     (- n 1))
            do (println d)
               (return-from main)
            finally (error "Huh?")))))

#-swank(main)


;; -*- 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 (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(defmacro nlet (name args &body body)
  (labels ((ensure-list (x) (if (listp x) x (list x))))
    (let ((args (mapcar #'ensure-list args)))
      `(labels ((,name ,(mapcar #'car args) ,@body))
         (,name ,@(mapcar #'cadr args))))))

(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (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
                                  ;; (return-from read-fixnum 0)
                                  (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) (the fixnum (* result 10))))
              (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

(declaim (ftype (function * (values (array (integer 0 #.most-positive-fixnum) (*))))
                enum-divisors))
(defun enum-divisors (x)
  "Enumerates divisors of X in ascending order"
  (declare #.OPT (fixnum x))
  (setf x (abs x))
  (let* ((sqrt (isqrt x))
         (res (make-array sqrt
                          :element-type '(integer 0 #.most-positive-fixnum)
                          :fill-pointer 0)))
    (loop for i from 1 to sqrt
          do (multiple-value-bind (quot rem) (floor x i)
               (when (zerop rem)
                 (vector-push-extend i res)
                 (unless (= i quot)
                   (vector-push-extend quot res)))))
    res))

(declaim (inline unique-merge))
(defun unique-merge (element-type vec1 vec2 predicate)
  (declare (function predicate))
  (let ((res (make-array 0
                         :element-type element-type
                         :fill-pointer 0)))
    (let ((i1 1)
          (i2 0)
          (pos -1))
      (declare ((integer -1 #.most-positive-fixnum) i1 i2 pos))
      (loop (if (= i1 (length vec1))
                (loop (when (= i2 (length vec2))
                        (return-from unique-merge res))
                      (when (or (= -1 pos)
                                (funcall predicate (aref res pos) (aref vec2 i2)))
                        (vector-push-extend (aref vec2 i2) res)
                        (incf pos))
                      (incf i2))
                (when (= i2 (length vec2))
                  (loop (when (= i1 (length vec1))
                          (return-from unique-merge res))
                        (when (or (= -1 pos)
                                  (funcall predicate (aref res pos) (aref vec1 i1)))
                          (vector-push-extend (aref vec1 i1) res)
                          (incf pos))
                        (incf i1))))
            (if (funcall predicate (aref vec1 i1) (aref vec2 i2))
                (progn
                  (when (or (= -1 pos)
                            (funcall predicate (aref res pos) (aref vec1 i1)))
                    (vector-push-extend (aref vec1 i1) res)
                    (incf pos))
                  (incf i1))
                (progn
                  (when (or (= -1 pos)
                            (funcall predicate (aref res pos) (aref vec2 i2)))
                    (vector-push-extend (aref vec2 i2) res)
                    (incf pos))
                  (incf i2)))))))

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (as (make-array (- n 2) :element-type 'uint32))
         (a1 (read-fixnum))
         (a2 (read-fixnum)))
    (declare (uint32 n))
    (dotimes (i (- n 2))
      (setf (aref as i) (read-fixnum)))
    (let* ((div1 (sort (enum-divisors a1) #'>))
           (div2 (sort (enum-divisors a2) #'>))
           (div (unique-merge 'uint32 div1 div2
                              (lambda (x y)
                                (declare (uint32 x y))
                                (> x y)))))
      (declare ((array uint32 (*)) div))
      (loop for d across div
            when (every (lambda (a) (zerop (mod (the uint32 a) d))) as)
            do (println d)
               (return-from main)
            finally (error "Huh?")))))

#-swank(main)


(defun input (n)
  (let ((a (make-array (1+ n))))
    (loop for i from 1 to n do (setf (aref a i) (read)))
    a))
(let* ((n (read))
       (a (input n))
       (r (make-array (+ n 2)))
       (l (make-array (+ n 2))))
  (setf (aref l n) (aref a n))
  (loop for i from 1 below n
    do (setf (aref r i) (gcd (aref r (1- i)) (aref a i))))
  (loop for i from n downto 1
    do (setf (aref l (1- i)) (gcd (aref l i) (aref a (1- i)))))
  (format t "~A~%"
          (loop for i from 1 to n
                maximize (gcd (aref r (1- i)) (aref l (1+ i))))))


(defparameter *a* #())
(defparameter *ans* 0)
(defparameter *memo* '())

(defun gcd2(a b)
  (if (= b 0) a (gcd2 b (mod a b))))

(defun solve ()
  (loop :for i :from 0 :to (1- (length *a*))
        :if (not (find (aref *a* i) *memo*))
        :do
           (let ((value (aref *a* i)))
             (append *memo* (list value))
             (setf (aref *a* i) 0)
             (let ((gcd (reduce #'gcd2 *a*)))
               (setf *ans* (max *ans* gcd)))
             (setf (aref *a* i) value)))
  *ans*)

(defun main ()
  (let* ((n (read))
         (a (loop :repeat n :collect (read))))
    (setf *a* (make-array (length a) :initial-contents a))
    (format t "~A~%" (solve))))

(main)


(defparameter *a* #())
(defparameter *ans* 0)
(defparameter *memo* '())

(defun solve ()
  (loop :for i :from 0 :to (1- (length *a*))
        :if (not (find (aref *a* i) *memo*))
        :do
           (let ((value (aref *a* i)))
             (append *memo* (list value))
             (setf (aref *a* i) 0)
             (let ((gcd (reduce #'gcd *a*)))
               (setf *ans* (max *ans* gcd)))
             (setf (aref *a* i) value)))
  *ans*)

(defun main ()
  (let* ((n (read))
         (a (loop :repeat n :collect (read))))
    (setf *a* (make-array (length a) :initial-contents a))
    (format t "~A~%" (solve))))

(main)


;; -*- 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 (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (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
                                  ;; (return-from read-fixnum 0)
                                  (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) (the fixnum (* result 10))))
              (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

(defun main ()
  (let* ((n (read))
         (as (make-array n :element-type 'uint32))
         (cumul (make-array (+ 1 n) :element-type 'uint32 :initial-element 0))
         (cumul2 (make-array (+ 1 n) :element-type 'uint32 :initial-element 0)))
    (dotimes (i n)
      (setf (aref as i) (read-fixnum)))
    (dotimes (i n)
      (setf (aref cumul (+ i 1))
            (gcd (aref cumul i) (aref as i))))
    (loop for i from n above 0
          do (setf (aref cumul2 (- i 1))
                   (gcd (aref cumul2 i) (aref as (- i 1)))))
    (println
     (loop for i from 0 below n
           maximize (gcd (aref cumul i) (aref cumul2 (+ i 1)))))))

#-swank(main)


(let* ((n (read))
       (arr (make-array n :element-type 'fixnum :initial-element 0)))
  (loop :for a :from 0 :upto (1- n) :do(setf (aref arr a) (read)))
  (defun r-at (q)
    (remove nil arr :start q :count 1 :test (constantly T)))
  (princ (loop :for k :from 0 :upto (1- n) :maximize (reduce #'gcd (r-at k)))))


(defun input (n)
  (let ((a (make-array n)))
    (loop for i below n do (setf (aref a i) (read)))
    a))
(let* ((n (read))
       (a (input n)))
  (format t "~A~%"
          (loop for i below n
                maximize (let ((x 0))
                           (loop for j below n
                                 when (not (= i j))
                                 do (setf x (gcd x (aref a j))))
                           x))))


(let ((a (read))
      (b (read))
      (c (read)))
     (princ (min (floor b a) c)))

Score : 100 points

Problem Statement

Takahashi likes the sound when he buys a drink from a vending machine.

That sound can be heard by spending A yen (the currency of Japan) each time.

Takahashi has B yen. He will hear the sound as many times as he can with that money, but at most C times, as he would be satisfied at that time.

How many times will he hear the sound?

Constraints

All values in input are integers.

1 \leq A, B, C \leq 100

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the number of times Takahashi will hear his favorite sound.

Sample Input 1

2 11 4

Sample Output 1

4

Since he has not less than 8 yen, he will hear the sound four times and be satisfied.

Sample Input 2

3 9 5

Sample Output 2

3

He may not be able to be satisfied.

Sample Input 3

100 1 10

Sample Output 3

0


(defun favorite-sound(a b c)
  (min
   (floor (/ b a))
   c))

(format t "~A~%" (favorite-sound(read) (read) (read)))


(defun fs (a b c) (min (floor b a) c))
(format t "~A~%" (fs (read) (read) (read)))


(let ((a (read))
      (b (read)))
     (princ(min(floor(/ b a))(read))))


(setq a(read))(setq b(read))(setq c(read))
(princ(min(floor b a)c))


(let* ((a (read))
       (b (read))
       (c (read)))
  (if (< (floor b a) c)
      (princ (floor b a))
      (princ c)))


(defun main ()
  (let* ((a (read))
         (b (read))
         (c (read)))
    (format t "~A~%" (min (floor b a) c))))


(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 (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

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

(defmacro println (obj &optional (stream '*standard-output*))
  `(let ((*read-default-float-format* 'double-float))
     (prog1 (princ ,obj ,stream) (terpri ,stream))))

(defconstant +mod+ 1000000007)

;; Hauptteil

(defun main ()
  (let* ((a (read))
         (b (read))
         (c (read)))
    (println
     (min c
          (floor b a)))))

#-swank(main)


;;; Utils

(in-package :cl-user)
(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
           #\# #\> (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)

(defconstant +mod+ 1000000007)

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

(in-package :cl-user)

(defmethod fast-sort ((sequence list) &optional (test #'<))
  (declare (inline sort)
           (inline sb-impl::stable-sort-list))
  (sort sequence (lambda (x y)
                   (funcall test x y))))

(defmethod fast-sort ((sequence array) &optional (test #'<))
  (declare (inline sort))
  (sort sequence (lambda (x y)
                   (funcall test x y))))

(defmacro read-numbers-to-list (size)
  `(loop repeat ,size collect (read)))

(defmacro read-numbers-to-array (size)
  `(make-array ,size :element-type 'fixnum :adjustable nil :initial-contents (read-numbers-to-list ,size)))

(defmacro read-numbers-to-board (row-size column-size)
  (let ((board (gensym))
        (r (gensym))
        (c (gensym)))
    `(let ((,board (make-array '(,row-size ,column-size))))
       (dotimes (,r ,row-size)
         (dotimes (,c ,column-size)
           (setf (aref ,board ,r ,c) (read))))
       ,board)))

(defmethod make-cumlative-sum ((sequence list))
  (labels ((inner (sequence &optional (acc '(0)))
             (if (null sequence)
                 (reverse acc)
                 (inner (rest sequence) (cons (+ (first sequence)
                                                 (first acc))
                                              acc)))))
    (inner sequence)))

(defmethod make-cumlative-sum ((sequence array))
  (declare (type (simple-array fixnum) sequence))
  (the array
       (let* ((n (length sequence))
              (acc (make-array (1+ n) :element-type 'integer :initial-element 0)))
         (loop for i below n do
              (setf (aref acc (1+ i)) (+ (aref sequence i)
                                         (aref acc i)))
            finally
              (return acc)))))

(defmethod princ-for-each-line ((sequence list))
  (labels ((inner (sequence)
             (if (null sequence)
                 (fresh-line)
                 (progn
                   (fresh-line)
                   (princ (first sequence))
                   (inner (rest sequence))))))
    (inner sequence)))

(defmethod princ-for-each-line ((sequence array))
  (dotimes (i (length sequence))
    (fresh-line)
    (princ (aref sequence i)))
  (fresh-line))

(defmacro with-buffered-stdout (&body body)
  (let ((out (gensym)))
    `(let ((,out (make-string-output-stream :element-type 'base-char)))
       (let ((*standard-output* ,out))
         ,@body)
       (write-string (get-output-stream-string ,out)))))

(defmacro minf (res cand)
  `(setf ,res (min ,res ,cand)))

;;; Write code here

(defparameter *inf* 1000000001)

(declaim (inline solve))
(defun solve (n h)
  (declare (fixnum n)
           ((array fixnum 1) h))
  (let ((memo (make-hash-table :test #'eql)))
    (declare (hash-table memo))
    (loop for i to (+ n 10) do
         (setf (gethash i memo) *inf*))
    (setf (gethash 0 memo) 0)
    (loop for i below n do
         (when (< (+ i 1) n)
           (minf (gethash (1+ i) memo)
                 (+ (gethash i memo)
                    (abs (- (aref h i)
                            (aref h (1+ i)))))))
         (when (< (+ i 2) n)
           (minf (gethash (+ i 2) memo)
                 (+ (gethash i memo)
                    (abs (- (aref h i)
                            (aref h (+ i 2))))))))
    (gethash (1- n) memo)))

(defun main ()
  (let* ((n (read))
         (h (read-numbers-to-array n)))
    (format t "~a~%" (solve n h))))

(main)

Score : 100 points

Problem Statement

There are N stones, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), the height of Stone i is h_i.

There is a frog who is initially on Stone 1.
He will repeat the following action some number of times to reach Stone N:

If the frog is currently on Stone i, jump to Stone i + 1 or Stone i + 2. Here, a cost of |h_i - h_j| is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq h_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 \ldots h_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

4
10 30 40 20

Sample Output 1

30

If we follow the path 1 → 2 → 4, the total cost incurred would be |10 - 30| + |30 - 20| = 30.

Sample Input 2

2
10 10

Sample Output 2

0

If we follow the path 1 → 2, the total cost incurred would be |10 - 10| = 0.

Sample Input 3

6
30 10 60 10 60 50

Sample Output 3

40

If we follow the path 1 → 3 → 5 → 6, the total cost incurred would be |30 - 60| + |60 - 60| + |60 - 50| = 40.


(defun calc-cost (heights a b)
  (abs (- (aref heights a)
          (aref heights b))))

(defun min-cost (heights costs n)
  (min (+ (aref costs (- n 1))
          (calc-cost heights n (- n 1)))
       (+ (aref costs (- n 2))
          (calc-cost heights n (- n 2)))))

(defun set-cost (heights costs n)
  (cond
    ((zerop n)
      (setf (aref costs 0) 0))
    ((= n 1)
      (setf (aref costs 1) (calc-cost heights 0 1)))
    (t
      (setf (aref costs n) (min-cost heights costs n)))))

(defun recursive (heights costs n to)
  (if (= n (- to 1))
      (if (= to 2)
          (calc-cost heights 0 1)
          (min-cost heights costs n))
      (progn
        (set-cost heights costs n)
        (recursive heights costs (+ n 1) to))))

(defun main ()
  (let* ((n (read))
         (heights (make-array n
                              :initial-contents (loop :repeat n :collect (read))))
         (costs (make-array n :initial-element -1)))
    (princ (recursive heights costs 0 n))))

(main)


#-swank
(unless (member :child-sbcl *features*)
  (quit
    :unix-status
    (process-exit-code
      (run-program *runtime-pathname*
                   `("--control-stack-size" "128MB"
                     "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                     "--eval" "(push :child-sbcl *features*)"
                     "--script" ,(namestring *load-pathname*))
                   :output t :error t :input t))))

(defun solve (n h)
  (declare (optimize speed))
  (let ((memo (make-array n :initial-element -1)))
    (labels ((dp (x h)
               (let (cost)
                 (cond
                   ((= x 0) 0)
                   ((< x 0) (expt 10 5))
                   ((= x 1) (abs (- (aref h 1) (aref h 0))))
                   ((/= (aref memo x) -1) (aref memo x))
                   (t   (setq cost (min (+ (dp (- x 1) h) (abs (- (aref h x)
                                                                  (aref h (- x 1)))))
                                        (+ (dp (- x 2) h) (abs (- (aref h x)
                                                                  (aref h (- x 2)))))))
                        (setf (aref memo x) cost)
                        cost)))))
      (dp (1- n) h))))

(defun main ()
  (let* ((n (read))
         (h (make-array n :initial-contents (read-from-string (concatenate 'string "(" (read-line) ")")))))
    (princ (solve n h))
    (fresh-line)))

(main)


(defun solve (n h)
  (declare (optimize speed))
  (let ((memo (make-array n :initial-element -1)))
    (labels ((dp (x h)
               (let (cost)
                 (cond
                   ((= x 0) 0)
                   ((< x 0) (expt 10 5))
                   ((= x 1) (abs (- (aref h 1) (aref h 0))))
                   ((/= (aref memo x) -1) (aref memo x))
                   (t   (setq cost (min (+ (dp (- x 1) h) (abs (- (aref h x)
                                                                  (aref h (- x 1)))))
                                        (+ (dp (- x 2) h) (abs (- (aref h x)
                                                                  (aref h (- x 2)))))))
                        (setf (aref memo x) cost)
                        cost)))))
      (dp (1- n) h))))

(defun main ()
  (let* ((n (read))
         (h (make-array n :initial-contents (read-from-string (concatenate 'string "(" (read-line) ")")))))
    (princ (solve n h))
    (fresh-line)))

(main)


(defun solve (n h)
  (let ((memo (make-array n :initial-element -1)))
    (labels ((dp (x h)
               (let (cost)
                 (cond
                   ((= x 0) 0)
                   ((< x 0) (expt 10 5))
                   ((= x 1) (abs (- (aref h 1) (aref h 0))))
                   ((/= (aref memo x) -1) (aref memo x))
                   (t   (setq cost (min (+ (dp (- x 1) h) (abs (- (aref h x)
                                                                  (aref h (- x 1)))))
                                        (+ (dp (- x 2) h) (abs (- (aref h x)
                                                                  (aref h (- x 2)))))))
                        (setf (aref memo x) cost)
                        cost)))))
      (dp (1- n) h))))

(defun main ()
  (let* ((n (read))
         (h (make-array n :initial-contents (read-from-string (concatenate 'string "(" (read-line) ")")))))
    (princ (solve n h))
    (fresh-line)))

(main)


(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
           #\# #\> (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
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  "NOTE: cannot read -2^62"
  (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 #\-))
                                  (setq 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))))))))

(in-package :cl-user)

(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-fixnum))
         (dp (make-array n :element-type 'uint32 :initial-element #xffffffff))
         (hs (make-array n :element-type 'uint32 :initial-element 0)))
    (dotimes (i n)
      (setf (aref hs i) (read-fixnum)))
    (setf (aref dp 0) 0)
    (dotimes (i (- n 1))
      (minf (aref dp (+ i 1))
            (+ (abs (- (aref hs (+ i 1)) (aref hs i))) (aref dp i)))
      (when (< i (- n 2))
        (minf (aref dp (+ i 2))
              (+ (abs (- (aref hs (+ i 2)) (aref hs i))) (aref dp i)))))
    (println (aref dp (- n 1)))))

#-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 "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 "4
10 30 40 20
"
    "30
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "2
10 10
"
    "0
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "6
30 10 60 10 60 50
"
    "40
")))


(eval-when (:compile-toplevel)
  #-swank (proclaim '(optimize (speed 3) (debug 0) (safety 0))))

(defparameter inf 2000000000)

(defun read-integer (&optional (in *standard-input*))
  (declare (inline read-byte))
  (labels ((number-char-p (b)
             (<= #.(char-code #\0) b #.(char-code #\9)))
           (minus-char-p (b)
             (= b #.(char-code #\-)))
           (to-number (b)
             (- b #.(char-code #\0))))
    (declare (inline number-char-p minus-char-p to-number))
    (macrolet ((%read-byte ()
                 '(the (unsigned-byte 8)
                       #+swank (char-code (read-char in nil #\Nul))
                       #-swank (read-byte in nil 0))))
      (let* ((minus-p nil)
             (x (loop for b = (%read-byte)
                      if (number-char-p b)
                        return (to-number b)
                        end
                      if (minus-char-p b)
                        do (setf minus-p t))))
        (declare (boolean minus-p) (fixnum x))
        (the fixnum (loop for b = (%read-byte)
                          and y = x then (+ (* y 10) (to-number b))
                          unless (number-char-p b)
                            return (funcall (if minus-p #'- #'+) y)))))))

(defun split (x str &optional (acc nil))
  (let ((pos (search x str))
        (size (length x)))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (nreverse (cons str acc)))))

(defmacro minf (field &rest x)
  `(setf ,field (min ,field ,@x)))

(defmacro maxf (field &rest x)
  `(setf ,field (max ,field ,@x)))

(defun solve (stairs n)
  (let ((dp (make-array n :element-type 'integer :initial-element inf)))
    (setf (aref dp 0) 0)
    (dotimes (i (1- n))
      (minf (aref dp (1+ i)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (1+ i))))))
      (if (< (+ i 2) n)
          (minf (aref dp (+ i 2)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (+ i 2))))))))
    (aref dp (1- n))))

(let* ((n (parse-integer (read-line)))
       (stairs (make-array n :adjustable t :fill-pointer 0)))
  (dotimes (i n)
    (vector-push (read-integer *standard-input*) stairs))
  (format t "~A~%" (solve stairs n)))


(eval-when (:compile-toplevel)
  #-swank (proclaim '(optimize (speed 3) (debug 0) (safety 0))))

(defparameter inf 2000000000)

(defun read-integer (&optional (in *standard-input*))
  (declare (inline read-byte))
  (labels ((number-char-p (b)
             (<= #.(char-code #\0) b #.(char-code #\9)))
           (minus-char-p (b)
             (= b #.(char-code #\-)))
           (to-number (b)
             (- b #.(char-code #\0))))
    (declare (inline number-char-p minus-char-p to-number))
    (macrolet ((%read-byte ()
                 '(the (unsigned-byte 8)
                       #+swank (char-code (read-char in nil #\Nul))
                       #-swank (read-byte in nil 0))))
      (let* ((minus-p nil)
             (x (loop for b = (%read-byte)
                      if (number-char-p b)
                        return (to-number b)
                        end
                      if (minus-char-p b)
                        do (setf minus-p t))))
        (declare (boolean minus-p) (fixnum x))
        (the fixnum (loop for b = (%read-byte)
                          and y = x then (+ (* y 10) (to-number b))
                          unless (number-char-p b)
                            return y))))))

(defun split (x str &optional (acc nil))
  (let ((pos (search x str))
        (size (length x)))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (nreverse (cons str acc)))))

(defmacro minf (field &rest x)
  `(setf ,field (min ,field ,@x)))

(defmacro maxf (field &rest x)
  `(setf ,field (max ,field ,@x)))

(defun solve (stairs n)
  (let ((dp (make-array n :element-type 'integer :initial-element inf)))
    (setf (aref dp 0) 0)
    (dotimes (i (1- n))
      (minf (aref dp (1+ i)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (1+ i))))))
      (if (< (+ i 2) n)
          (minf (aref dp (+ i 2)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (+ i 2))))))))
    (aref dp (1- n))))

(let* ((n (parse-integer (read-line)))
       (stairs (make-array n :adjustable t :fill-pointer 0)))
  (dotimes (i n)
    (vector-push (read-integer *standard-input*) stairs))
  (format t "~A~%" (solve stairs n)))


#-swank (eval-when (:compile-toplevel)
          (proclaim '(optimize (speed 3) (debug 0) (safety 0))))

(defparameter inf 2000000000)

(defun read-integer (&optional (in *standard-input*))
  (labels ((number-char-p (b)
             (<= #.(char-code #\0) b #.(char-code #\9)))
           (minus-char-p (b)
             (= b #.(char-code #\-)))
           (to-number (b)
             (- b #.(char-code #\0))))
    (let* ((minus-p nil)
           (x (loop for b = (read-byte in nil 0)
                    if (number-char-p b)
                      return (to-number b)
                      end
                    if (minus-char-p b)
                      do (setf minus-p t))))
      (loop for b = (read-byte in nil 0)
            and y = x then (+ (* y 10) (to-number b))
            unless (number-char-p b)
              return y))))

(defun split (x str &optional (acc nil))
  (let ((pos (search x str))
        (size (length x)))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (nreverse (cons str acc)))))

(defmacro minf (field &rest x)
  `(setf ,field (min ,field ,@x)))

(defmacro maxf (field &rest x)
  `(setf ,field (max ,field ,@x)))

(defun solve (stairs n)
  (let ((dp (make-array n :element-type 'integer :initial-element inf)))
    (setf (aref dp 0) 0)
    (dotimes (i (1- n))
      (minf (aref dp (1+ i)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (1+ i))))))
      (if (< (+ i 2) n)
          (minf (aref dp (+ i 2)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (+ i 2))))))))
    (aref dp (1- n))))

(let* ((n (parse-integer (read-line)))
       (stairs (make-array n :adjustable t :fill-pointer 0)))
  (dotimes (i n)
    (vector-push (read-integer *standard-input*) stairs))
  (format t "~A~%" (solve stairs n)))


#-swank (eval-when (:compile-toplevel)
          (proclaim '(optimize (speed 3) (debug 0) (safety 0))))

(defparameter inf 2000000000)

(defun read-integer (&optional (in *standard-input*))
  (labels ((number-char-p (b)
             (<= #.(char-code #\0) c #.(char-code #\9)))
           (minus-char-p (b)
             (= b #.(char-code #\-)))
           (to-number (b)
             (- b #.(char-code #\0))))
    (let* ((minus-p nil)
           (x (loop for b = (read-byte in)
                    if (number-char-p b)
                      return b
                      end
                    if (minus-char-p b)
                      do (setf minus-p t))))
      (loop for b = (read-byte in)
            unless (number-char-p b)
              return x
              end
            do (setf x (+ (* x 10) (to-number b))))
      x)))

(defun split (x str &optional (acc nil))
  (let ((pos (search x str))
        (size (length x)))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (nreverse (cons str acc)))))

(defmacro minf (field &rest x)
  `(setf ,field (min ,field ,@x)))

(defmacro maxf (field &rest x)
  `(setf ,field (max ,field ,@x)))

(defun solve (stairs n)
  (let ((dp (make-array n :element-type 'integer :initial-element inf)))
    (setf (aref dp 0) 0)
    (dotimes (i (1- n))
      (minf (aref dp (1+ i)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (1+ i))))))
      (if (< (+ i 2) n)
          (minf (aref dp (+ i 2)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (+ i 2))))))))
    (aref dp (1- n))))

(let* ((n (parse-integer (read-line)))
       (stairs (make-array n :adjustable t :fill-pointer 0)))
  (dotimes (i n)
    (vector-push (read-integer) stairs)))


#-swank (eval-when (:compile-toplevel)
          (proclaim '(optimize (speed 3) (debug 0) (safety 0))))

(defparameter inf 2000000000)

(defun read-integer (&optional (in *standard-input*))
  (labels ((number-char-p (b)
             (<= #.(char-code #\0) c #.(char-code #\9)))
           (minus-char-p (b)
             (= b #.(char-code #\-)))
           (to-number (b)
             (- b #.(char-code #\0))))
    (let* ((minus-p nil)
           (x (loop for b = (read-byte in)
                    if (number-char-p b)
                      return b
                      end
                    if (minus-char-p b)
                      do (setf minus-p t))))
      (loop for b = (read-byte in)
            unless (number-char-p b)
              return x
              end
            do (setf x (+ (* x 10) (to-number b)))))))

(defun split (x str &optional (acc nil))
  (let ((pos (search x str))
        (size (length x)))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (nreverse (cons str acc)))))

(defmacro minf (field &rest x)
  `(setf ,field (min ,field ,@x)))

(defmacro maxf (field &rest x)
  `(setf ,field (max ,field ,@x)))

(defun solve (stairs n)
  (let ((dp (make-array n :element-type 'integer :initial-element inf)))
    (setf (aref dp 0) 0)
    (dotimes (i (1- n))
      (minf (aref dp (1+ i)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (1+ i))))))
      (if (< (+ i 2) n)
          (minf (aref dp (+ i 2)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (+ i 2))))))))
    (aref dp (1- n))))

(let* ((n (parse-integer (read-line)))
       (stairs (make-array n :adjustable t :fill-pointer 0)))
  (dotimes (i n)
    (vector-push (read-integer) stairs)))


(eval-when (:compile-toplevel)
  (proclaim '(optimize (speed 3) (debug 3) (safety 0))))

(defparameter inf 2000000000)

(defun split (x str &optional (acc (make-array 100001 :adjustable t :fill-pointer 0)))
  (declare (type string x str) (type vector acc))
  (let ((pos (search x str))
        (size (length x)))
    (declare (type integer size))
    (if pos
        (progn (vector-push (subseq str 0 pos) acc)
               (split x (subseq str (+ pos size)) acc))
        (progn (vector-push str acc)
               (the vector acc)))))

(let* ((n (parse-integer (read-line)))
       (stairs (map 'vector #'parse-integer (split " " (read-line)))))
  (format t "~A~%" stairs))


(eval-when (:compile-toplevel)
  (proclaim '(optimize (speed 3) (debug 3) (safety 3))))

(defparameter inf 2000000000)

(defun split (x str &optional (acc (make-array 100001 :adjustable t :fill-pointer 0)))
  (declare (type string x str) (type vector acc))
  (let ((pos (search x str))
        (size (length x)))
    (declare (type integer size))
    (if pos
        (progn (vector-push (subseq str 0 pos) acc)
               (split x (subseq str (+ pos size)) acc))
        (progn (vector-push str acc)
               (the vector acc)))))

(defmacro minf (field &rest x)
  `(setf ,field (min ,field ,@x)))

(defmacro maxf (field &rest x)
  `(setf ,field (max ,field ,@x)))

(defun solve (stairs n)
  (let ((dp (make-array n :initial-element inf)))
    (setf (aref dp 0) 0)
    (dotimes (i (1- n))
      (format t "i: ~A~%" i)
      (minf (aref dp (+ i 1)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (+ i 1))))))
      (if (< (+ i 2) n)
          (minf (aref dp (+ i 2)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (+ i 2)))))))
    (aref dp (1- n)))))

(let* ((n (parse-integer (read-line)))
       (stairs (map 'vector #'parse-integer (split " " (read-line)))))
  (format t "~A~%" (solve stairs n)))


(eval-when (:compile-toplevel)
  (proclaim '(optimize (speed 3) (debug 3) (safety 0))))

(defparameter inf 2000000000)

(defun split (x str &optional (acc (make-array 100001 :adjustable t :fill-pointer 0)))
  (declare (type string x str) (type vector acc))
  (let ((pos (search x str))
        (size (length x)))
    (declare (type integer size))
    (if pos
        (progn (vector-push (subseq str 0 pos) acc)
               (split x (subseq str (+ pos size)) acc))
        (progn (vector-push str acc)
               (the vector acc)))))

(defmacro minf (field &rest x)
  `(setf ,field (min ,field ,@x)))

(defmacro maxf (field &rest x)
  `(setf ,field (max ,field ,@x)))

(defun solve (stairs n)
  (let ((dp (make-array n :initial-element inf)))
    (setf (aref dp 0) 0)
    (dotimes (i n)
      (let ((a nil)
            (b nil))
        (if (> i 0)
            (setf a (+ (aref dp (- i 1)) (abs (- (aref stairs i) (aref stairs (- i 1)))))))
        (if (> i 1)
            (setf b (+ (aref dp (- i 2)) (abs (- (aref stairs i) (aref stairs (- i 2)))))))
        (setf (aref dp i) (min a b))))
    (aref dp (1- n))))

(let* ((n (parse-integer (read-line)))
       (stairs (map 'vector #'parse-integer (split " " (read-line)))))
  (format t "~A~%" (solve stairs n)))


(eval-when (:compile-toplevel)
  (proclaim '(optimize (speed 3) (debug 3) (safety 0))))

(defparameter inf 2000000000)

(defun split (x str &optional (acc (make-array 100001 :adjustable t :fill-pointer 0)))
  (declare (type string x str) (type vector acc))
  (let ((pos (search x str))
        (size (length x)))
    (declare (type integer size))
    (if pos
        (progn (vector-push (subseq str 0 pos) acc)
               (split x (subseq str (+ pos size)) acc))
        (progn (vector-push str acc)
               (the vector acc)))))

(defmacro minf (field &rest x)
  `(setf ,field (min ,field ,@x)))

(defmacro maxf (field &rest x)
  `(setf ,field (max ,field ,@x)))

(defun solve (stairs n)
  (let ((dp (make-array n :element-type 'integer :initial-element inf)))
    (setf (aref dp 0) 0)
    (dotimes (i (1- n))
      (minf (aref dp (1+ i)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (1+ i))))))
      (if (< (+ i 2) n)
          (minf (aref dp (+ i 2)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (+ i 2))))))))
    (aref dp (1- n))))

(let* ((n (parse-integer (read-line)))
       (stairs (map 'vector #'parse-integer (split " " (read-line)))))
  (format t "~A~%" (solve stairs n)))


(eval-when (:compile-toplevel)
  (proclaim '(optimize (speed 3) (debug 3) (safety 0))))

(defparameter inf 2000000000)

(defun split (x str &optional (acc (make-array 0 :adjustable t :fill-pointer 0)))
  (declare (type string x str) (type vector acc))
  (let ((pos (search x str))
        (size (length x)))
    (declare (type integer size))
    (if pos
        (progn (vector-push-extend (subseq str 0 pos) acc)
               (split x (subseq str (+ pos size)) acc))
        (progn (vector-push-extend str acc)
               (the vector acc)))))

(defmacro minf (field &rest x)
  `(setf ,field (min ,field ,@x)))

(defmacro maxf (field &rest x)
  `(setf ,field (max ,field ,@x)))

(defun solve (stairs n)
  (let ((dp (make-array n :element-type 'integer :initial-element inf)))
    (setf (aref dp 0) 0)
    (dotimes (i (1- n))
      (minf (aref dp (1+ i)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (1+ i))))))
      (if (< (+ i 2) n)
          (minf (aref dp (+ i 2)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (+ i 2))))))))
    (aref dp (1- n))))

(let* ((n (parse-integer (read-line)))
       (stairs (map 'vector #'parse-integer (split " " (read-line)))))
  (format t "~A~%" (solve stairs n)))


(eval-when (:compile-toplevel)
  (proclaim '(optimize (speed 3) (debug 0) (safety 0))))

(defparameter inf 2000000000)

(defun split (x str &optional (acc nil))
  (declare (type string x str) (type list acc))
  (let ((pos (search x str))
        (size (length x)))
    (declare (type integer size))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (the list (reverse (cons str acc))))))

(defmacro minf (field &rest x)
  `(setf ,field (min ,field ,@x)))

(defmacro maxf (field &rest x)
  `(setf ,field (max ,field ,@x)))

(defun solve (stairs n)
  (declare (type integer n) (type simple-vector stairs))
  (let ((dp (make-array n :element-type 'integer :initial-element inf)))
    (declare (type simple-vactor dp))
    (setf (aref dp 0) 0)
    (dotimes (i (1- n))
      (declare (type integer i))
      (minf (aref dp (1+ i)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (1+ i))))))
      (if (< (+ i 2) n)
          (minf (aref dp (+ i 2)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (+ i 2))))))))
    (the integer (aref dp (1- n)))))

(let* ((n (parse-integer (read-line)))
       (stairs (make-array n :element-type 'integer :initial-contents (mapcar #'parse-integer (split " " (read-line))))))
  (declare (type fixnum n) (type simple-vector stairs))
  (format t "~A~%" (solve stairs n)))


(declaim (optimize (speed 3) (debug 0) (safety 0)))

(defparameter inf 2000000000)

(defun split (x str &optional (acc nil))
  (declare (type string x str) (type list acc))
  (let ((pos (search x str))
        (size (length x)))
    (declare (type integer size))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (the list (reverse (cons str acc))))))

(defmacro minf (field &rest x)
  `(setf ,field (min ,field ,@x)))

(defmacro maxf (field &rest x)
  `(setf ,field (max ,field ,@x)))

(defun solve (stairs n)
  (declare (type integer n) (type simple-vector stairs))
  (let ((dp (make-array n :element-type 'integer :initial-element inf)))
    (declare (type simple-vactor dp))
    (setf (aref dp 0) 0)
    (dotimes (i (1- n))
      (declare (type integer i))
      (minf (aref dp (1+ i)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (1+ i))))))
      (if (< (+ i 2) n)
          (minf (aref dp (+ i 2)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (+ i 2))))))))
    (the integer (aref dp (1- n)))))

(let* ((n (read))
       (stairs (make-array n :element-type 'integer :initial-contents (mapcar #'parse-integer (split " " (read-line))))))
  (declare (type fixnum n) (type simple-vector stairs))
  (format t "~A~%" (solve stairs n)))


(declaim (optimize (speed 3) (debug 0) (safety 0)))

(defparameter mod-number 1000000007)
(defparameter inf 2000000000)

(defun unfold (p f g seed &optional (tail-gen (lambda () '())))
  (if (p seed)
      (tail-gen seed)
	  (cons (f seed)
	        (unfold p f g (g seed) tail-gen))))

(defun iota (c &optional (s 0) (step 1) (acc nil))
  (if (zerop c)
      (reverse acc)
      (iota (1- c) (+ s step) step (cons s acc))))

(defun sum (list &optional (init 0))
  (reduce #'+ list :initial-value init))

(defun chomp (str)
    (string-right-trim '(#\Return #\Linefeed) str))

(defun split (x str &optional (acc nil))
  (let ((pos (search x str))
        (size (length x)))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (reverse (cons str acc)))))

(defmacro minf (field x)
  `(setf ,field (min ,field ,x)))

(defmacro maxf (field x)
  `(setf ,field (max ,field ,x)))

(defmacro debug-print (x)
  `(let ((y ,x))
    (format t "~A: ~A~%" ',x y)
	y))

(defun solve (stairs n)
  (declare (type integer n) (type simple-vector stairs))
  (let ((dp (make-array n :element-type 'integer :initial-element inf)))
    (declare (type simple-vactor dp))
    (setf (aref dp 0) 0)
    (dotimes (i (1- n))
      (declare (type integer i))
      (minf (aref dp (1+ i)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (1+ i))))))
      (if (< (+ i 2) n)
          (minf (aref dp (+ i 2)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (+ i 2))))))))
    (the integer (aref dp (1- n)))))

(let* ((n (read))
       (stairs (make-array n :element-type 'integer :initial-contents (mapcar #'parse-integer (split " " (read-line))))))
  (declare (type fixnum n) (type simple-vector stairs))
  (format t "~A~%" (solve stairs n)))


(defparameter mod-number 1000000007)
(defparameter inf 2000000000)

(defun unfold (p f g seed &optional (tail-gen (lambda () '())))
  (if (p seed)
      (tail-gen seed)
	  (cons (f seed)
	        (unfold p f g (g seed) tail-gen))))

(defun iota (c &optional (s 0) (step 1) (acc nil))
  (if (zerop c)
      (reverse acc)
      (iota (1- c) (+ s step) step (cons s acc))))

(defun sum (list &optional (init 0))
  (reduce #'+ list :initial-value init))

(defun chomp (str)
    (string-right-trim '(#\Return #\Linefeed) str))

(defun split (x str &optional (acc nil))
  (let ((pos (search x str))
        (size (length x)))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (reverse (cons str acc)))))

(defmacro minf (field x)
  `(setf ,field (min ,field ,x)))

(defmacro maxf (field x)
  `(setf ,field (max ,field ,x)))

(defmacro debug-print (x)
  `(let ((y ,x))
    (format t "~A: ~A~%" ',x y)
	y))

(declaim (ftype (function (simple-vector integer) integer) solve))
(defun solve (stairs n)
  (declaim (simple-vector dp))
  (let ((dp (make-array n :initial-element inf)))
    (setf (aref dp 0) 0)
    (dotimes (i (1- n))
      (minf (aref dp (1+ i)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (1+ i))))))
      (if (< (+ i 2) n)
          (minf (aref dp (+ i 2)) (+ (aref dp i) (abs (- (aref stairs i) (aref stairs (+ i 2))))))))
    (the integer (aref dp (1- n)))))

(let* ((n (read))
       (stairs (make-array n :initial-contents (mapcar #'parse-integer (split " " (read-line))))))
  (format t "~A~%" (solve stairs n)))


(defparameter mod-number 1000000007)
(defparameter inf 2000000000)

(defun unfold (p f g seed &optional (tail-gen (lambda () '())))
  (if (p seed)
      (tail-gen seed)
	  (cons (f seed)
	        (unfold p f g (g seed) tail-gen))))

(defun iota (c &optional (s 0) (step 1) (acc nil))
  (if (zerop c)
      (reverse acc)
      (iota (1- c) (+ s step) step (cons s acc))))

(defun sum (list &optional (init 0))
  (reduce #'+ list :initial-value init))

(defun chomp (str)
    (string-right-trim '(#\Return #\Linefeed) str))

(defun split (x str &optional (acc nil))
  (let ((pos (search x str))
        (size (length x)))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (reverse (cons str acc)))))

(defmacro debug-print (x)
  `(let ((y ,x))
    (format t "~A: ~A~%" ',x y)
	y))

(defun memorized-solve (stairs)
  (let ((dp (make-hash-table :test #'eq)))
    (labels ((solve (pos)
               (let ((memo (gethash pos dp)))
                 (if memo
                     (the integer memo)
                     (let ((ret (if (zerop pos)
                                    0
                                    (min (if (minusp (1- pos))
                                             inf
                                             (+ (solve (1- pos))
                                                (abs (- (aref stairs pos) (aref stairs (1- pos))))))
                                         (if (minusp (- pos 2))
                                             inf
                                             (+ (solve (- pos 2))
                                                (abs (- (aref stairs pos) (aref stairs (- pos 2))))))))))
                       (setf (gethash pos dp) ret)
                       (the integer ret))))))
      #'solve)))

(let* ((n (read))
       (stairs (make-array n :initial-contents (mapcar #'parse-integer (split " " (read-line))))))
  (format t "~A~%" (funcall (memorized-solve stairs) (1- n))))