一、内容

The development of a text editor is a hard problem. You need to implement an extra module for brackets coloring in text.Your editor consists of a line with infinite length and cursor, which points to the current character. Please note that it points to only one of the characters (and not between a pair of characters). Thus, it points to an index character. The user can move the cursor left or right one position. If the cursor is already at the first (leftmost) position, then it does not move left.Initially, the cursor is in the first (leftmost) character.Also, the user can write a letter or brackets (either (, or )) to the position that the cursor is currently pointing at. A new character always overwrites the old value at that position.Your editor must check, whether the current line is the correct text. Text is correct if the brackets in them form the correct bracket sequence.Formally, correct text (CT) must satisfy the following rules: any line without brackets is CT (the line can contain whitespaces); If the first character of the string — is (, the last — is ), and all the rest form a CT, then the whole line is a CT; two consecutively written CT is also CT. Examples of correct texts: hello(codeforces), round, ((i)(write))edi(tor)s, ( me). Examples of incorrect texts: hello)oops(, round), ((me).The user uses special commands to work with your editor. Each command has its symbol, which must be written to execute this command.The correspondence of commands and characters is as follows: 

Input

The first line contains an integer n(1≤n≤106) — the number of commands.The second line contains s— a sequence of commands. The string s consists of n

characters. It is guaranteed that all characters in a string are valid commands.

Output

In a single line print integers, where the i-th number is: −1if the line received after processing the first icommands is not valid text, the minimal number of colors in the case of the correct text.

Input

11
(RaRbR)L)L(

Output

-1 -1 -1 -1 -1 -1 1 1 -1 -1 2 

Input

11
(R)R(R)Ra)c

Output

-1 -1 1 1 -1 -1 1 1 1 -1 1 

二、思路

  • 询问已经组成的字符串的括号是否匹配成功,若匹配成功输出最大的嵌套数。
  • 我们规定 ‘(’ 为 1 , ‘)’为-1,那么最大的前套数就是整个区间最大的前缀和。
    如 ((()())) 前缀和最大为第三个括号的位置,最大的时候为3。故我们只需要维护一个区间内的最大前缀和即可。
  • 判断非法括号:
    (1)区间内的’( ’数 必须等于’) 数。 我们可以用一个sum来维护区间和,区间和为0代表2个括号数量相等。
    (2)区间的最小前缀和不能小于0。 如 )( 这种情况区间和虽然为0,但是并不合法。这种情况下前缀和最小的就小于0,最小值为-1。故用minS维护一个前缀和最小值。

三、代码

#include <cstdio>
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a > b ? b : a)
const int N = 1e6 +5;
int n, maxS[N <<2], minS[N <<2], sum[N << 2];
char s[N];
void pushup(int id) {
	sum[id] = sum[id << 1] + sum[id << 1 | 1];
	//该区间的前缀和最大值 = 左子树的最大值 与 左子树区间和+右子树最大值比较 
	maxS[id] = max(maxS[id << 1], maxS[id << 1 | 1] + sum[id << 1]);
	minS[id] = min(minS[id << 1], sum[id << 1] + minS[id << 1 | 1]);
} 
void update(int id, int l, int r, int x, int c) {
	if (l == r) {
		sum[id] = maxS[id] = minS[id] = c;
		return;
	}
	int mid = (l + r) >> 1;
	if (x <= mid) update(id << 1, l, mid, x, c);
	else update(id << 1 | 1, mid + 1, r, x, c);
	pushup(id);
}
int main() {
	scanf("%d%s", &n, s);
	int cur = 1;
	
	for (int i = 0; i < n; i++) {
		if (s[i] == 'L') {
			if (cur > 1) cur--;
		} else if (s[i] == 'R') {
			cur++;
		} else if (s[i] == '(') {
			update(1, 1, n, cur, 1);
		} else if (s[i] == ')') {
			update(1, 1, n, cur, -1);
		} else {
			update(1, 1, n, cur, 0);
		}
		if (sum[1] != 0 || minS[1] < 0) {
			printf("-1 ");
		} else {
			printf("%d ", maxS[1]);
		}
	} 
	return 0;	
}