This page is still under construction.

Parts of this page are still being built. What you see may change.

Dams

Time limit1sMemory limit512 MB

Summary
Each of n sectors fills at its own rate behind dams of given heights, and you compute when water first spills past an end dam.
Level

Hard8 of 10

Topics
Heap, Union-find, Simulation
Solved
No attempts yet

Problem

A large water reservoir was built in Byteotia. It is divided into several sectors of equal length. Between every two neighboring sectors stands a dam of some height, and dams also stand in front of the first sector and behind the last sector.

At this moment the water level is the same across the whole reservoir. But heavy rain has begun and the water level is rising fast. The king wants to know how long it will take before water spills over the first dam or the last dam, which would surely flood the whole country. The calculation is tricky because the rain may fall with a different intensity over each sector.

Compute the time left until water spills out of the reservoir. If the water level is exactly equal to a dam's height, the water is not yet spilling. When a flooded part of the reservoir is bounded on both sides by dams of the same height, water spills from it to both sides equally fast.

Input

The first line contains one integer nn (1≤n≤500 0001 \le n \le 500\,000), the number of sectors the reservoir is divided into.

The second line contains n+1n+1 integers wiw_i (1≤wi≤1 000 0001 \le w_i \le 1\,000\,000) separated by single spaces, giving the heights of the successive dams from left to right, measured above the initial water level.

The third line contains nn integers kik_i (1≤ki≤1 000 0001 \le k_i \le 1\,000\,000) separated by single spaces, where kik_i is the number of levels by which the water in the ii-th sector rises during one second.

Output

Print one integer: the smallest integer not smaller than the number of seconds after which water spills out of the reservoir.

Hint

Examples1

  1. Example 1

    Input
    4
    3 5 2 6 4
    1 3 3 1
    
    Expected output
    2