Dynamic maximum sum (2) P84977


Statement
 

pdf   zip

Here, you have to efficiently keep a list of integer numbers, which is initially empty. Let the current list be x0,,xn1x_0, \dots, x_{n-1}. There are just two operations:

  • Given an integer xx and any position jj between 0 and nn, insert xx before the jj-th position (at the end, if j=nj = n). That is, the new list must be x0,,xj1,x,xj,,xn1x_0, \dots, x_{j-1}, x, x_j, \dots, x_{n-1}.

  • Report the maximum sum of all the consecutive subsequences of the list.

Input

Input consists of several cases. Every case begins with the number of operations mm, followed by the mm operations. We have an M for reporting the maximum, and I xx jj for inserting. Assume 1m21051 \le m \le 2 \cdot 10^5, 1012x1012-10^{12} \le x \le 10^{12}, and that jj is between 0 and the current list size.

Output

For every case, and for every M operation, print the maximum sum of consecutive elements inside the current list. Print a line with 10 dashes at the end of each case.

Public test cases
  • Input

    8
    I 5 0
    M
    I 1 1
    M
    I -3 1
    M
    I 4 2
    M
    
    3
    M
    I -100 0
    M
    
    6
    I 1000000000000 0
    I 1000000000000 0
    I -1 1
    M
    I 1000000000000 3
    M
    

    Output

    5
    6
    5
    7
    ----------
    0
    0
    ----------
    1999999999999
    2999999999999
    ----------
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++
    User solutions
    C++