#P11274. Maximum Score with Clearing Operations

    ID: 13348 Type: Default 1000ms 256MiB

Maximum Score with Clearing Operations

Maximum Score with Clearing Operations

You are given a single row chessboard with n cells numbered from 1 to n. Each cell i contains a score a_i and an integer parameter b_i. You traverse the board from left to right. At each cell, you can either choose to select it or skip it.

The process works as follows. Initially, you are in a clearable mode (mode 1) with an empty list of selected cells. When you reach cell i:

  1. If you skip it, nothing changes.
  2. If you select it and you are in clearable mode (mode 1):
    • If the number of previously selected cells is at least $b_i$, then immediately the first $b_i$ selected cells (i.e. the oldest ones) are cleared (removed) and cell i is added to the end of the selection list. You remain in clearable mode.
    • If the number of previously selected cells is less than $b_i$, then no clearing occurs not only for cell i but also for any subsequent selections. In other words, from that point onward you enter a final mode (mode 2) where every cell you select simply adds its score.
  3. If you select a cell when you are already in final mode (mode 2), its score is simply added (with no clearing).

The final score is the sum of the scores of the cells that remain selected (after all clearings have taken place). Your task is to choose a subset of cells (in order) so that the final score is maximized.

Note: All formulas are expressed in \(\LaTeX\) format. For example, the condition for clearing is formulated as: if \(|S| \geq b_i\), then remove the first \(b_i\) elements from the current selection list \(S\).

inputFormat

The first line contains an integer n representing the number of cells. The second line contains n integers \(a_1, a_2, \dots, a_n\) representing the scores on the cells. The third line contains n integers \(b_1, b_2, \dots, b_n\) representing the clearing parameters for each cell.

outputFormat

Output a single integer — the maximum final score you can achieve.

sample

5
1 2 3 4 5
0 1 1 1 1
14