#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define fi first
#define se second
const int maxN=3e5+5;
const ll inf=2e18;
int n;
ll dp[maxN+1];
string s;
vector<int> adj[maxN+1];
stack<int> st;
void clr()
{
while(!st.empty())
{
st.pop();
}
}
int main()
{
//freopen("zagrade.inp","r",stdin);
//freopen("zagrade.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>s;
s=" "+s;
for(int i=1;i<n;i++)
{
int u,v;
cin>>u>>v;
}
for(int i=1;i<=n;i++)
{
if(s[i]=='(')
{
st.push(i);
}
else
{
if(!st.empty()&&s[st.top()]=='(')
{
dp[i]=dp[st.top()-1]+1;
st.pop();
}
else clr();
}
}
ll res=0;
for(int i=1;i<=n;i++)
{
res+=dp[i];
}
memset(dp,0,sizeof(dp));
clr();
for(int i=1;i<=n;i++)
{
if(s[i]==')')
{
st.push(i);
}
else
{
if(!st.empty()&&s[st.top()]==')')
{
dp[i]=dp[st.top()-1]+1;
st.pop();
}
else clr();
}
}
for(int i=1;i<=n;i++)
{
res+=dp[i];
}
cout<<res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
9816 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
14264 KB |
Output is correct |
2 |
Correct |
39 ms |
14752 KB |
Output is correct |
3 |
Correct |
40 ms |
14268 KB |
Output is correct |
4 |
Correct |
40 ms |
14528 KB |
Output is correct |
5 |
Correct |
40 ms |
14372 KB |
Output is correct |
6 |
Correct |
39 ms |
14268 KB |
Output is correct |
7 |
Correct |
39 ms |
14400 KB |
Output is correct |
8 |
Correct |
41 ms |
14272 KB |
Output is correct |
9 |
Correct |
40 ms |
14268 KB |
Output is correct |
10 |
Correct |
39 ms |
14780 KB |
Output is correct |
11 |
Correct |
46 ms |
14380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
9816 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |