(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
(macrolet ((def (name fname)
             `(define-modify-macro ,name (new-value) ,fname)))
  (def minf min)
  (def maxf max)
  (def mulf *)
  (def divf /)
  (def iorf logior)
  (def xorf logxor)
  (def andf logand))

;;;
;;; Disjoint set (union by size & path compression)
;;;

(defstruct (disjoint-set
            (:constructor make-disjoint-set
                (size &aux (data (make-array size :element-type 'fixnum :initial-element -1))))
            (:conc-name ds-))
  (data nil :type (simple-array fixnum (*))))

(declaim (inline ds-root))
(defun ds-root (disjoint-set x)
  "Returns the root of X."
  (declare ((mod #.array-total-size-limit) x))
  (let ((data (ds-data disjoint-set)))
    (labels ((recur (x)
               (if (< (aref data x) 0)
                   x
                   (setf (aref data x)
                         (recur (aref data x))))))
      (recur x))))

(declaim (inline ds-unite!))
(defun ds-unite! (disjoint-set x1 x2)
  "Destructively unites X1 and X2 and returns true iff X1 and X2 become
connected for the first time."
  (let ((root1 (ds-root disjoint-set x1))
        (root2 (ds-root disjoint-set x2)))
    (unless (= root1 root2)
      (let ((data (ds-data disjoint-set)))
        ;; ensure the size of root1 >= the size of root2
        (when (> (aref data root1) (aref data root2))
          (rotatef root1 root2))
        (incf (aref data root1) (aref data root2))
        (setf (aref data root2) root1)))))

(declaim (inline ds-connected-p))
(defun ds-connected-p (disjoint-set x1 x2)
  "Returns true iff X1 and X2 have the same root."
  (= (ds-root disjoint-set x1) (ds-root disjoint-set x2)))

(declaim (inline ds-size))
(defun ds-size (disjoint-set x)
  "Returns the size of the connected component to which X belongs."
  (- (aref (ds-data disjoint-set)
           (ds-root disjoint-set x))))

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

(defconstant +neg-inf+ #x-80000000)

(defun solve-small (s)
  (declare (simple-bit-vector s))
  (let ((n (length s))
        (max 0))
    (sb-int:named-let dfs ((depth 0))
      (maxf max depth)
      (dotimes (i (- n 2))
        (when (and (= 1 (aref s i))
                   (= 0 (aref s (+ i 1)))
                   (= 1 (aref s (+ i 2))))
          (setf (aref s i) 0
                (aref s (+ i 1)) 1
                (aref s (+ i 2)) 0)
          (dfs (+ depth 1))
          (setf (aref s i) 1
                (aref s (+ i 1)) 0
                (aref s (+ i 2)) 1))))
    max))

(defun solve (s)
  (declare (simple-bit-vector s))
  (let* ((n (length s))
         (dset (make-disjoint-set n)))
    (when (<= n 2)
      (return-from solve 0))
    (dotimes (i (- n 1))
      (when (= 1 (aref s i) (aref s (+ i 1)))
        (ds-unite! dset i (+ i 1))))
    (let ((consecutive-p (make-array n :element-type 'bit :initial-element 0))
          (l-sizes (make-array n :element-type 'uint31 :initial-element 0))
          (r-sizes (make-array n :element-type 'uint31 :initial-element 0))
          (prev +neg-inf+)
          (end 0))
      (dotimes (i (- n 2))
        (when (and (= 1 (aref s i))
                   (= 0 (aref s (+ i 1)))
                   (= 1 (aref s (+ i 2))))
          (setf (aref l-sizes end) (ds-size dset i)
                (aref r-sizes end) (ds-size dset (+ i 2)))
          (when (= prev (- i (aref l-sizes end) 1))
            (setf (aref consecutive-p end) 1))
          (incf end)
          (setq prev i)))
      ;; (dbg consecutive-p l-sizes r-sizes)
      ;; 0: 取っていない
      ;; 1: 左に向かって取れるだけ取る
      ;; 2: 右に向かって取れるだけ取る
      ;; 3: 右に向かって1個残して取る
      (let ((dp (make-array (list (+ end 1) 4) :element-type 'int32 :initial-element +neg-inf+)))
        (setf (aref dp 0 0) 0)
        (dotimes (x end)
          ;; 1つ前で取っていない
          (let ((current (aref dp x 0)))
            (when (>= current 0)
              (maxf (aref dp (+ x 1) 0) current)
              (maxf (aref dp (+ x 1) 1) (+ (aref l-sizes x) current))
              (maxf (aref dp (+ x 1) 2) (+ (aref r-sizes x) current))
              (maxf (aref dp (+ x 1) 3) (+ (aref r-sizes x) -1 current))))
          ;; 1つ前で左に向かって取った
          (let ((current (aref dp x 1)))
            (when (>= current 0)
              (maxf (aref dp (+ x 1) 0) current)
              (if (= 1 (aref consecutive-p x))
                  (maxf (aref dp (+ x 1) 1) (+ (aref l-sizes x) -1 current))
                  (maxf (aref dp (+ x 1) 1) (+ (aref l-sizes x) current)))
              (unless (and (= 1 (aref consecutive-p x))
                           (= 1 (aref l-sizes x)))
                (maxf (aref dp (+ x 1) 2) (+ (aref r-sizes x) current))
                (maxf (aref dp (+ x 1) 3) (+ (aref r-sizes x) -1 current)))))
          ;; 1つ前で右に向かってすべて取った
          (let ((current (aref dp x 2)))
            (when (>= current 0)
              (maxf (aref dp (+ x 1) 0) current)
              (unless (= 1 (aref consecutive-p x))
                (maxf (aref dp (+ x 1) 1) (+ (aref l-sizes x) current))
                (maxf (aref dp (+ x 1) 2) (+ (aref r-sizes x) current))
                (maxf (aref dp (+ x 1) 3) (+ (aref r-sizes x) -1 current)))))
          ;; 1つ前で右に向かって1つ残して取った
          (let ((current (aref dp x 3)))
            (when (>= current 0)
              (maxf (aref dp (+ x 1) 0) current)
              (if (= 1 (aref consecutive-p x))
                  (progn
                    (maxf (aref dp (+ x 1) 1) (+ 1 current))
                    (maxf (aref dp (+ x 1) 2) (+ (aref r-sizes x) current))
                    (maxf (aref dp (+ x 1) 3) (+ (aref r-sizes x) -1 current)))
                  (progn
                    (maxf (aref dp (+ x 1) 1) (+ (aref l-sizes x) current))
                    (maxf (aref dp (+ x 1) 2) (+ (aref r-sizes x) current))
                    (maxf (aref dp (+ x 1) 3) (+ (aref r-sizes x) -1 current)))))))
        (max (aref dp end 0)
             (aref dp end 1)
             (aref dp end 2)
             (aref dp end 3))))))

(defun random-test (n sample)
  (dotimes (_ sample)
    (let ((s (make-array n :element-type 'bit :initial-element 0)))
      (dotimes (i n)
        (setf (aref s i) (random 2)))
      (unless (= (solve-small s) (solve s))
        (return-from random-test s)))))
(defun main ()
  (let* ((n (read))
         (s (map 'simple-bit-vector #'digit-char-p (read-line))))
    (println (solve s))))

#-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)
    #+os-windows (run-program "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)
    #+os-unix (run-program "xsel" '("-b" "-o") :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 "7
1010101
"
    "2
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "50
10101000010011011110001001111110000101010111100110
"
    "10
")))


Measured memory_bytes: 36320