C - Bad Sequence

Petya's friends made him a birthday present — a bracket sequence. Petya was quite disappointed with his gift, because he dreamed of correct bracket sequence, yet he told his friends nothing about his dreams and decided to fix present himself.

To make everything right, Petya is going to move at most one bracket from its original place in the sequence to any other position. Reversing the bracket (e.g. turning "(" into ")" or vice versa) isn't allowed.

We remind that bracket sequence ss is called correct if:

  • ss is empty;
  • ss is equal to "(tt)", where tt is correct bracket sequence;
  • ss is equal to t_1 t_2t1​t2​, i.e. concatenation of t_1t1​ and t_2t2​, where t_1t1​ and t_2t2​ are correct bracket sequences.

For example, "(()())", "()" are correct, while ")(" and "())" are not. Help Petya to fix his birthday present and understand whether he can move one bracket so that the sequence becomes correct.

Input

First of line of input contains a single number nn (1 \leq n \leq 200\,0001≤n≤200000) — length of the sequence which Petya received for his birthday.

Second line of the input contains bracket sequence of length nn, containing symbols "(" and ")".

Output

Print "Yes" if Petya can make his sequence correct moving at most one bracket. Otherwise print "No".

Examples

Input

2
)(

Output

Yes

Input

3
(()

Output

No

Input

2
()

Output

Yes

Input

10
)))))(((((

Output

No

Note

In the first example, Petya can move first bracket to the end, thus turning the sequence into "()", which is correct bracket sequence.

In the second example, there is no way to move at most one bracket so that the sequence becomes correct.

In the third example, the sequence is already correct and there's no need to move brackets.

#include<iostream>
using namespace std;
#include<string>
#include<algorithm>
#pragma warning (disable:4996)
#include <climits>
#include <vector>
#include<stack>
int main() {
	int n;
	cin >> n;
	if (n % 2 != 0) {
		cout << "No";
		return 0;
	}
	getchar();
	stack<char> a;
	char temp;
	bool flag = false;
	while ((temp = getchar()) != '\n')
	{
		if (temp == '(')
			a.push(temp);
		else if (temp == ')') {
			if (a.empty() && !flag)
				flag = true;
			else if (a.empty() && flag) {
				cout << "No";
				return 0;
			}
			else if (!a.empty()) {
				a.pop();
			}
		}
	}
	if (a.size() == 0 && flag == false) {
		cout << "Yes";
	}
	else if (a.size() == 1 && flag == true) {
		cout << "Yes";
	}
	else {
		cout << "No";
	}
	return 0;

}

这道题就是考察堆栈数据结构吧,然后因为完全对称性的问题,肯定是偶数满足条件,然后通过简单的证明,我得到了需要挪动的位置其实就是当栈为空的时候,只有一个')',并且是有且仅有一个

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cjz-lxg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值