一、内容

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 2anoseconds).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) — the length of Dima's sequence.The second line contains string of length n  , 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

Input8
))((())(Output6Input3
(()Output-1

二、思路

  • 当我们遇到‘(’的时候代表+1, 遇到 ')'代表-1。
  • 那么当我们首次从水平线 遇到) 的时候 这时候 如果再遇到( 值会变成0,但是这部分必须重新排序。 因为)的原因导致这部分不合法。
  • 故 我们只需要统计红色的部分的长度就是需要修改的长度。

三、代码

#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e6 + 5;
int n;
char s[N];
void solve() {int cnt = 0, ans = 0;for (int i = 1; i <= n; i++) {if (s[i] == '(') cnt++; else cnt--;if (cnt < 0 || cnt == 0 && s[i] == '(') ans++;} printf("%d", cnt != 0 ? -1 : ans);
}
int main() {scanf("%d%s", &n, s + 1);solve(); return 0;
}

CodeForces - 1323 C Unusual Competitions相关推荐

  1. Codeforces 1322 A. Unusual Competitions

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

  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 1323 div2题解ABC

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

  4. Unusual Competitions

    Unusual Competitions A bracketed sequence is called correct (regular) if by inserting "+" ...

  5. A. Unusual Competitions

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

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

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

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

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

  8. C. Unusual Competitions

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

  9. 1322A. Unusual Competitions · 括号

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

最新文章

  1. java 获取 反射 方法 名_乐字节Java反射之一:反射概念与获取反射源头Class
  2. CentOS下安装Orcale
  3. oracle ora-01476: 除数为 0,Oracle常见错误:ORA-01403 的根本原因和解决方案
  4. android编译单独image
  5. 你真的理解a -- -- a a++ ++a 吗?
  6. docker 卸载镜像_docker删除所有容器和镜像命令
  7. (8)二进制文件方式部署Kubernetes高可用集群----------部署高可用ETCD集群
  8. 腾讯 csig 面试简述(已通过)
  9. GB2312/GBK编码规则及单片机汉显字库算法
  10. EXCEL 图表-双坐标轴
  11. 【数学建模】2021 年全国大学生数学建模竞赛A题一等奖获奖论文
  12. 系统函数,频率响应定义
  13. fluent p1模型_FLUENT中的辐射模型
  14. 平板电脑性价比排行2022
  15. pytorch 中 利用自定义函数 get_mask_from_lengths(lengths, max_len)获取每个batch的mask
  16. php怎么访问ldap,用Php如何操作LDAP_PHP
  17. H3C iMC 存在远程命令执行漏洞
  18. Python yield 详解(嚼碎了喂你,一篇精通,无需再看其他文章)
  19. 启扬智慧电梯物联网解决方案,解决运营、维保、监管难题
  20. 怎样开启电脑护眼模式

热门文章

  1. 无人机航测案例-湘南某县增减挂钩拆旧地块航拍任务
  2. 探究 EIP-4488 —— Rollup 助燃剂
  3. HiveSql一天一个小技巧:如何不使用union all 进行列转行
  4. MixMarvel行业分享专栏 韩国区块链市场观察:行业巨头布局区块链游戏领域
  5. 登陆id显示无法连接服务器失败,无法连接id服务器失败怎么办
  6. commvault备份mysql数据库_2-CommVault备份项目实施方案-XXXX.docx
  7. Redis的配置及与memcached区别
  8. [英语阅读]经济危机 清明祭品也缩水
  9. 请问汽车CD接线各个的字母代表什么,ACC,ILL,RR,FR,FL,RLANT,B/U,NC,CND,真诚的谢谢了!
  10. 小节点也能引爆活动!2021四月活动指导方案