CF BUDDY
← Problems·

1866G · Grouped Carriages

2100 · binary search, data structures, dp

Problem: Pak Chanek observes that the carriages of a train is always full on morning departure hours and afternoon departure hours. Therefore, the balance between carriages is needed so that it is not too crowded in only a few carriages.

A train contains NN carriages that are numbered from 11 to NN from left to right. Carriage ii initially contains AiA_i passengers. All carriages are connected by carriage doors, namely for each ii (1iN11\leq i\leq N-1), carriage ii and carriage i+1i+1 are connected by a two-way door.

Each passenger can move between carriages, but train regulation regulates that for each ii, a passenger that starts from carriage ii cannot go through more than DiD_i doors.

Define ZZ as the most number of passengers in one same carriage after moving. Pak Chanek asks, what is the minimum possible value of ZZ?

Input Format: The first line contains a single integer NN (1N21051 \leq N \leq 2\cdot10^5) — the number of carriages.

The second line contains NN integers A1,A2,A3,,ANA_1, A_2, A_3, \ldots, A_N (0Ai1090 \leq A_i \leq 10^9) — the initial number of passengers in each carriage.

The third line contains NN integers D1,D2,D3,,DND_1, D_2, D_3, \ldots, D_N (0DiN10 \leq D_i \leq N-1) — the maximum limit of the number of doors for each starting carriage.

Output Format: An integer representing the minimum possible value of ZZ.

Note: One strategy that is optimal is as follows:

  • 55 people in carriage 11 move to carriage 44 (going through 33 doors).
  • 33 people in carriage 55 move to carriage 33 (going through 22 doors).
  • 22 people in carriage 66 move to carriage 55 (going through 11 door).
  • 11 person in carriage 66 moves to carriage 77 (going through 11 door).

The number of passengers in each carriage becomes [2,4,5,5,4,5,4][2,4,5,5,4,5,4].

Sample Cases

Case 1

Input

7
7 4 2 0 5 8 3
4 0 0 1 3 1 3

Output

5

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback