Ninja training
Limits: 4 sec., 512 MiB
This statement was translated automatically from Ukrainian.
Splinter has prepared a training program for the Teenage Mutant Ninja Turtles for \(n\) days. There are \(n\) exercises in the program, numbered from \(1\) to \(n\). Exercise number \(i\) has complexity \(a_i\).
Throughout the program, the turtles will master the exercises. After the \(i\)-th day of the program, the turtles will have completely mastered the technique of performing exercise number \(p_i\) and will no longer perform this exercise during the following days of the program — in this case, the exercise is removed from the program.
Every day, Splinter chooses a sequence of exercises that have not yet been removed from the program for the turtles to perform. The sequence must satisfy the following conditions.
The turtles perform exercises in increasing order of their numbers.
The turtles perform exercises in increasing order of their complexity.
The number of performed exercises must be maximized.
Help Splinter and the turtles find the number of exercises to perform during each of the \(n\) days of the program.
Input
The first line contains an integer \(n\) — the number of exercises in the program.
The second line contains \(n\) integers \(a_i\) — the complexities of the exercises.
The third line contains \(n\) integers \(p_i\) — the numbers of the removed exercises.
Output
Output \(n\) integers — the number of exercises to be performed by the turtles for each of the \(n\) days of the program in order.
Constraints
\(1 \le n \le 10^6\),
\(1 \le a_i \le 10^6\),
the sum of \(a_i\) does not exceed \(10^6\),
\(p\) is a permutation of numbers from \(1\) to \(n\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 5 1 2 3 2 4 3 4 5 1 2 | 4 3 3 2 1 |
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|