Codeforces 722C - Destroying Array

Xem dạng PDF

Gửi bài giải

Điểm: 6,00 (OI)
Giới hạn thời gian: 1.0s
Giới hạn bộ nhớ: 512M
Input: stdin
Output: stdout

Nguồn bài:
https://codeforces.com/problemset/problem/1249/D2
Dạng bài
Ngôn ngữ cho phép
C, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

Reference: Codeforces 722C — Destroying Array

You are given an array of ~n~ non-negative integers.

The elements are destroyed one by one. The order of destruction is given by a permutation ~p_1, p_2, \ldots, p_n~, where operation ~i~ destroys the element at position ~p_i~.

After every operation, consider all contiguous segments containing no destroyed positions. Among those segments, find the maximum possible sum.

The value of an empty segment is considered to be ~0~.

Print the answer after each destruction.

Input

  • The first line contains one integer ~n~.
  • The second line contains the array: ~a_1, a_2, \ldots, a_n~.
  • The third line contains a permutation: ~p_1, p_2, \ldots, p_n~.

Output

  • Print ~n~ lines. The ~i~-th line must contain the maximum sum of an undestroyed contiguous segment after the first ~i~ operations.

Constraints

  • ~1 \le n \le 10^5~
  • ~0 \le a_i \le 10^9~
  • ~p~ is a permutation of ~1, 2, \ldots, n~

Example 1

Input:

4
1 3 2 5
3 4 1 2

Output:

5
4
3
0

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.