Under construction

Still being built. Things may change

Bike Parking

Time limit1sMemory limit1024 MB

Summary
Assign each user to one slot so that upvotes minus downvotes is maximized, given the slot count per tier and the user count per subscription level.
Level

Medium7 of 10

Topics
Greedy, Sorting, Prefix sum
Solved
No attempts yet

Problem

Sanne recently came up with a lucrative business idea: renting out premium bike parking at Eindhoven train station. To maximize profit, she split the bike parking slots into NN tiers, numbered from 00 to N−1N-1. Tier 0 is the premium tier, very close to the train platforms. Higher-numbered tiers hold worse slots. Tier tt has xtx_t slots.

Users park their bikes and get their slot through an app. Each user has a subscription level and expects a slot in the matching tier. However, the terms of service do not guarantee a slot in that tier.

If a user with subscription level ss gets a slot in tier tt, one of three things happens:

  1. If t<st < s, the user is happy and upvotes the app.
  2. If t=st = s, the user is satisfied and does nothing.
  3. If t>st > s, the user is angry and downvotes the app.

Today the app has y0+y1+…+yN−1y_0+y_1+\ldots+y_{N-1} users, where ysy_s is the number of users with subscription level ss. Help Sanne assign the users to slots. Each user gets exactly one slot. No slot can go to more than one user, but some slots may stay empty. The number of users does not exceed the number of available slots.

Sanne wants to maximize the rating of her app. Let UU be the number of upvotes and DD the number of downvotes. Maximize U−DU-D.

Input

The first line contains one integer NN, the number of tiers or subscription levels.

The second line contains NN integers x0,x1,…,xN−1x_0, x_1, \ldots, x_{N-1}, the number of slots in each tier.

The third line contains NN integers y0,y1,…,yN−1y_0, y_1, \ldots, y_{N-1}, the number of users at each subscription level.

Output

Output one integer, the maximum possible value of U−DU-D over all assignments of users to slots.

Constraints

  • 1≤N≤3⋅1051 \leq N \leq 3 \cdot 10^5.
  • 0≤xi,yi≤1090 \leq x_i, y_i \leq 10^9 for i=0,1,…,N−1i = 0,1,\ldots,N-1.
  • y0+y1+…+yN−1≤x0+x1+…+xN−1≤109y_0+y_1+\ldots+y_{N-1} \leq x_0+x_1+\ldots+x_{N-1} \leq 10^9.

Hints

In the first sample, you can assign the user with subscription level 0 to a tier 0 slot, assign two users of level 1 to tier 0 slots (leading to 2 upvotes), and assign the remaining level 1 user to a tier 1 slot. This leads to a rating of 22.

In the second sample, you can assign the level 1 user to the tier 0 slot, the level 2 user to the tier 1 slot, and the level 0 user to the tier 2 slot. This gives 2 upvotes and 1 downvote, leading to a rating of 11.

In the third sample, you can assign the level 1 user to the tier 0 slot, the level 0 user to the tier 2 slot, and the level 4 user to the tier 3 slot. This again gives 2 upvotes and 1 downvote, leading to a rating of 11.

The fourth sample is illustrated below. You can assign the users of level 1 to slots of tiers 0, 0, 3 and 3, leading to 2 upvotes and 2 downvotes. Next, assign the users of level 2 to slots of tiers 1, 2, 3 and 3, leading to 1 upvote and 2 downvotes. This amounts to 3 upvotes and 4 downvotes, so the rating is −1-1.

In the fifth sample, you can assign everyone to a slot matching their own subscription level, so the rating is 00.

Examples5

  1. Example 1

    Input
    2
    3 3
    1 3
    
    Expected output
    2
    
  2. Example 2

    Input
    3
    1 1 1
    1 1 1
    
    Expected output
    1
    
  3. Example 3

    Input
    6
    1 0 1 1 0 1
    1 1 0 0 1 0
    
    Expected output
    1
    
  4. Example 4

    Input
    4
    2 1 1 8
    0 4 4 0
    
    Expected output
    -1
    
  5. Example 5

    Input
    1
    1000000000
    1000000000
    
    Expected output
    0