Problem:
Score : 600 points

Problem Statement

Given are two integer sequences of N elements each: A_1,...,A_N and B_1,...,B_N.
Determine if it is possible to do the following operation at most N-2 times (possibly zero) so that, for every integer i from 1 to N, A_i \leq B_i holds:

Choose two distinct integers x and y between 1 and N (inclusive), and swap the values of A_x and A_y.

Constraints

2 \leq N \leq 10^5

1 \leq A_i,B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

Output

If the objective is achievable, print Yes; if it is not, print No.

Sample Input 1

3
1 3 2
1 2 3

Sample Output 1

Yes

We should swap the values of A_2 and A_3.

Sample Input 2

3
1 2 3
2 2 2

Sample Output 2

No

Sample Input 3

6
3 1 2 6 3 4
2 2 8 3 4 3

Sample Output 3

Yes

Solution in Common Lisp (SBCL 1.1.14) (judge verdict: Wrong Answer)

;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           #\# #\> (lambda (s c p) (declare (ignore c p)) (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

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

;;;
;;; Implicit treap
;;; (treap with implicit key)
;;;

;; Note:
;; - An empty treap is NIL.

(declaim (inline op))
(defun op (a b)
  "Is a binary operator comprising a monoid."
  (declare (uint62 a b))
  (min a b))

(defconstant +op-identity+ most-positive-fixnum
  "identity element w.r.t. OP")

(defstruct (itreap (:constructor %make-itreap (value priority &key left right (count 1) (accumulator value)))
                  (:copier nil)
                  (:conc-name %itreap-))
  (value +op-identity+ :type fixnum)
  (accumulator +op-identity+ :type fixnum)
  (priority 0 :type (integer 0 #.most-positive-fixnum))
  (count 1 :type (integer 0 #.most-positive-fixnum)) ; size of (sub)treap
  (left nil :type (or null itreap))
  (right nil :type (or null itreap)))

(declaim (inline itreap-count))
(defun itreap-count (itreap)
  "Returns the number of the elements."
  (declare ((or null itreap) itreap))
  (if itreap
      (%itreap-count itreap)
      0))

(declaim (inline itreap-accumulator))
(defun itreap-accumulator (itreap)
  "Returns the sum (w.r.t. OP) of the whole ITREAP:
ITREAP[0]+ITREAP[1]+...+ITREAP[SIZE-1]."
  (declare ((or null itreap) itreap))
  (if itreap
      (%itreap-accumulator itreap)
      +op-identity+))

(declaim (inline update-count))
(defun update-count (itreap)
  (declare (itreap itreap))
  (setf (%itreap-count itreap)
        (+ 1
           (itreap-count (%itreap-left itreap))
           (itreap-count (%itreap-right itreap)))))

(declaim (inline update-accumulator))
(defun update-accumulator (itreap)
  (declare (itreap itreap))
  (setf (%itreap-accumulator itreap)
        (if (%itreap-left itreap)
            (if (%itreap-right itreap)
                (let ((mid (op (%itreap-accumulator (%itreap-left itreap))
                               (%itreap-value itreap))))
                  (op mid (%itreap-accumulator (%itreap-right itreap))))
                (op (%itreap-accumulator (%itreap-left itreap))
                    (%itreap-value itreap)))
            (if (%itreap-right itreap)
                (op (%itreap-value itreap)
                    (%itreap-accumulator (%itreap-right itreap)))
                (%itreap-value itreap)))))

(declaim (inline force-up))
(defun force-up (itreap)
  "Propagates up the information from children."
  (declare (itreap itreap))
  (update-count itreap)
  (update-accumulator itreap))

(defun %heapify (top)
  "Properly swaps the priorities of the node and its two children."
  (declare (optimize (speed 3) (safety 0)))
  (when top
    (let ((high-priority-node top))
      (when (and (%itreap-left top)
                 (> (%itreap-priority (%itreap-left top))
                    (%itreap-priority high-priority-node)))
        (setq high-priority-node (%itreap-left top)))
      (when (and (%itreap-right top)
                 (> (%itreap-priority (%itreap-right top))
                    (%itreap-priority high-priority-node)))
        (setq high-priority-node (%itreap-right top)))
      (unless (eql high-priority-node top)
        (rotatef (%itreap-priority high-priority-node)
                 (%itreap-priority top))
        (%heapify high-priority-node)))))

(declaim (inline make-itreap))
(defun make-itreap (size &key initial-contents)
  "Makes a treap of SIZE in O(SIZE) time. Its values are filled with the
identity element unless INITIAL-CONTENTS are supplied."
  (declare ((or null vector) initial-contents))
  (labels ((build (l r)
             (declare ((integer 0 #.most-positive-fixnum) l r))
             (if (= l r)
                 nil
                 (let* ((mid (ash (+ l r) -1))
                        (node (%make-itreap (if initial-contents
                                                (aref initial-contents mid)
                                                +op-identity+)
                                           (random most-positive-fixnum))))
                   (setf (%itreap-left node) (build l mid))
                   (setf (%itreap-right node) (build (+ mid 1) r))
                   (%heapify node)
                   (force-up node)
                   node))))
    (build 0 size)))

(defun itreap-split (itreap index)
  "Destructively splits the ITREAP into two nodes [0, INDEX) and [INDEX, N),
where N is the number of elements of the ITREAP."
  (declare (optimize (speed 3))
           ((integer 0 #.most-positive-fixnum) index))
  (unless (<= index (itreap-count itreap))
    (error 'invalid-itreap-index-error :index index :itreap itreap))
  (labels ((recur (itreap ikey)
             (unless itreap
               (return-from itreap-split (values nil nil)))
             (let ((left-count (itreap-count (%itreap-left itreap))))
               (if (<= ikey left-count)
                   (multiple-value-bind (left right)
                       (itreap-split (%itreap-left itreap) ikey)
                     (setf (%itreap-left itreap) right)
                     (force-up itreap)
                     (values left itreap))
                   (multiple-value-bind (left right)
                       (itreap-split (%itreap-right itreap) (- ikey left-count 1))
                     (setf (%itreap-right itreap) left)
                     (force-up itreap)
                     (values itreap right))))))
    (recur itreap index)))

(defun itreap-merge (left right)
  "Destructively concatenates two ITREAPs."
  (declare #.OPT
           ((or null itreap) left right))
  (cond ((null left) (when right (force-up right)) right)
        ((null right) (when left (force-up left)) left)
        (t (if (> (%itreap-priority left) (%itreap-priority right))
               (progn
                 (setf (%itreap-right left)
                       (itreap-merge (%itreap-right left) right))
                 (force-up left)
                 left)
               (progn
                 (setf (%itreap-left right)
                       (itreap-merge left (%itreap-left right)))
                 (force-up right)
                 right)))))

(declaim (inline itreap-map))
(defun itreap-map (function itreap)
  "Successively applies FUNCTION to ITREAP[0], ..., ITREAP[SIZE-1]."
  (declare (function function))
  (labels ((recur (node)
             (when node
               (recur (%itreap-left node))
               (funcall function (%itreap-value node))
               (recur (%itreap-right node))
               (force-up node))))
    (recur itreap)))

(defmethod print-object ((object itreap) stream)
  (print-unreadable-object (object stream :type t)
    (let ((init t))
      (itreap-map (lambda (x)
                    (if init
                        (setq init nil)
                        (write-char #\  stream))
                   (write x :stream stream))
                 object))))

(declaim (inline itreap-ref))
(defun itreap-ref (itreap index)
  "Returns the element ITREAP[INDEX]."
  (declare ((integer 0 #.most-positive-fixnum) index))
  (unless (< index (itreap-count itreap))
    (error 'invalid-itreap-index-error :itreap itreap :index index))
  (labels ((%ref (itreap index)
             (declare ((integer 0 #.most-positive-fixnum) index))
             (prog1
                 (let ((left-count (itreap-count (%itreap-left itreap))))
                   (cond ((< index left-count)
                          (%ref (%itreap-left itreap) index))
                         ((> index left-count)
                          (%ref (%itreap-right itreap) (- index left-count 1)))
                         (t (%itreap-value itreap))))
               (force-up itreap))))
    (%ref itreap index)))

(declaim (inline (setf itreap-ref)))
(defun (setf itreap-ref) (new-value itreap index)
  "Sets ITREAP[INDEX] to the given value."
  (declare ((integer 0 #.most-positive-fixnum) index))
  (unless (< index (itreap-count itreap))
    (error 'invalid-itreap-index-error :itreap itreap :index index))
  (labels ((%set (itreap index)
             (declare ((integer 0 #.most-positive-fixnum) index))
             (prog1
                 (let ((left-count (itreap-count (%itreap-left itreap))))
                   (cond ((< index left-count)
                          (%set (%itreap-left itreap) index))
                         ((> index left-count)
                          (%set (%itreap-right itreap) (- index left-count 1)))
                         (t (setf (%itreap-value itreap) new-value))))
               (force-up itreap))))
    (%set itreap index)
    new-value))

(declaim (inline itreap-query))
(defun itreap-query (itreap l r)
  "Queries the `sum' (w.r.t. OP) of the range ITREAP[L, R)."
  (declare ((integer 0 #.most-positive-fixnum) l r))
  (unless (<= l r (itreap-count itreap))
    (error 'invalid-itreap-index-error :itreap itreap :index (cons l r)))
  (labels
      ((recur (itreap l r)
         (declare ((integer 0 #.most-positive-fixnum) l r))
         (unless itreap
           (return-from recur +op-identity+))
         (prog1
             (if (and (zerop l) (= r (%itreap-count itreap)))
                 (itreap-accumulator itreap)
                 (let ((left-count (itreap-count (%itreap-left itreap))))
                   (if (<= l left-count)
                       (if (< left-count r)
                           ;; LEFT-COUNT is in [L, R)
                           (op (op (recur (%itreap-left itreap) l (min r left-count))
                                   (%itreap-value itreap))
                               (recur (%itreap-right itreap) 0 (- r left-count 1)))
                           ;; LEFT-COUNT is in [R, END)
                           (recur (%itreap-left itreap) l (min r left-count)))
                       ;; LEFT-COUNT is in [0, L)
                       (recur (%itreap-right itreap) (- l left-count 1) (- r left-count 1)))))
           (force-up itreap))))
    (recur itreap l r)))

;; FIXME: might be problematic when two priorities collide and START is not
;; zero. (It will be negligible from the viewpoint of probability, however.)
(declaim (inline itreap-range-bisect-left))
(defun itreap-range-bisect-left (itreap value order &optional (start 0))
  "Returns the smallest index that satisfies ITREAP[START]+ ITREAP[START+1] +
... + ITREAP[index] >= VALUE (if ORDER is #'<).

Note:
- This function handles a **closed** interval.
- This function returns the length of ITREAP instead if ITREAP[START]+
... +ITREAP[length-1] < VALUE.
- The prefix sums of ITREAP, (ITREAP[START], ITREAP[START]+ITREAP[START+1], ...)
  must be monotone w.r.t. ORDER.
- ORDER must be a strict order"
  (declare ((integer 0 #.most-positive-fixnum) start))
  (multiple-value-bind (itreap-prefix itreap)
      (if (zerop start)
          (values nil itreap)
          (itreap-split itreap start))
    (labels
        ((recur (itreap offset prev-sum)
           (declare ((integer 0 #.most-positive-fixnum) offset)
                    #+sbcl (values (integer 0 #.most-positive-fixnum)))
           (unless itreap
             (return-from recur offset))
           (let ((sum prev-sum))
             (prog1
                 (cond ((not (funcall order
                                      (setq sum (op sum (itreap-accumulator (%itreap-left itreap))))
                                      value))
                        (recur (%itreap-left itreap) offset prev-sum))
                       ((not (funcall order
                                      (setq sum (op sum (%itreap-value itreap)))
                                      value))
                        (+ offset (itreap-count (%itreap-left itreap))))
                       (t
                        (recur (%itreap-right itreap)
                               (+ offset (itreap-count (%itreap-left itreap)) 1)
                               sum)))
               (force-up itreap)))))
      (prog1 (+ start (recur itreap 0 +op-identity+))
        (itreap-merge itreap-prefix itreap)))))

(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
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setf minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48)
                              (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result))))
              (return (if minus (- result) result))))))))

(declaim (inline delete-adjacent-duplicates))
(defun delete-adjacent-duplicates (seq &key (test #'eql))
  "Destructively deletes adjacent duplicates of SEQ: e.g. #(1 1 1 2 2 1 3) ->
#(1 2 1 3)"
  (declare (sequence seq)
           (function test))
  (etypecase seq
    (vector
     (if (zerop (length seq))
         seq
         (let ((prev (aref seq 0))
               (end 1))
           (loop for pos from 1 below (length seq)
                 unless (funcall test prev (aref seq pos))
                 do (setf prev (aref seq pos)
                          (aref seq end) (aref seq pos)
                          end (+ 1 end)))
           ;; KLUDGE: Resorting to ADJUST-ARRAY is maybe substandard. 
           (if (array-has-fill-pointer-p seq)
               (adjust-array seq end :fill-pointer end)
               (adjust-array seq end)))))
    (list
     (loop for rest on seq
           unless (and (cdr rest)
                       (funcall test (first rest) (second rest)))
           collect (car rest)))))

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

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


;; Note:
;; - An empty treap is NIL.

(declaim (inline mop))
(defun mop (a b)
  "Is a binary moperator comprising a monoid."
  (declare (uint62 a b))
  (max a b))

(defconstant +mop-identity+ 0
  "identity element w.r.t. MOP")

(defstruct (mitreap (:constructor %make-mitreap (value priority &key left right (count 1) (accumulator value)))
                  (:copier nil)
                  (:conc-name %mitreap-))
  (value +mop-identity+ :type fixnum)
  (accumulator +mop-identity+ :type fixnum)
  (priority 0 :type (integer 0 #.most-positive-fixnum))
  (count 1 :type (integer 0 #.most-positive-fixnum)) ; size of (sub)treap
  (left nil :type (or null mitreap))
  (right nil :type (or null mitreap)))

(declaim (inline mitreap-count))
(defun mitreap-count (mitreap)
  "Returns the number of the elements."
  (declare ((or null mitreap) mitreap))
  (if mitreap
      (%mitreap-count mitreap)
      0))

(declaim (inline mitreap-accumulator))
(defun mitreap-accumulator (mitreap)
  "Returns the sum (w.r.t. MOP) of the whole MITREAP:
MITREAP[0]+MITREAP[1]+...+MITREAP[SIZE-1]."
  (declare ((or null mitreap) mitreap))
  (if mitreap
      (%mitreap-accumulator mitreap)
      +mop-identity+))

(declaim (inline mupdate-count))
(defun mupdate-count (mitreap)
  (declare (mitreap mitreap))
  (setf (%mitreap-count mitreap)
        (+ 1
           (mitreap-count (%mitreap-left mitreap))
           (mitreap-count (%mitreap-right mitreap)))))

(declaim (inline mupdate-accumulator))
(defun mupdate-accumulator (mitreap)
  (declare (mitreap mitreap))
  (setf (%mitreap-accumulator mitreap)
        (if (%mitreap-left mitreap)
            (if (%mitreap-right mitreap)
                (let ((mid (mop (%mitreap-accumulator (%mitreap-left mitreap))
                               (%mitreap-value mitreap))))
                  (mop mid (%mitreap-accumulator (%mitreap-right mitreap))))
                (mop (%mitreap-accumulator (%mitreap-left mitreap))
                    (%mitreap-value mitreap)))
            (if (%mitreap-right mitreap)
                (mop (%mitreap-value mitreap)
                    (%mitreap-accumulator (%mitreap-right mitreap)))
                (%mitreap-value mitreap)))))

(declaim (inline mforce-up))
(defun mforce-up (mitreap)
  "Propagates up the information from children."
  (declare (mitreap mitreap))
  (mupdate-count mitreap)
  (mupdate-accumulator mitreap))

(defun %mheapify (top)
  "Properly swaps the priorities of the node and its two children."
  (declare (optimize (speed 3) (safety 0)))
  (when top
    (let ((high-priority-node top))
      (when (and (%mitreap-left top)
                 (> (%mitreap-priority (%mitreap-left top))
                    (%mitreap-priority high-priority-node)))
        (setq high-priority-node (%mitreap-left top)))
      (when (and (%mitreap-right top)
                 (> (%mitreap-priority (%mitreap-right top))
                    (%mitreap-priority high-priority-node)))
        (setq high-priority-node (%mitreap-right top)))
      (unless (eql high-priority-node top)
        (rotatef (%mitreap-priority high-priority-node)
                 (%mitreap-priority top))
        (%mheapify high-priority-node)))))

(declaim (inline make-mitreap))
(defun make-mitreap (size &key initial-contents)
  "Makes a treap of SIZE in O(SIZE) time. Its values are filled with the
identity element unless INITIAL-CONTENTS are supplied."
  (declare ((or null vector) initial-contents))
  (labels ((build (l r)
             (declare ((integer 0 #.most-positive-fixnum) l r))
             (if (= l r)
                 nil
                 (let* ((mid (ash (+ l r) -1))
                        (node (%make-mitreap (if initial-contents
                                                (aref initial-contents mid)
                                                +mop-identity+)
                                           (random most-positive-fixnum))))
                   (setf (%mitreap-left node) (build l mid))
                   (setf (%mitreap-right node) (build (+ mid 1) r))
                   (%mheapify node)
                   (mforce-up node)
                   node))))
    (build 0 size)))

(defun mitreap-split (mitreap index)
  "Destructively splits the MITREAP into two nodes [0, INDEX) and [INDEX, N),
where N is the number of elements of the MITREAP."
  (declare (optimize (speed 3))
           ((integer 0 #.most-positive-fixnum) index))
  (unless (<= index (mitreap-count mitreap))
    (error 'invalid-mitreap-index-error :index index :mitreap mitreap))
  (labels ((recur (mitreap ikey)
             (unless mitreap
               (return-from mitreap-split (values nil nil)))
             (let ((left-count (mitreap-count (%mitreap-left mitreap))))
               (if (<= ikey left-count)
                   (multiple-value-bind (left right)
                       (mitreap-split (%mitreap-left mitreap) ikey)
                     (setf (%mitreap-left mitreap) right)
                     (mforce-up mitreap)
                     (values left mitreap))
                   (multiple-value-bind (left right)
                       (mitreap-split (%mitreap-right mitreap) (- ikey left-count 1))
                     (setf (%mitreap-right mitreap) left)
                     (mforce-up mitreap)
                     (values mitreap right))))))
    (recur mitreap index)))

(defun mitreap-merge (left right)
  "Destructively concatenates two MITREAPs."
  (declare #.OPT
           ((or null mitreap) left right))
  (cond ((null left) (when right (mforce-up right)) right)
        ((null right) (when left (mforce-up left)) left)
        (t (if (> (%mitreap-priority left) (%mitreap-priority right))
               (progn
                 (setf (%mitreap-right left)
                       (mitreap-merge (%mitreap-right left) right))
                 (mforce-up left)
                 left)
               (progn
                 (setf (%mitreap-left right)
                       (mitreap-merge left (%mitreap-left right)))
                 (mforce-up right)
                 right)))))

(declaim (inline mitreap-map))
(defun mitreap-map (function mitreap)
  "Successively applies FUNCTION to MITREAP[0], ..., MITREAP[SIZE-1]."
  (declare (function function))
  (labels ((recur (node)
             (when node
               (recur (%mitreap-left node))
               (funcall function (%mitreap-value node))
               (recur (%mitreap-right node))
               (mforce-up node))))
    (recur mitreap)))

(defmethod print-object ((object mitreap) stream)
  (print-unreadable-object (object stream :type t)
    (let ((init t))
      (mitreap-map (lambda (x)
                    (if init
                        (setq init nil)
                        (write-char #\  stream))
                   (write x :stream stream))
                 object))))

(declaim (inline mitreap-ref))
(defun mitreap-ref (mitreap index)
  "Returns the element MITREAP[INDEX]."
  (declare ((integer 0 #.most-positive-fixnum) index))
  (unless (< index (mitreap-count mitreap))
    (error 'invalid-mitreap-index-error :mitreap mitreap :index index))
  (labels ((%ref (mitreap index)
             (declare ((integer 0 #.most-positive-fixnum) index))
             (prog1
                 (let ((left-count (mitreap-count (%mitreap-left mitreap))))
                   (cond ((< index left-count)
                          (%ref (%mitreap-left mitreap) index))
                         ((> index left-count)
                          (%ref (%mitreap-right mitreap) (- index left-count 1)))
                         (t (%mitreap-value mitreap))))
               (mforce-up mitreap))))
    (%ref mitreap index)))

(declaim (inline (setf mitreap-ref)))
(defun (setf mitreap-ref) (new-value mitreap index)
  "Sets MITREAP[INDEX] to the given value."
  (declare ((integer 0 #.most-positive-fixnum) index))
  (unless (< index (mitreap-count mitreap))
    (error 'invalid-mitreap-index-error :mitreap mitreap :index index))
  (labels ((%set (mitreap index)
             (declare ((integer 0 #.most-positive-fixnum) index))
             (prog1
                 (let ((left-count (mitreap-count (%mitreap-left mitreap))))
                   (cond ((< index left-count)
                          (%set (%mitreap-left mitreap) index))
                         ((> index left-count)
                          (%set (%mitreap-right mitreap) (- index left-count 1)))
                         (t (setf (%mitreap-value mitreap) new-value))))
               (mforce-up mitreap))))
    (%set mitreap index)
    new-value))

(declaim (inline mitreap-query))
(defun mitreap-query (mitreap l r)
  "Queries the `sum' (w.r.t. MOP) of the range MITREAP[L, R)."
  (declare ((integer 0 #.most-positive-fixnum) l r))
  (unless (<= l r (mitreap-count mitreap))
    (error 'invalid-mitreap-index-error :mitreap mitreap :index (cons l r)))
  (labels
      ((recur (mitreap l r)
         (declare ((integer 0 #.most-positive-fixnum) l r))
         (unless mitreap
           (return-from recur +mop-identity+))
         (prog1
             (if (and (zerop l) (= r (%mitreap-count mitreap)))
                 (mitreap-accumulator mitreap)
                 (let ((left-count (mitreap-count (%mitreap-left mitreap))))
                   (if (<= l left-count)
                       (if (< left-count r)
                           ;; LEFT-COUNT is in [L, R)
                           (mop (mop (recur (%mitreap-left mitreap) l (min r left-count))
                                   (%mitreap-value mitreap))
                               (recur (%mitreap-right mitreap) 0 (- r left-count 1)))
                           ;; LEFT-COUNT is in [R, END)
                           (recur (%mitreap-left mitreap) l (min r left-count)))
                       ;; LEFT-COUNT is in [0, L)
                       (recur (%mitreap-right mitreap) (- l left-count 1) (- r left-count 1)))))
           (mforce-up mitreap))))
    (recur mitreap l r)))

;; FIXME: might be problematic when two priorities collide and START is not
;; zero. (It will be negligible from the viewpoint of probability, however.)
(declaim (inline mitreap-range-bisect-left))
(defun mitreap-range-bisect-left (mitreap value order &optional (start 0))
  "Returns the smallest index that satisfies MITREAP[START]+ MITREAP[START+1] +
... + MITREAP[index] >= VALUE (if ORDER is #'<).

Note:
- This function handles a **closed** interval.
- This function returns the length of MITREAP instead if MITREAP[START]+
... +MITREAP[length-1] < VALUE.
- The prefix sums of MITREAP, (MITREAP[START], MITREAP[START]+MITREAP[START+1], ...)
  must be monotone w.r.t. ORDER.
- ORDER must be a strict order"
  (declare ((integer 0 #.most-positive-fixnum) start))
  (multiple-value-bind (mitreap-prefix mitreap)
      (if (zerop start)
          (values nil mitreap)
          (mitreap-split mitreap start))
    (labels
        ((recur (mitreap offset prev-sum)
           (declare ((integer 0 #.most-positive-fixnum) offset)
                    #+sbcl (values (integer 0 #.most-positive-fixnum)))
           (unless mitreap
             (return-from recur offset))
           (let ((sum prev-sum))
             (prog1
                 (cond ((not (funcall order
                                      (setq sum (mop sum (mitreap-accumulator (%mitreap-left mitreap))))
                                      value))
                        (recur (%mitreap-left mitreap) offset prev-sum))
                       ((not (funcall order
                                      (setq sum (mop sum (%mitreap-value mitreap)))
                                      value))
                        (+ offset (mitreap-count (%mitreap-left mitreap))))
                       (t
                        (recur (%mitreap-right mitreap)
                               (+ offset (mitreap-count (%mitreap-left mitreap)) 1)
                               sum)))
               (mforce-up mitreap)))))
      (prog1 (+ start (recur mitreap 0 +mop-identity+))
        (mitreap-merge mitreap-prefix mitreap)))))

(defun main ()
  (let* ((n (read))
         (as (make-array n :element-type 'uint31))
         (bs (make-array n :element-type 'uint31))
         (pairs (make-array n :element-type '(cons uint32 uint32))))
    (dotimes (i n)
      (let ((a (read-fixnum)))
        (setf (aref as i) a)))
    (dotimes (i n)
      (let ((b (read-fixnum)))
        (setf (aref bs i) b)))
    (dotimes (i n)
      (setf (aref pairs i)
            (cons (aref as i) (aref bs i))))
    (setq pairs (sort pairs (lambda (x y) (< (the uint31 x) (the uint31 y))) :key #'cdr))
    (let ((new-as (make-array n :element-type 'uint31))
          (sorted-as (sort (copy-seq as) #'<))
          (sorted-bs (sort (copy-seq bs) #'<)))
      (declare ((simple-array uint31 (*)) sorted-as sorted-bs))
      (dotimes (i n)
        (setf (aref new-as i) (car (aref pairs i))))
      (when (loop for a across sorted-as
                  for b across sorted-bs
                  thereis (> a b))
        (write-line "No")
        (return-from main))
      (when (< (length (delete-adjacent-duplicates (copy-seq sorted-as)))
               n)
        (write-line "Yes")
        (return-from main))
      (let ((dp (make-itreap n :initial-contents new-as))
            (pos 0))
        (dotimes (i (- n 2))
          (loop while (and (< pos n)
                           (<= (itreap-ref dp pos) (aref sorted-bs pos)))
                do (incf pos))
          (when (= pos n)
            (return))
          (let* ((new-min (itreap-query dp pos n))
                 (min-pos (itreap-range-bisect-left dp new-min #'> pos)))
            (declare (uint62 new-min min-pos))
            (unless (<= new-min (itreap-ref dp pos))
              (write-line "No")
              (return-from main))
            (rotatef (itreap-ref dp pos)
                     (itreap-ref dp min-pos))))
        (when (loop for i below n
                    always (<= (itreap-ref dp i) (aref sorted-bs i)))
          (write-line "Yes")
          (return-from main)))
      (let ((dp (make-mitreap n :initial-contents new-as))
            (pos (- n 1)))
        (dotimes (i (- n 2))
          (loop while (and (<= 0 pos)
                           (<= (mitreap-ref dp pos) (aref sorted-bs pos)))
                do (decf pos))
          (when (< pos 0)
            (return))
          (let* ((new-max (mitreap-query dp 0 (+ pos 1)))
                 (max-pos (mitreap-range-bisect-left dp new-max #'<)))
            (declare (uint62 new-max max-pos))
            (unless (<= new-max (mitreap-ref dp pos))
              (write-line "No")
              (return-from main))
            (rotatef (mitreap-ref dp pos)
                     (mitreap-ref dp max-pos))))
        (when (loop for i below n
                    always (<= (mitreap-ref dp i) (aref sorted-bs i)))
          (write-line "Yes")
          (return-from main)))
      (write-line "No"))))

#-swank (main)

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

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

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

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

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

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

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

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

;; To run: (5am:run! :sample)
#+swank
(it.bese.fiveam:test :sample
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "3
1 3 2
1 2 3
"
    "Yes
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "3
1 2 3
2 2 2
"
    "No
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "6
3 1 2 6 3 4
2 2 8 3 4 3
"
    "Yes
")))