Bike Parking
Time limit1sMemory limit1024 MB
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 tiers, numbered from to . Tier 0 is the premium tier, very close to the train platforms. Higher-numbered tiers hold worse slots. Tier has 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 gets a slot in tier , one of three things happens:
- If , the user is happy and upvotes the app.
- If , the user is satisfied and does nothing.
- If , the user is angry and downvotes the app.
Today the app has users, where is the number of users with subscription level . 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 be the number of upvotes and the number of downvotes. Maximize .
Input
The first line contains one integer , the number of tiers or subscription levels.
The second line contains integers , the number of slots in each tier.
The third line contains integers , the number of users at each subscription level.
Output
Output one integer, the maximum possible value of over all assignments of users to slots.
Constraints
- .
- for .
- .
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 .
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 .
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 .
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 .

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