722C.md (2045B)
1 Source: https://codeforces.com/problemset/problem/722/C 2 3 C. Destroying Array 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You are given an array consisting of *n* non-negative integers *a*1, *a*2, ..., *a**n*. 22 23 You are going to destroy integers in the array one by one. Thus, you are given the permutation of integers from 1 to *n* defining the order elements of the array are destroyed. 24 25 After each element is destroyed you have to find out the segment of the array, such that it contains no destroyed elements and the sum of its elements is maximum possible. The sum of elements in the empty segment is considered to be 0. 26 27 Input 28 29 The first line of the input contains a single integer *n* (1 ≤ *n* ≤ 100 000) — the length of the array. 30 31 The second line contains *n* integers *a*1, *a*2, ..., *a**n* (0 ≤ *a**i* ≤ 109). 32 33 The third line contains a permutation of integers from 1 to *n* — the order used to destroy elements. 34 35 Output 36 37 Print *n* lines. The *i*-th line should contain a single integer — the maximum possible sum of elements on the segment containing no destroyed elements, after first *i* operations are performed. 38 39 Examples 40 41 Input 42 43 ``` 44 4 45 1 3 2 5 46 3 4 1 2 47 ``` 48 49 Output 50 51 ``` 52 5 53 4 54 3 55 0 56 ``` 57 58 Input 59 60 ``` 61 5 62 1 2 3 4 5 63 4 2 3 5 1 64 ``` 65 66 Output 67 68 ``` 69 6 70 5 71 5 72 1 73 0 74 ``` 75 76 Input 77 78 ``` 79 8 80 5 5 4 4 6 6 5 5 81 5 2 8 7 1 3 4 6 82 ``` 83 84 Output 85 86 ``` 87 18 88 16 89 11 90 8 91 8 92 6 93 6 94 0 95 ``` 96 97 Note 98 99 Consider the first sample: 100 101 1. Third element is destroyed. Array is now 1 3 * 5. Segment with maximum sum 5 consists of one integer 5. 102 2. Fourth element is destroyed. Array is now 1 3 * * . Segment with maximum sum 4 consists of two integers 1 3. 103 3. First element is destroyed. Array is now * 3 * * . Segment with maximum sum 3 consists of one integer 3. 104 4. Last element is destroyed. At this moment there are no valid nonempty segments left in this array, so the answer is equal to 0.