Bike Parking

시간 제한1초메모리 제한1024 MB

요약
각 사용자를 하나의 주차 슬롯에 배정해 추천 수에서 비추천 수를 뺀 값이 최대가 되도록 한다.
난이도

보통10점 중 7점

유형
그리디, 동적 계획법, 누적 합
정답자
아직 제출이 없습니다

문제

Sanne recently conceived a lucrative business idea: renting out premium bike parking at the Eindhoven train station. To maximize her profits, she divided the bike parking slots into NN different tiers, numbered from 00 to N−1N-1. Tier 0, the premium tier, is located very close to the train platforms. Higher-numbered tiers consist of parking slots that are worse (the higher the tier, the worse the slot). The number of slots in tier tt is x_tx\_t.

Users parking their bikes are assigned their parking slot via an app. Each user has a subscription level and expects a parking slot in the corresponding tier. However, the terms of service do not guarantee users a slot in their respective tier.

If a user with subscription level ss is assigned a slot in tier tt, then one of the following three things happens:

  1. If t<st < s, the user will be happy and upvote the app.
  2. If t=st = s, the user will be satisfied and will not do anything.
  3. If t>st > s, the user will be angry and downvote the app.

Today, Sanne's app has y_0+y_1+…+y_N−1y\_0+y\_1+\ldots+y\_{N-1} users, where y_sy\_s is the number of users with subscription level ss. She needs your help to assign the users to the parking slots. Each user should get exactly one slot. No slot can be assigned to more than one user, but it is okay for some parking slots to not be assigned to any users. Furthermore, the total number of users does not exceed the total number of available parking slots.

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

입력

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

The second line contains NN integers x_0,x_1,…,x_N−1x\_0, x\_1, \ldots, x\_{N-1}, the number of slots in the different tiers.

The third line contains NN integers y_0,y_1,…,y_N−1y\_0, y\_1, \ldots, y\_{N-1}, the number of users with each subscription level.

출력

Output one integer, the maximum possible value of U−DU-D by assigning the users to parking slots optimally.

제한

  • 1≤N≤3⋅1051 \leq N \leq 3 \cdot 10^5.
  • 0≤x_i,y_i≤1090 \leq x\_i, y\_i \leq 10^9 for i=0,1,…,N−1i = 0,1,\ldots , N-1.
  • y_0+y_1+…+y_N−1≤x_0+x_1+…+x_N−1≤109y\_0+y\_1+\ldots + y\_{N-1} \leq x\_0+x\_1+\ldots + x\_{N-1} \leq 10^9.

힌트

Note that some of the samples are not valid input for all test groups. The iith sample is at least valid for the iith test group.

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.

예제5

  1. 예제 1

    입력
    2
    3 3
    1 3
    
    예상 출력
    2
    
  2. 예제 2

    입력
    3
    1 1 1
    1 1 1
    
    예상 출력
    1
    
  3. 예제 3

    입력
    6
    1 0 1 1 0 1
    1 1 0 0 1 0
    
    예상 출력
    1
    
  4. 예제 4

    입력
    4
    2 1 1 8
    0 4 4 0
    
    예상 출력
    -1
    
  5. 예제 5

    입력
    1
    1000000000
    1000000000
    
    예상 출력
    0