panik and his Chocolate bar

0

0 votes
Easy
Problem

Panik has a large chocolate bar with N  linearly connected pieces.

Each piece has its own chocolate value Ai. His friend Priyam keeps on making cuts on his chocolate bar and asks him to pick the connected component of the chocolate with the maximum chocolate content.

The chocolate content of a connected component is the sum of all the chocolate value of the pieces lying in the connected component.

Since the task is too difficult for him, Panik wants you to help him pick the connected component of the chocolate with maximum chocolate content after every cut.


INPUT

  • The first line contains 2 integers: n, k, denoting the number of chocolate pieces, k denoting the number of cuts made.
  • The second line contains N integers, A1,A2,A3... An denoting chocolate values of the ith piece.
  • The third line contains K integers, B1,B2,B3...Bk where Bi denotes a cut between index Bi and Bi +1.

OUTPUT

Print K lines, each line containing chocolate content of the component having the maximum chocolate content out of all available components after the ith cut.


CONSTRAINTS 

  • 1 ≤ N ≤ 300000
  • 1 ≤ K ≤ N−1
  • 1 ≤ A[i] ≤ 1000
  • 1 ≤ B[i] ≤ N−1
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Initially, the chocolate had 5 pieces with values 4,5,6,7,8. After the first cut, the chocolate is divided into 2 parts. The first component will have chocolate value of 4 and the second component will have chocolate value of 5+6+7+8 i.e. 26. After the second cut, the chocolate is divided into 3 components. The first component will have chocolate value of 4, the second component will have chocolate value of 5+6 11, the third component will have chocolate value of 7+8 i.e 15. 

Figure out the rest of the answers

Editor Image

?