Unusual Competitions

A bracketed sequence is called correct (regular) if by inserting “+” and “1” you can get a well-formed mathematical expression from it. For example, sequences “(())()”, “()” and “(()(()))” are correct, while “)(”, “(()” and “(()))(” are not.

The teacher gave Dmitry’s class a very strange task — she asked every student to come up with a sequence of arbitrary length, consisting only of opening and closing brackets. After that all the students took turns naming the sequences they had invented. When Dima’s turn came, he suddenly realized that all his classmates got the correct bracketed sequence, and whether he got the correct bracketed sequence, he did not know.

Dima suspects now that he simply missed the word “correct” in the task statement, so now he wants to save the situation by modifying his sequence slightly. More precisely, he can the arbitrary number of times (possibly zero) perform the reorder operation.

The reorder operation consists of choosing an arbitrary consecutive subsegment (substring) of the sequence and then reordering all the characters in it in an arbitrary way. Such operation takes l nanoseconds, where l is the length of the subsegment being reordered. It’s easy to see that reorder operation doesn’t change the number of opening and closing brackets. For example for “))((” he can choose the substring “)(” and do reorder “)()(” (this operation will take 2 nanoseconds).

Since Dima will soon have to answer, he wants to make his sequence correct as fast as possible. Help him to do this, or determine that it’s impossible.

Input

The first line contains a single integer n(1≤n≤106)n (1≤n≤10^6)n(1≤n≤106)— the length of Dima’s sequence.

The second line contains string of length nnn, consisting of characters “(” and “)” only.

Output

Print a single integer — the minimum number of nanoseconds to make the sequence correct or “-1” if it is impossible to do so.

Examples
Input
8
))((())(
Output
6
Input
3
(()
Output
-1

Note

In the first example we can firstly reorder the segment from first to the fourth character, replacing it with “()()”, the whole sequence will be “()()())(”. And then reorder the segment from the seventh to eighth character, replacing it with “()”. In the end the sequence will be “()()()()”, while the total time spent is 4+2=6 nanoseconds.

思路

题意:给出长度为n的有左括号和有括号的数组,我们可以每次选择长度为m的连续序列使其满足匹配要求,同时会消耗m秒,求使得整个数组满足匹配要求需要的最小时间。
思路:直接遍历,记录左右括号的数量,当数量相等时,判断是否满足匹配,如果不能,那么这一段序列需要重排,花费该段长度时间~

代码如下

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int mx=1e6+10;
int a,b,n,m,ans;//a记录左括号,b记录右括号,m判断是否匹配
char s[mx];//似乎不需要数组了= =int main()
{a=b=m=0;scanf("%d",&n);ans=0;getchar();for(int i=1;i<=n;i++){scanf("%c",&s[i]);if(s[i]=='('){a++;m++;}else {b++;if(m>0)m--;}if(a==b){if(m!=0)ans+=2*a;a=b=m=0;//一定要记住清零}}if(a!=b)printf("-1\n");elseprintf("%d\n",ans);return 0;
}

Unusual Competitions相关推荐

  1. A. Unusual Competitions

    滴答滴答---题目链接 A. Unusual Competitions time limit per test 1 second memory limit per test 512 megabytes ...

  2. Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)C. Unusual Competitions

    C. Unusual Competitions time limit per test1 second memory limit per test512 megabytes inputstandard ...

  3. CodeForces - 1323C Unusual Competitions(贪心)

    题目链接:点击查看 题目大意:给出一个长度为 n 的括号序列,现在允许的操作是对于一段区间 [ l , r ] 内的括号重新排列,所需要的花费为区间长度,问如果想要使得括号序列变为正确的形式,最少花费 ...

  4. Unusual Competitions CodeForces - 1323C(思维)

    A bracketed sequence is called correct (regular) if by inserting "+" and "1" you ...

  5. 1322A. Unusual Competitions · 括号

    题解 题意: 括号匹配,给一段括号,可以选择一段区间[l,r][l,r][l,r],交换其内部括号的位置使得这一段区间的括号是匹配的,代价是区间长度,问要使整个区间都是匹配的最小代价是多少 官方题解: ...

  6. Codeforces 1322 A. Unusual Competitions

    题意: 给出一个含有 ()( )() 的字符串,让你可以选择一个区间进行重新排序,问一共选择的区间长度是多少可以使得字符串最后变成 "(())()","()"& ...

  7. C. Unusual Competitions

    链接:https://codeforces.ml/contest/1323/problem/C A bracketed sequence is called correct (regular) if ...

  8. CodeForces - 1323 C Unusual Competitions

    一.内容 A bracketed sequence is called correct (regular) if by inserting "+" and "1" ...

  9. Codeforces 1323 div2题解ABC

    A. Even Subset Sum Problem 签到题 #include <bits/stdc++.h> using namespace std; template <type ...

最新文章

  1. no copy constructor available or copy constructor is declared #39;explicit#39;
  2. 处理业务代码中循环遍历出现的性能问题
  3. Linux的链接工具 putty 以及一些命令。
  4. Android模拟器的模拟键盘区不见了怎么办
  5. alisql mysql_AliSQL · 特性介绍 · 动态加字段
  6. macOS完整项目源码之macOS动态壁纸创建工具(教程含源码)
  7. 如何复制百度文库、豆丁网上的文本
  8. α-β剪枝算法学习寄(蒟蒻向,巨佬勿入)
  9. html5生成桌面图标,简单几步打造独一无二的动态桌面图标
  10. IDEA社区版没有spring Initializr
  11. c语言中常用运算符号
  12. 5ic计算机考试考卷读取错误,北京自考出现错误试卷
  13. Arduino uno读取mpu6050模块角度以及温度值
  14. 【软件测试】测试人,我们35岁焦虑怎样破?
  15. 中文版-Because He Lives-因他活着-好消息诗班(音乐河2)
  16. C# 简单图片爬虫 快来斗图把
  17. P1765 手机python题解
  18. 护眼灯真能护眼吗?学习专用的护眼灯推荐
  19. 五线城市房价已大幅下跌,房地产泡沫破裂或由此开始
  20. 英语影视台词---三、Cinema Paradiso

热门文章

  1. 八个不得不知的重要法则 让你更懂互联网
  2. 小白入门篇:量化大神Eric跟你聊量化交易
  3. 《关键对话》如何高效沟通,营造无往不利的事业和人生?
  4. qq播放器免费的方法
  5. python绘制糖葫芦_python用小海龟画糖葫芦
  6. 2018-2019-2 20175227张雪莹《Java程序设计》 实验二《Java面向对象程序设计》
  7. CSS (二) 背景
  8. 适合短期内想快速上手数字孪生系统的人使用的数字孪生软件推荐
  9. JS中页面跳转,传值包含中文时乱码解决方案
  10. 淘宝怎么做推广?史上最全的淘宝流量入口整理