let () =
    let split s =
        let rec loop i cur flag acc =
            if i = String.length s then ( if flag then cur :: acc else acc) else (
                match flag, s.[i] with
                | true,  ' ' -> loop (i + 1) "" false (cur :: acc)
                | false, ' ' -> loop (i + 1) "" false acc
                | true,  v   -> loop (i + 1) (Printf.sprintf "%s%c" cur v) true acc
                | false, v   -> loop (i + 1) (Printf.sprintf "%s%c" cur v) true acc
            )
        in
        List.map int_of_string (List.rev (loop 0 "" false []))
    in
    let main () =
        let n = int_of_string (read_line ()) in
        let a = split (read_line ()) in
        let b = split (read_line ()) in
        let rec loop prev acc a b =
            match a, b with
            | (a1 :: al, b1 :: bl) ->
                    let arest = max 0 (a1 - prev) in
                    let brest = max 0 (b1 - arest) in
                    let acc = acc + a1 - arest + b1 - brest in
                    loop brest acc al bl
            | a1 :: al, [] ->
                    let arest = max 0 (a1 - prev) in
                    acc + a1 - arest
            | _ -> failwith "??"
        in
        Printf.printf "%d\n" (loop 0 0 a b)
    in
    main ()