#DSU0000069. Phá hủy dãy số (Destroying Array)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Phá hủy dãy số (Destroying Array)
Destroying Array
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Given an array of non-negative integers , positions will be destroyed in the order of a permutation .
After each destruction, consider every contiguous segment of the original array that contains no destroyed position. Find the greatest sum of such a segment. An empty segment has sum , so the final answer after all positions are destroyed is .
Removing an element does not make its neighbors on either side adjacent. For example, after position is removed, positions and cannot belong to the same valid segment even if both remain present.
After each removal, consider all contiguous segments containing no removed positions and select the largest sum. If no nonempty segment remains, the answer is . All array values are nonnegative, so the answer cannot be negative.
Input
The first line contains , the second contains , and the third contains the destruction permutation .
Output
Print lines; line is the largest valid segment sum after positions are destroyed.
Subtasks
- Subtask 1 (20 points): ; ; is a permutation of .
- Subtask 2 (30 points): ; ; is a permutation of .
- Subtask 3 (50 points): ; ; is a permutation of .
Examples
Input
4
1 3 2 5
3 4 1 2
Output
5
4
3
0
Explanation
After removing position , the maximum remaining segment sum is . Removing position next leaves a best segment of sum . After removing position , the best sum is . Once all positions have been removed, the answer is .