#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define int long long
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define make(type, x) type x; cin>>x;
#define make2(type, x, y) type x, y; cin>>x>>y;
#define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z;
#define make4(type, x, y, z, t) type x, y, z, t; cin>>x>>y>>z>>t;
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
using namespace std;
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;
template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third; };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
for (T& v : vec) { ResizeVec(v, sz); }
}
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
while(*sdbg != ',')cerr<<*sdbg++; cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
typedef vector<VI> VVI;
class ParenthesesDiv1Hard {
public:
int depth(string s) {
int bil = 0;
int dep = 0;
for (int i = 0; i < SZ(s); i++) {
if (s[i] == '(') {
bil++;
maxi(dep, bil);
} else {
bil--;
}
}
/* if (bil != 0) {
reutrn*/
//assert(bil == 0);
return dep;
}
int cost(string s) {
if (s == "") {
return 0;
}
int bil = 0;
for (int i = 0; i < SZ(s); i++) {
if (s[i] == '(') {
bil++;
} else {
bil--;
}
if (bil == 0 && i < SZ(s) - 1) {
return cost(s.substr(0, i + 1)) + cost(s.substr(i + 1));
}
}
int d = depth(s);
debug(s, d);
return d * d + cost(s.substr(1, SZ(s) - 2));
}
#undef int
int minCost(string s) {
#define int long long
string left, right;
int hei_left = 0, hei_right = 0;
int min_hei = 0;
debug(s);
for (int i = 0; i < SZ(s); i++) {
if (s[i] == '(') {
if (hei_right < hei_left) {
right += '(';
hei_right++;
} else {
left += '(';
hei_left++;
}
} else {
if (hei_right == hei_left) {
right += ')';
hei_right--;
} else {
left += ')';
hei_left--;
}
}
mini(min_hei, hei_right);
}
if (min_hei < 0 || hei_left != 0 || hei_right != 0) {
return {-1};
}
debug(left, right);
int l = cost(left), r = cost(right);
debug(l, r);
return l + r;
// return cost(left) + cost(right);
}
};
#undef int
// BEGIN CUT HERE
#include <cstdio>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
namespace moj_harness {
using std::string;
using std::vector;
int run_test_case(int);
void run_test(int casenum = -1, bool quiet = false) {
if (casenum != -1) {
if (run_test_case(casenum) == -1 && !quiet) {
std::cerr << "Illegal input! Test case " << casenum << " does not exist." << std::endl;
}
return;
}
int correct = 0, total = 0;
for (int i=0;; ++i) {
int x = run_test_case(i);
if (x == -1) {
if (i >= 100) break;
continue;
}
correct += x;
++total;
}
if (total == 0) {
std::cerr << "No test cases run." << std::endl;
} else if (correct < total) {
std::cerr << "Some cases FAILED (passed " << correct << " of " << total << ")." << std::endl;
} else {
std::cerr << "All " << total << " tests passed!" << std::endl;
}
}
int verify_case(int casenum, const int &expected, const int &received, std::clock_t elapsed) {
std::cerr << "Example " << casenum << "... ";
string verdict;
vector<string> info;
char buf[100];
if (elapsed > CLOCKS_PER_SEC / 200) {
std::sprintf(buf, "time %.2fs", elapsed * (1.0/CLOCKS_PER_SEC));
info.push_back(buf);
}
if (expected == received) {
verdict = "PASSED";
} else {
verdict = "FAILED";
}
std::cerr << verdict;
if (!info.empty()) {
std::cerr << " (";
for (size_t i=0; i<info.size(); ++i) {
if (i > 0) std::cerr << ", ";
std::cerr << info[i];
}
std::cerr << ")";
}
std::cerr << std::endl;
if (verdict == "FAILED") {
std::cerr << " Expected: " << expected << std::endl;
std::cerr << " Received: " << received << std::endl;
}
return verdict == "PASSED";
}
int run_test_case(int casenum__) {
switch (casenum__) {
case 0: {
string s = "(())";
int expected__ = 2;
std::clock_t start__ = std::clock();
int received__ = ParenthesesDiv1Hard().minCost(s);
return verify_case(casenum__, expected__, received__, clock()-start__);
}
case 1: {
string s = "((())())(()()())";
int expected__ = 11;
std::clock_t start__ = std::clock();
int received__ = ParenthesesDiv1Hard().minCost(s);
return verify_case(casenum__, expected__, received__, clock()-start__);
}
case 2: {
string s = "())(()";
int expected__ = -1;
std::clock_t start__ = std::clock();
int received__ = ParenthesesDiv1Hard().minCost(s);
return verify_case(casenum__, expected__, received__, clock()-start__);
}
case 3: {
string s = "(((((((((())))))))))";
int expected__ = 110;
std::clock_t start__ = std::clock();
int received__ = ParenthesesDiv1Hard().minCost(s);
return verify_case(casenum__, expected__, received__, clock()-start__);
}
case 4: {
string s = "()";
int expected__ = 1;
std::clock_t start__ = std::clock();
int received__ = ParenthesesDiv1Hard().minCost(s);
return verify_case(casenum__, expected__, received__, clock()-start__);
}
case 5: {
string s = "(((())()()()((((()))))(((())))()()()))(()(()))";
int expected__ = 69;
std::clock_t start__ = std::clock();
int received__ = ParenthesesDiv1Hard().minCost(s);
return verify_case(casenum__, expected__, received__, clock()-start__);
}
// custom cases
/* case 6: {
string s = ;
int expected__ = ;
std::clock_t start__ = std::clock();
int received__ = ParenthesesDiv1Hard().minCost(s);
return verify_case(casenum__, expected__, received__, clock()-start__);
}*/
/* case 7: {
string s = ;
int expected__ = ;
std::clock_t start__ = std::clock();
int received__ = ParenthesesDiv1Hard().minCost(s);
return verify_case(casenum__, expected__, received__, clock()-start__);
}*/
/* case 8: {
string s = ;
int expected__ = ;
std::clock_t start__ = std::clock();
int received__ = ParenthesesDiv1Hard().minCost(s);
return verify_case(casenum__, expected__, received__, clock()-start__);
}*/
default:
return -1;
}
}
}
#include <cstdlib>
int main(int argc, char *argv[]) {
if (argc == 1) {
moj_harness::run_test();
} else {
for (int i=1; i<argc; ++i)
moj_harness::run_test(std::atoi(argv[i]));
}
}
// END CUT HERE
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgTVAgbWFrZV9wYWlyCiNkZWZpbmUgUEIgcHVzaF9iYWNrCiNkZWZpbmUgaW50IGxvbmcgbG9uZwojZGVmaW5lIHN0IGZpcnN0CiNkZWZpbmUgbmQgc2Vjb25kCiNkZWZpbmUgcmQgdGhpcmQKI2RlZmluZSBGT1IoaSwgYSwgYikgZm9yKGludCBpID0oYSk7IGkgPD0oYik7ICsraSkKI2RlZmluZSBSRShpLCBuKSBGT1IoaSwgMSwgbikKI2RlZmluZSBGT1JEKGksIGEsIGIpIGZvcihpbnQgaSA9IChhKTsgaSA+PSAoYik7IC0taSkKI2RlZmluZSBSRVAoaSwgbikgZm9yKGludCBpID0gMDtpIDwobik7ICsraSkKI2RlZmluZSBWQVIodiwgaSkgX190eXBlb2YoaSkgdj0oaSkKI2RlZmluZSBGT1JFKGksIGMpIGZvcihWQVIoaSwgKGMpLmJlZ2luKCkpOyBpICE9IChjKS5lbmQoKTsgKytpKQojZGVmaW5lIEFMTCh4KSAoeCkuYmVnaW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iI7IH19CiNlbHNlCiNkZWZpbmUgZGVidWcoLi4uKSAoX19WQV9BUkdTX18pCiNkZWZpbmUgZGVidWd2KHgpCiNkZWZpbmUgY2VyciBpZigwKWNvdXQKI2VuZGlmCgp0eXBlZGVmIHZlY3RvcjxWST4gVlZJOwpjbGFzcyBQYXJlbnRoZXNlc0RpdjFIYXJkIHsKcHVibGljOgogIGludCBkZXB0aChzdHJpbmcgcykgewogICAgaW50IGJpbCA9IDA7CiAgICBpbnQgZGVwID0gMDsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgU1oocyk7IGkrKykgewogICAgICBpZiAoc1tpXSA9PSAnKCcpIHsKICAgICAgICBiaWwrKzsKICAgICAgICBtYXhpKGRlcCwgYmlsKTsKICAgICAgfSBlbHNlIHsKICAgICAgICBiaWwtLTsKICAgICAgfQogICAgfQovKiAgICBpZiAoYmlsICE9IDApIHsKICAgICAgcmV1dHJuKi8gCiAgICAvL2Fzc2VydChiaWwgPT0gMCk7CiAgICByZXR1cm4gZGVwOwogIH0KICBpbnQgY29zdChzdHJpbmcgcykgewogICAgaWYgKHMgPT0gIiIpIHsKICAgICAgcmV0dXJuIDA7CiAgICB9CiAgICBpbnQgYmlsID0gMDsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgU1oocyk7IGkrKykgewogICAgICBpZiAoc1tpXSA9PSAnKCcpIHsKICAgICAgICBiaWwrKzsKICAgICAgfSBlbHNlIHsKICAgICAgICBiaWwtLTsKICAgICAgfQogICAgICBpZiAoYmlsID09IDAgJiYgaSA8IFNaKHMpIC0gMSkgewogICAgICAgIHJldHVybiBjb3N0KHMuc3Vic3RyKDAsIGkgKyAxKSkgKyBjb3N0KHMuc3Vic3RyKGkgKyAxKSk7CiAgICAgIH0KICAgIH0KICAgIGludCBkID0gZGVwdGgocyk7CiAgICBkZWJ1ZyhzLCBkKTsKICAgIHJldHVybiBkICogZCArIGNvc3Qocy5zdWJzdHIoMSwgU1oocykgLSAyKSk7CiAgfQojdW5kZW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