Bracket Problem Yet Again

시간 제한8초메모리 제한2048 MB

요약
각 k=0부터 n까지에 대해, 최대 k개 위치의 비용을 0으로 만들 수 있을 때 균형 잡힌 괄호 문자열의 최소 비용을 구한다.
난이도

어려움10점 중 8점

유형
그리디, 힙, 누적 합, 구현
정답자
아직 제출이 없습니다

문제

A balanced bracket string is a string consisting of '(' and ')', which can make a valid mathematical expression by inserting 00 or ++ in the string 00 or more times.

Bob had learned how to find the balanced bracket string with minimum cost, when the cost is defined as follows.

  • If position ii contains '(', the cost of the position is a_ia\_i;
  • If position ii contains ')', the cost of the position is b_ib\_i;
  • The cost of a balanced bracket string is the sum of costs of all positions.

While rethinking about the solution, Bob became curious about the following problem.

  • Assuming I can make a_ia\_i and b_ib\_i both 00 for at most kk different indices, what is the new minimum cost?

As he was unable to solve the modified problem, he asked you to solve it. As Bob is a very selfish person, he wants you to solve it for each possible value of kk. The indices chosen for k=xk=x need not be a strict subset of the indices chosen for k=x+1k=x+1. In other words, the problem must be solved independently for all values of kk.

입력

The first line contains an even integer nn, the length of the bracket sequence. (2≤n≤2⋅1052 \le n \le 2\cdot 10^5)

The second line contains nn integers a_1,a_2,⋯ ,a_na\_1,a\_2,\cdots,a\_n, the costs of using '(' on each index. (0≤a_i≤1090 \le a\_i \le 10^9)

The third line contains nn integers b_1,b_2,⋯ ,b_nb\_1,b\_2,\cdots,b\_n, the costs of using ')' on each index. (0≤b_i≤1090 \le b\_i \le 10^9)

출력

Output n+1n+1 integers c_0,c_1,⋯ ,c_nc\_0,c\_1,\cdots,c\_n separated by spaces. c_xc\_x is defined by the minimum cost of the problem when k=xk=x.

예제1

  1. 예제 1

    입력
    4
    0 5 0 5
    3 3 3 3
    
    예상 출력
    6 3 0 0 0