#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long ans=0,sh=0,cnt=0;
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        stack<char>st;
        if(s[i]==')')
        {
            continue;
        }
        else st.push('(');
        sh=cnt=0;
        for(int j=i+1;j<s.size();j++)
        {
            if(s[j]==')')
            {
                if(!st.empty()&&st.top()=='(') st.pop();
                else
                {
                    st.push(')');
                    sh++;
                }
            }
            else if(s[j]=='(')
            {
                st.push('(');
            }
            else
            {
                if(st.empty()) st.push('(');
                else if(!st.empty()&&st.top()=='(')
                {
                    st.pop();
                    cnt+=2;
                }
                else
                {
                    st.push('(');
                }
            }
            if(st.empty())
            {
                ans++;
                //cout<<i<<" "<<j<<endl;
            }
        }
        if(cnt==sh&&cnt!=0)
        {
            ans++;
            cout<<i<<endl;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long ans=0,sh=0,cnt=0;
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        stack<char>st;
        if(s[i]==')')
        {
            continue;
        }
        else st.push('(');
        sh=cnt=0;
        for(int j=i+1;j<s.size();j++)
        {
            if(s[j]==')')
            {
                if(!st.empty()&&st.top()=='(') st.pop();
                else
                {
                    st.push(')');
                    sh++;
                }
            }
            else if(s[j]=='(')
            {
                st.push('(');
            }
            else
            {
                if(st.empty()) st.push('(');
                else if(!st.empty()&&st.top()=='(')
                {
                    st.pop();
                    cnt+=2;
                }
                else
                {
                    st.push('(');
                }
            }
            if(st.empty())
            {
                ans++;
                cout<<i<<" "<<j<<endl;
            }
            else if(cnt==sh&&cnt!=0)
            {
                cout<<i<<endl;
                ans++;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long ans=0,fat=0,afl=0,qu=0;
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        fat=afl=qu=0;
        for(int j=i;j<s.size();j++)
        {
            if(s[j]=='(') fat++;
            else if(s[j]==')') afl++;
            else
            {
                if(fat==afl) fat++;
                else qu++;
            }

            if(afl>fat+qu) break;

            if(s[j]=='(') continue;
            int x=abs(afl-fat);
            if(qu>=x&&(qu-x)%2==0)
            {
                ans++;
                //cout<<i<<" "<<j<<endl;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    map<int,char>m;
    m[0]='(';
    m[1]=')';
    m[2]='?';

    string s;
    cin>>s;
    long long ans=0,cnt=0,qu=0,pos=0;
    for(int i=0;i<s.size();i++)
    {
        cnt=qu=pos=0;
        for(int j=i;j<s.size();j++)
        {
            if(s[j]==')') cnt--;
            else if(s[j]=='(') cnt++;
            else
            {
                if(cnt==0) cnt++;
                else if(cnt>0)
                {
                    cnt--;
                    pos+=2;
                }
            }
            if(cnt==0)
            {
                ans++;
                //cout<<i<<" "<<j<<endl;
            }
            else if(cnt+pos==0)
            {
                ans++;
                //cout<<i<<" "<<j<<endl;
            }
            if(cnt<0)
            {
                if(pos>0)
                {
                    pos-=2;
                    cnt=1;
                }
                else break;
            }
        }
    }
    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long ans=0,sh=0,cnt=0;
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        stack<char>st;
        if(s[i]==')')
        {
            continue;
        }
        else st.push('(');
        sh=cnt=0;
        for(int j=i+1;j<s.size();j++)
        {
            if(s[j]==')')
            {
                if(!st.empty()&&st.top()=='(') st.pop();
                else
                {
                    st.push(')');
                    sh++;
                }
            }
            else if(s[j]=='(')
            {
                st.push('(');
            }
            else
            {
                if(st.empty())
                {
                    st.push('(');
                    cnt++;
                }
                else if(!st.empty()&&st.top()=='(')
                {
                    st.pop();
                    cnt+=2;
                }
                else
                {
                    st.push('(');
                }
            }
            if(st.empty())
            {
                ans++;
                ///cout<<i<<" "<<j<<endl;
            }
            else if(cnt==sh&&cnt!=0)
            {
                //cout<<i<<endl;
                ans++;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    map<int,char>m;
    m[0]='(';
    m[1]=')';
    m[2]='?';

    string s;
    cin>>s;
    long long ans=0,fat=0,afl=0,qu=0;
    for(int i=0;i<s.size();i++)
    {
        fat=afl=qu=0;
        for(int j=i;j<s.size();j++)
        {
            if(s[j]=='(') fat++;
            else if(s[j]==')') afl++;
            else
            {
                if(fat==afl) fat++;
                else qu++;
            }

            if(afl>fat+qu) break;

            if(s[j]=='(') continue;

            if(fat==afl&&qu%2==1)
            {
                fat++;
                qu--;
            }

            int x=abs(afl-fat);
            if(qu>=x&&(qu-x)%2==0)
            {
                ans++;
                //if(i==0&&j==5) cout<<fat<<" "<<afl<<" "<<qu<<" "<<x<<endl;
                //cout<<i<<" "<<j<<endl;
            }
        }
    }
    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long ans=0,sh=0,cnt=0;
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        stack<char>st;
        if(s[i]==')')
        {
            continue;
        }
        else st.push('(');
        sh=cnt=0;
        for(int j=i+1;j<s.size();j++)
        {
            if(s[j]==')')
            {
                if(!st.empty()&&st.top()=='(') st.pop();
                else
                {
                    st.push(')');
                    sh++;
                }
            }
            else if(s[j]=='(')
            {
                st.push('(');
            }
            else
            {
                if(st.empty()) st.push('(');
                else if(!st.empty()&&st.top()=='(')
                {
                    st.pop();
                    cnt+=2;
                }
                else
                {
                    st.push('(');
                }
            }
            if(st.empty())
            {
                ans++;
                //cout<<i<<" "<<j<<endl;
            }
        }
        if(cnt==sh&&cnt!=0)
        {
            ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long ans=0,sh=0,cnt=0;
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        stack<char>st;
        if(s[i]==')')
        {
            continue;
        }
        else st.push('(');
        sh=cnt=0;
        for(int j=i+1;j<s.size();j++)
        {
            if(s[j]==')')
            {
                if(!st.empty()&&st.top()=='(') st.pop();
                else
                {
                    st.push(')');
                    sh++;
                }
            }
            else if(s[j]=='(')
            {
                st.push('(');
            }
            else
            {
                if(st.empty()) st.push('(');
                else if(!st.empty()&&st.top()=='(')
                {
                    st.pop();
                    cnt+=2;
                }
                else
                {
                    st.push('(');
                }
            }
            if(st.empty())
            {
                ans++;
                //cout<<i<<" "<<j<<endl;
            }
        }
        if(cnt>=sh&&cnt!=0)
        {
            ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long ans=0,sh=0,cnt=0;
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        stack<char>st;
        if(s[i]==')')
        {
            continue;
        }
        else st.push('(');
        sh=cnt=0;
        for(int j=i+1;j<s.size();j++)
        {
            if(s[j]==')')
            {
                if(st.top()=='(') st.pop();
                else
                {
                    st.push(')');
                    sh++;
                }
            }
            else if(s[j]=='(')
            {
                st.push('(');
            }
            else
            {
                if(st.empty()) st.push('(');
                else if(st.top()=='(')
                {
                    st.pop();
                    cnt+=2;
                }
                else
                {
                    st.push('(');
                }
            }
            if(st.empty())
            {
                ans++;
                //cout<<i<<" "<<j<<endl;
            }
        }
        if(cnt==sh&&cnt!=0)
        {
            ans++;
            cout<<i<<endl;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    long long ans=0,sh=0,cnt=0;
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        stack<char>st;
        if(s[i]==')')
        {
            continue;
        }
        else st.push('(');
        sh=cnt=0;
        for(int j=i+1;j<s.size();j++)
        {
            if(s[j]==')')
            {
                if(!st.empty()&&st.top()=='(') st.pop();
                else
                {
                    st.push(')');
                    sh++;
                }
            }
            else if(s[j]=='(')
            {
                st.push('(');
            }
            else
            {
                if(st.empty()) st.push('(');
                else if(!st.empty()&&st.top()=='(')
                {
                    st.pop();
                    cnt+=2;
                }
                else
                {
                    st.push('(');
                }
            }
            if(st.empty())
            {
                ans++;
                //cout<<i<<" "<<j<<endl;
            }
            else if(cnt==sh&&cnt!=0)
            {
                //cout<<i<<endl;
                ans++;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = 5000 + 10;
const int mod = 1000000007;
LL dp[maxn][2];
int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    IOS
    char s[maxn];
    scanf("%s", s+1);
    int len = strlen(s+1);
    for(int i = 1;i<=len;i++){
        if(s[i]=='?' || s[i] == ')'){
            if( i-1 > 0 && (s[i-1] == '?' || s[i-1] == '(' )){
                dp[i][0] ++;
            }

            for(int j = 0;j<=dp[i-1][0];j++){
                if(i-2*j-1>=1 && (s[i-2*j-1]=='?'||s[i-2*j-1]=='(') ){
                    dp[i][1]++;
                }
            }
        }
        dp[i][0] += dp[i-1][0];
        dp[i][1] += dp[i-1][1];
    }
//    for(int i = 1;i<=len;i++)
//        cout << dp[i][0] << " " << dp[i][1] << endl;
//
////    long ans = (1LL*dp[len][0] + 1) * dp[len][0] / 2;
////    ans += 1LL*dp[len][1];
////    cout << ans << endl;
    cout << dp[len][1] << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = 5000 + 10;
const int mod = 1000000007;
char s[maxn];
int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
//    IOS
    scanf("%s", s+1);
    int len = strlen(s+1);
    int ans = 0;
    for(int i = 1;i<=len;i++){
        int left = 0;
        int cnt = 0;
        for(int j = i;j<=len;j++){
            if(s[j]=='(') left++;
            else if(s[j]==')')  left--;
            else{
                if(left-1<0)    left++;
                else left++, cnt++;
            }
            if(left<0)  break;
            else if(left==0) ans++;
            else if(left % 2 == 0 && left - 2*cnt <= 0 )    ans++;

//            cout << i << " " << j << " " << ans << endl;
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = 5000 + 10;
const int mod = 1000000007;
char s[maxn];
int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
//    IOS
    scanf("%s", s+1);
    int len = strlen(s+1);
    int ans = 0;
    for(int i = 1;i<=len;i++){
        int left = 0;
        int cnt = 0;
        for(int j = i;j<=len;j++){
            if(s[j]=='(') left++;
            else if(s[j]==')')  left--;
            else left++, cnt++;
            if(left<0)  break;
            else if(left==0) ans++;
            else if(left % 2 == 0 && left - 2*cnt <= 0 )    ans++;

//            cout << i << " " << j << " " << ans << endl;
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = 5000 + 10;
const int mod = 1000000007;
long dp[maxn][2];
int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    IOS
    char s[maxn];
    scanf("%s", s+1);
    int len = strlen(s+1);
    for(int i = 1;i<=len;i++){
        if(s[i]=='?' || s[i] == ')'){
            if( i-1 > 0 && (s[i-1] == '?' || s[i-1] == '(' )){
                dp[i][0] ++;
            }

            for(int j = 0;j<=dp[i-1][0];j++){
                if(i-2*j-1>=1 && (s[i-2*j-1]=='?'||s[i-2*j-1]=='(') ){
                    dp[i][1]++;
                }
            }
        }
        dp[i][0] += dp[i-1][0];
        dp[i][1] += dp[i-1][1];
    }
//    for(int i = 1;i<=len;i++)
//        cout << dp[i][0] << " " << dp[i][1] << endl;
//
////    long ans = (1LL*dp[len][0] + 1) * dp[len][0] / 2;
////    ans += 1LL*dp[len][1];
////    cout << ans << endl;
    cout << dp[len][1] << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = 5000 + 10;
const int mod = 1000000007;
char s[maxn];
int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
//    IOS
    scanf("%s", s+1);
    int len = strlen(s+1);
    int ans = 0;
    for(int i = 1;i<=len;i++){
        int left = 0;
        int cnt = 0;
        for(int j = i;j<=len;j++){
            if(s[j]=='(') left++;
            else if(s[j]==')')  left--;
            else{
                left--, cnt++;
            }
            if(left==0) ans++;
            else if(left<0 && cnt) left+=2, cnt--;
            else if(left<0 && !cnt) break;

//            cout << i << " " << j << " " << ans << endl;
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = 5000 + 10;
const int mod = 1000000007;
LL dp[maxn][2];
int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    IOS
    char s[maxn];
    scanf("%s", s+1);
    int len = strlen(s+1);
    for(int i = 1;i<=len;i++){
        if(s[i]=='?' || s[i] == ')'){
            if( i-1 > 0 && (s[i-1] == '?' || s[i-1] == '(' )){
                dp[i][0] ++;
            }

            for(int j = 0;j<=dp[i-1][0];j++){
                if(i-2*j-1>=1 && (s[i-2*j-1]=='?'||s[i-2*j-1]=='(') && dp[i-1][0] - dp[i-2*j-1][0] == j){
                    dp[i][1]++;
                }
            }
        }
        dp[i][0] += dp[i-1][0];
        dp[i][1] += dp[i-1][1];
    }
    cout << dp[len][1] << endl;
    return 0;
}


#include<iostream>
#include<vector>

using namespace std;

int main()
{
    int res = 0;
    string in;
    cin >> in;
    for(int i = 0 ; i < in.length() ; i++)
    {
        vector<int> openP, questionM;
        for(int j = i ; j < in.length() ; j++)
        {
            if(in[j] == '(')
                openP.push_back(j);
            else if(in[j] == '?')
                questionM.push_back(j);
            else
            {
                if(openP.size() > 0)
                    openP.pop_back();
                else if(questionM.size() > 0)
                    questionM.erase(questionM.begin());
                else
                    break;
            }

            if((j - i + 1) % 2 == 0)
            {
                if(openP.empty() && questionM.size() % 2 == 0)
                    res++;
                else if(!openP.empty() && !questionM.empty())
                {
                    int m,n;
                    for(m = openP.size() - 1, n = questionM.size() - 1 ; m >= 0 && n >= 0 ; m--, n--)
                        if(openP[m] > questionM[n])
                            break;
                    if(m == -1 && (n + 1) % 2 == 0) {
                        res++;
                    }
                }
            }
        }
    }

    cout << res;

    return 0;
}


#include<iostream>
#include<algorithm>
#include<stack>
#include<vector>

using namespace std;

vector<pair<int,int> > res;
string in;
bool b[5002][5002];

void solve(stack<pair<char,int> > s, int indx)
{
    if(indx == in.length())
        return ;
    else if(in[indx] == '(') {
        s.push(pair<char,int>('(', indx));
        solve(s, indx + 1);
    }
    else if(in[indx] == ')')
    {
        if(s.size() > 0){
            if(!b[s.top().second][indx]) {
                res.push_back(pair<int, int>(s.top().second, indx));
                b[s.top().second][indx] = 1;
            }
            s.pop();
        }
        solve(s, indx + 1);
    }
    else {
        if(s.size() > 0){
            int temp = s.top().second;
            if(!b[s.top().second][indx]) {
                res.push_back(pair<int, int>(s.top().second, indx));
                b[s.top().second][indx] = 1;
            }
            s.pop();
            solve(s, indx + 1);
            s.push(pair<char,int>('(', temp));
            s.push(pair<char,int>('(', indx));
            solve(s, indx + 1);
        }
        else{
            s.push(pair<char,int>('(', indx));
            solve(s, indx + 1);
        }
    }
}

int main()
{
    cin >> in;
    stack<pair<char, int> > s;
    solve(s, 0);
    sort(res.begin(), res.end());

    for(int i = 0 ; i < res.size() ; i++){
        int temp = res[i].second + 1;
        for(int j = 0 ; j < res.size() ; j++){
            if(res[j].first == temp && !b[res[i].first][res[j].second]){
                b[res[i].first][res[j].second] = 1;
                res.push_back(pair<int,int>(res[i].first, res[j].second));
            }
        }
    }

    cout << res.size();

    return 0;
}


#include<iostream>
#include<algorithm>
#include<stack>
#include<vector>

using namespace std;

vector<pair<int,int> > res;
string in;
bool b[5002][5002];

void solve(stack<pair<char,int> > s, int indx)
{
    if(indx == in.length())
        return ;
    else if(in[indx] == '(') {
        s.push(pair<char,int>('(', indx));
        solve(s, indx + 1);
    }
    else if(in[indx] == ')')
    {
        if(s.size() > 0){
            if(!b[s.top().second][indx]) {
                res.push_back(pair<int, int>(s.top().second, indx));
                b[s.top().second][indx] = 1;
            }
            s.pop();
        }
        solve(s, indx + 1);
    }
    else {
        if(s.size() > 0){
            int temp = s.top().second;
            if(!b[s.top().second][indx]) {
                res.push_back(pair<int, int>(s.top().second, indx));
                b[s.top().second][indx] = 1;
            }
            s.pop();
            solve(s, indx + 1);
            s.push(pair<char,int>('(', temp));
            s.push(pair<char,int>('(', indx));
            solve(s, indx + 1);
        }
        else{
            s.push(pair<char,int>('(', indx));
            solve(s, indx + 1);
        }
    }
}

int main()
{
    cin >> in;
    stack<pair<char, int> > s;
    solve(s, 0);
    sort(res.begin(), res.end());

    for(int i = 0 ; i < res.size() ; i++){
        int temp = res[i].second + 1;
        for(int j = i + 1 ; j < res.size() ; j++){
            if(res[j].first == temp && !b[res[i].first][res[j].second]){
                b[res[i].first][res[j].second] = 1;
                res.push_back(pair<int,int>(res[i].first, res[j].second));
            }
        }
    }

    cout << res.size();

    return 0;
}


#include<iostream>
#include<stack>

using namespace std;

int res =  0;
string in;

void solve(stack<char> s, int indx)
{
    if(indx == in.length())
        return ;
    else if(in[indx] == '(') {
        s.push('(');
        solve(s, indx + 1);
    }
    else if(in[indx] == ')')
    {
        if(s.size() > 0){
            res++;
            s.pop();
        }
        solve(s, indx + 1);
    }
    else {
        if(s.size() > 0){
            res++;
            s.pop();
            solve(s, indx + 1);
            s.push('(');
            s.push('(');
            solve(s, indx + 1);
        }
        else{
            s.push('(');
            solve(s, indx + 1);
        }
    }
}

int main()
{
    cin >> in;
    stack<char> s;
    solve(s, 0);
    cout << res ;

    return 0;
}


#include<iostream>
#include<stack>

using namespace std;

int main()
{
    int res = 0;
    string s;
    cin >> s;
    for(int i = 0 ; i < s.length(); ++i)
    {
        stack<int> o,qo;
        int open = 0,qOpen = 0, qClose = 0;
        for(int j = i ; j < s.length() ; ++j)
        {
            if(s[j] == '(') {
                ++open;
                o.push(j);
            }
            else if(s[j] == '?')
            {
                if(open - qOpen > 0) {
                    ++qOpen;
                    qo.push(j);
                }
                else
                    ++qClose;
            }
            else
            {
                if(open > 0)
                {
                    --open;
                    if(qOpen > 0 && o.top() < qo.top())
                    {
                        if(open - qOpen + 1 == 0) {
                            qo.pop();
                            --qOpen;
                            ++qClose;
                        }
                    }
                    o.pop();
                }
                else
                {
                    if(qClose > 0)
                        --qClose;
                    else
                        break;
                }
            }

            if(open - qOpen == 0 && qClose % 2 == 0) {
                ++res;
            }
        }
    }

    cout << res;

    return 0;
}


#include<iostream>
#include<stack>

using namespace std;

int main()
{
    int res = 0;
    string s;
    cin >> s;
    for(int i = 0 ; i < s.length(); ++i)
    {
        stack<int> o,qo;
        int open = 0,qOpen = 0, qClose = 0;
        for(int j = i ; j < s.length() ; ++j)
        {
            if(s[j] == '(') {
                ++open;
                o.push(j);
            }
            else if(s[j] == '?')
            {
                if(open - qOpen > 0) {
                    ++qOpen;
                    qo.push(j);
                }
                else
                    ++qClose;
            }
            else
            {
                if(open > 0)
                {
                    --open;
                    if(qOpen > 0 && o.top() < qo.top())
                    {
                        int temp = qo.top();
                        qo.pop();
                        o.pop();
                        if((qo.size() > 0 && o.size() > 0 && o.top() < qo.top()) || o.size() == 0)
                        {
                            --qOpen;
                            ++qClose;
                        }
                        else if(o.size() > 0)
                            qo.push(temp);
                    }
                }
                else
                {
                    if(qClose > 0)
                        --qClose;
                    else
                        break;
                }
            }

            if(open - qOpen == 0 && qClose % 2 == 0) {
                ++res;
            }
        }
    }

    cout << res;

    return 0;
}


#include <bits/stdc++.h>
#define MAXN 5005

int N;
std::string s;
int pos[MAXN][MAXN]; //idx, dist

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);

    std::cin >> s;
    N = (int)s.size();

    memset(pos, 0, sizeof pos);
    for(int i=0; i<=N; i++) pos[i][0] = 1;

    int ans=0;
    for(int i=2; i<=N; i+=2)
    {
        for(int j=0; j+i-1<N; j++)
        {
            if(s[j] != ')' && s[j+1] != '(' && pos[j+2][i-2] == 1) pos[j][i] = 1;
            else if(s[j] != ')' && s[j+i-1] != '(' && pos[j+1][i-2] == 1) pos[j][i] = 1;
            else if(s[j+i-2] != ')' && s[j+i-1] != '(' && pos[j+1][i-2] == 1) pos[j][i] = 1;

            ans += pos[j][i];
        }
    }
    std::cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>

int N;
std::string s;

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);

    std::cin >> s;
    N = (int)s.size();

    int ans=0, a, b, c;

    for(int l=0; l<N; l++)
    {
        a=1; b=0; c=0;
        if(s[l] == ')') continue;
        for(int r=l+1; r<N; r++)
        {
            if(s[r] == '(') a++;
            else if(s[r] == ')') b++;
            else c++;

            if(b > a + c) break;
            if((r-l)%2 == 1 && abs(a-b) <= c) ans++;
        }
    }

    std::cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define MAXN 5005

typedef long long int ll;

int N;
std::string s;
int pos[MAXN][MAXN]; //idx, dist

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);

    std::cin >> s;
    N = (int)s.size();

    memset(pos, 0, sizeof pos);
    for(int i=0; i<=N; i++) pos[i][0] = 1;

    ll ans=0;
    for(int i=2; i<=N; i+=2)
    {
        for(int j=0; j+i-1<N; j++)
        {
            if(s[j] != ')' && s[j+1] != '(' && pos[j+2][i-2] == 1) pos[j][i] = 1;
            else if(s[j] != ')' && s[j+i-1] != '(' && pos[j+1][i-2] == 1) pos[j][i] = 1;
            else if(s[j+i-2] != ')' && s[j+i-1] != '(' && pos[j+1][i-2] == 1) pos[j][i] = 1;

            ans += pos[j][i];
        }
    }
    std::cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define MAXN 5005

typedef long long int ll;

int N;
std::string s;
int pos[MAXN][MAXN]; //idx, dist

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);

    std::cin >> s;
    N = (int)s.size();

    memset(pos, 0, sizeof pos);
    for(int i=0; i<=N+2; i++) pos[i][0] = 1;

    ll ans=0;
    for(int i=2; i<=N; i+=2)
    {
        for(int j=0; j+i-1<N; j++)
        {
            if(s[j] != ')' && s[j+1] != '(' && pos[j+2][i-2] == 1) pos[j][i] = 1;
            else if(s[j] != ')' && s[j+i-1] != '(' && pos[j+1][i-2] == 1) pos[j][i] = 1;
            else if(s[j+i-2] != ')' && s[j+i-1] != '(' && pos[j][i-2] == 1) pos[j][i] = 1;

            if(pos[j][i] == 1) ans++;
        }
    }
    std::cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define MAXN 5005

typedef long long int ll;

int N;
std::string s;
int pos[MAXN][MAXN]; //idx, dist

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);

    std::cin >> s;
    N = (int)s.size();

    memset(pos, 0, sizeof pos);
    for(int i=0; i<=N; i++) pos[i][0] = 1;

    ll ans=0;
    for(int i=2; i<=N; i+=2)
    {
        for(int j=0; j+i-1<N; j++)
        {
            if(s[j] != ')' && s[j+1] != '(' && pos[j+2][i-2] == 1) pos[j][i] = 1;
            else if(s[j] != ')' && s[j+i-1] != '(' && pos[j+1][i-2] == 1) pos[j][i] = 1;
            else if(s[j+i-2] != ')' && s[j+i-1] != '(' && pos[j][i-2] == 1) pos[j][i] = 1;

            if(pos[j][i] == 1) ans++;
        }
    }
    std::cout << ans << "\n";

    return 0;
}


#include <iostream>

int N;
std::string s;

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);

    std::cin >> s;
    N = (int)s.size();

    int ans=0, a, b;

    for(int l=0; l<N; l++)
    {
        a=0; b=0;
        for(int r=l; r<N; r++)
        {
            if(s[r] == '(') a++;
            else if(s[r] == ')') a--;
            else b++;

            if(b > a && b > 0)
            {
                a++;
                b--;
            }

            if(a < 0) break;
            if(b >= a && (r-l)%2 == 1) ans++;
        }
    }

    std::cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define MAXN 5005

typedef long long int ll;

int N;
std::string s;
int pos[MAXN][MAXN]; //idx, dist

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);

    std::cin >> s;
    N = (int)s.size();

    for(int i=0; i<=N+2; i++) pos[i][0] = 1;

    ll ans=0;
    for(int i=2; i<=N; i+=2)
    {
        for(int j=0; j+i-1<N; j++)
        {
            pos[j][i] = 0;
            if(s[j] != ')' && s[j+1] != '(' && pos[j+2][i-2] == 1) pos[j][i] = 1;
            else if(s[j] != ')' && s[j+i-1] != '(' && pos[j+1][i-2] == 1) pos[j][i] = 1;
            else if(s[j+i-2] != ')' && s[j+i-1] != '(' && pos[j][i-2] == 1) pos[j][i] = 1;

            if(pos[j][i] == 1) ans++;
        }
    }
    std::cout << ans << "\n";

    return 0;
}


#include <iostream>

int N;
std::string s;

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);

    std::cin >> s;
    N = (int)s.size();

    int ans=0, a, b;

    for(int l=0; l<N; l++)
    {
        a=1; b=0;
        if(s[l] == ')') continue;
        for(int r=l+1; r<N; r++)
        {
            if(s[r] == '(') a++;
            else if(s[r] == ')') a--;
            else b++;

            if(b > a)
            {
                a++;
                b--;
            }

            if(a < 0) break;
            if(b >= a && (r-l)%2 == 1) ans++;
        }
    }

    std::cout << ans << "\n";

    return 0;
}


#include <bits/stdc++.h>
#define MAXN 5005

typedef long long int ll;

int N;
std::string s;
int pos[MAXN][MAXN]; //idx, dist

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);

    std::cin >> s;
    N = (int)s.size();

    memset(pos, 0, sizeof pos);
    for(int i=0; i<=N; i++) pos[i][0] = 1;

    ll ans=0;
    for(int i=2; i<=N; i+=2)
    {
        for(int j=0; j+i-1<N; j++)
        {
            if(s[j] != ')' && s[j+1] != '(' && pos[j+2][i-2] == 1) pos[j][i] = 1;
            else if(s[j] != ')' && s[j+i-1] != '(' && pos[j+1][i-2] == 1) pos[j][i] = 1;
            else if(s[j+i-2] != ')' && s[j+i-1] != '(' && pos[j][i-2] == 1) pos[j][i] = 1;

            ans += pos[j][i];
        }
    }
    std::cout << ans << "\n";

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define rep(i,a,b) for (int i=a; i<=b; ++i)
#define per(i,b,a) for (int i=b; i>=a; --i)
#define mes(a,b)  memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MP make_pair
#define PB push_back
#define fi  first
#define se  second
typedef long long ll;
const int N = 5005;

char s[N];
int len, dp[N][N];
bool check(int t1, int t2)
{
    if(s[t1]==')' && s[t2]==')') return false;
    if(s[t1]=='(' && s[t2]=='(') return false;
    if(s[t1]=='(' && s[t2]==')') return true;
    if(s[t1]=='?' && s[t2]==')') return true;
    if(s[t1]=='(' && s[t2]=='?') return true;
    if(s[t1]=='?' && s[t2]=='?') return true;
}
int solve(int t1, int t2)
{
    if(dp[t1][t2]!=-1) return dp[t1][t2];
    if((t2-t1+1)&1) return dp[t1][t2]=0;
    bool flag = check(t1, t2);
    if(!flag) return dp[t1][t2]=0;
    if(t1+1==t2) return dp[t1][t2]=1;
    if(solve(t1+1,t2-1)==1) return dp[t1][t2]=1;
    rep(k,t1+1,t2-1) if(solve(t1,k)==1 && solve(k+1,t2)==1)
        return dp[t1][t2]=1;
    return -1;
}
int main()
{
    mes(dp, -1);
    scanf("%s", s+1);
    len = strlen(s+1);
    ll  ans = 0;
    rep(i,1,len) rep(j,i,len)
        if(solve(i, j)==1) ++ans;
    printf("%lld\n", ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define rep(i,a,b) for (int i=a; i<=b; ++i)
#define per(i,b,a) for (int i=b; i>=a; --i)
#define mes(a,b)  memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MP make_pair
#define PB push_back
#define fi  first
#define se  second
typedef long long ll;
const int N = 5500;

char s[N];
int len, dp[N][N];
bool check(int t1, int t2)
{
    if(s[t1]=='(' && s[t2]==')') return true;
    if(s[t1]=='?' && s[t2]==')') return true;
    if(s[t1]=='(' && s[t2]=='?') return true;
    if(s[t1]=='?' && s[t2]=='?') return true;
    return false;
}
int solve(int t1, int t2)
{
    if(t1>=t2) return dp[t1][t2]=0;
    if(t1<1 || t1>len || t2<1 || t2>len) return dp[t1][t2]=0;
    if(dp[t1][t2]!=-1) return dp[t1][t2];
    if((t2-t1+1)&1) return dp[t1][t2]=0;
    bool flag = check(t1, t2);
    if(!flag) return dp[t1][t2]=0;
    if(t1+1==t2) return dp[t1][t2]=1;
    if(solve(t1+1,t2-1)==1) return dp[t1][t2]=1;
    rep(k,t1+1,t2-2) if(solve(t1,k)==1 && solve(k+1,t2)==1)
        return dp[t1][t2]=1;
    return -1;
}
int main()
{
    scanf("%s", s+1);
    len = strlen(s+1);
    rep(i,1,len) rep(j,1,len) dp[i][j]=-1;
    int  ans = 0;
    rep(i,1,len) rep(j,i+1,len)
        if(solve(i, j)==1) ++ans;
    printf("%d\n", ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define rep(i,a,b) for (int i=a; i<=b; ++i)
#define per(i,b,a) for (int i=b; i>=a; --i)
#define mes(a,b)  memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MP make_pair
#define PB push_back
#define fi  first
#define se  second
typedef long long ll;
const int N = 5005;

char s[N];
int len, dp[N][N];
bool check(int t1, int t2)
{
    if(s[t1]==')' && s[t2]==')') return false;
    if(s[t1]=='(' && s[t2]=='(') return false;
    if(s[t1]=='(' && s[t2]==')') return true;
    if(s[t1]=='?' && s[t2]==')') return true;
    if(s[t1]=='(' && s[t2]=='?') return true;
    if(s[t1]=='?' && s[t2]=='?') return true;
}
int solve(int t1, int t2)
{
    if(dp[t1][t2]!=-1) return dp[t1][t2];
    if((t2-t1+1)&1) return dp[t1][t2]=0;
    bool flag = check(t1, t2);
    if(!flag) return dp[t1][t2]=0;
    if(t1+1==t2) return dp[t1][t2]=1;
    if(solve(t1+1,t2-1)==1) return dp[t1][t2]=1;
    rep(k,t1+1,t2-1) if(solve(t1,k)==1 && solve(k+1,t2)==1)
        return dp[t1][t2]=1;
    return -1;
}
int main()
{
    mes(dp, -1);
    scanf("%s", s+1);
    len = strlen(s+1);
    ll  ans = 0;
    rep(i,1,len) rep(j,1,len)
        if(solve(i, j)==1) ++ans;
    printf("%lld\n", ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define rep(i,a,b) for (int i=a; i<=b; ++i)
#define per(i,b,a) for (int i=b; i>=a; --i)
#define mes(a,b)  memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MP make_pair
#define PB push_back
#define fi  first
#define se  second
typedef long long ll;
const int N = 5500;

char s[N];
int len, dp[N][N];
bool check(int t1, int t2)
{
    if(s[t1]==')' && s[t2]==')') return false;
    if(s[t1]=='(' && s[t2]=='(') return false;
    if(s[t1]=='(' && s[t2]==')') return true;
    if(s[t1]=='?' && s[t2]==')') return true;
    if(s[t1]=='(' && s[t2]=='?') return true;
    if(s[t1]=='?' && s[t2]=='?') return true;
}
int solve(int t1, int t2)
{
    if(dp[t1][t2]!=-1) return dp[t1][t2];
    if((t2-t1+1)&1) return dp[t1][t2]=0;
    bool flag = check(t1, t2);
    if(!flag) return dp[t1][t2]=0;
    if(t1+1==t2) return dp[t1][t2]=1;
    if(solve(t1+1,t2-1)==1) return dp[t1][t2]=1;
    rep(k,t1+1,t2-1) if(solve(t1,k)==1 && solve(k+1,t2)==1)
        return dp[t1][t2]=1;
    return -1;
}
int main()
{
    mes(dp, -1);
    scanf("%s", s+1);
    len = strlen(s+1);
    ll  ans = 0;
    rep(i,1,len) rep(j,1,len)
        if(solve(i, j)==1) ++ans;
    printf("%lld\n", ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define rep(i,a,b) for (int i=a; i<=b; ++i)
#define per(i,b,a) for (int i=b; i>=a; --i)
#define mes(a,b)  memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MP make_pair
#define PB push_back
#define fi  first
#define se  second
typedef long long ll;
const int N = 200005;

char s[N];
int len;
int main()
{
    scanf("%s", s+1);
    len = strlen(s+1);
    ll  ans = 0;
    rep(i,2,len)
    {
        for(int t1=i-1, t2=i; t1>0 && t2<=len; --t1, ++t2)
        {
            if(s[t1]==')' && s[t2]==')') break;
            if(s[t1]=='(' && s[t2]=='(') break;
            if(s[t1]=='(' && s[t2]==')') ans += 1;
            if(s[t1]=='?' && s[t2]==')') ans += 1;
            if(s[t1]=='(' && s[t2]=='?') ans += 1;
            if(s[t1]=='?' && s[t2]=='?') ans += 1;
        }
        //cout<<i<<"  "<<ans<<"  ----\n";
    }
    printf("%lld\n", ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define rep(i,a,b) for (int i=a; i<=b; ++i)
#define per(i,b,a) for (int i=b; i>=a; --i)
#define mes(a,b)  memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MP make_pair
#define PB push_back
#define fi  first
#define se  second
typedef long long ll;
const int N = 5500;

char s[N];
int len, dp[N][N];
bool check(int t1, int t2)
{
    if(s[t1]==')' && s[t2]==')') return false;
    if(s[t1]=='(' && s[t2]=='(') return false;
    if(s[t1]=='(' && s[t2]==')') return true;
    if(s[t1]=='?' && s[t2]==')') return true;
    if(s[t1]=='(' && s[t2]=='?') return true;
    if(s[t1]=='?' && s[t2]=='?') return true;
}
int solve(int t1, int t2)
{
    if(t1>=t2) return dp[t1][t2]=0;
    if(dp[t1][t2]!=-1) return dp[t1][t2];
    if((t2-t1+1)&1) return dp[t1][t2]=0;
    bool flag = check(t1, t2);
    if(!flag) return dp[t1][t2]=0;
    if(t1+1==t2) return dp[t1][t2]=1;
    if(solve(t1+1,t2-1)==1) return dp[t1][t2]=1;
    rep(k,t1+1,t2-1) if(solve(t1,k)==1 && solve(k+1,t2)==1)
        return dp[t1][t2]=1;
    return -1;
}
int main()
{
    mes(dp, -1);
    scanf("%s", s+1);
    len = strlen(s+1);
    ll  ans = 0;
    rep(i,1,len) rep(j,i+1,len)
        if(solve(i, j)==1) ++ans;
    printf("%lld\n", ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define rep(i,a,b) for (int i=a; i<=b; ++i)
#define per(i,b,a) for (int i=b; i>=a; --i)
#define mes(a,b)  memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MP make_pair
#define PB push_back
#define fi  first
#define se  second
typedef long long ll;
const int N = 5500;

char s[N];
int len, dp[N][N];
bool check(int t1, int t2)
{
    if(s[t1]=='(' && s[t2]==')') return true;
    if(s[t1]=='?' && s[t2]==')') return true;
    if(s[t1]=='(' && s[t2]=='?') return true;
    if(s[t1]=='?' && s[t2]=='?') return true;
    return false;
}
int solve(int t1, int t2)
{
    if(t1>=t2) return dp[t1][t2]=0;
    if(t1<1 || t1>len || t2<1 || t2>len) return dp[t1][t2]=0;
    if(dp[t1][t2]!=-1) return dp[t1][t2];
    if((t2-t1+1)&1) return dp[t1][t2]=0;
    bool flag = check(t1, t2);
    if(!flag) return dp[t1][t2]=0;
    if(t1+1==t2) return dp[t1][t2]=1;
    if(solve(t1+1,t2-1)==1) return dp[t1][t2]=1;
    rep(k,t1+1,t2-2) if(solve(t1,k)==1 && solve(k+1,t2)==1)
        return dp[t1][t2]=1;
    return dp[t1][t2]=-2;
}
int main()
{
    scanf("%s", s+1);
    len = strlen(s+1);
    rep(i,1,len) rep(j,1,len) dp[i][j]=-1;
    int  ans = 0;
    rep(i,1,len) rep(j,i+1,len)
        if(solve(i, j)==1) ++ans;
    printf("%d\n", ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define rep(i,a,b) for (int i=a; i<=b; ++i)
#define per(i,b,a) for (int i=b; i>=a; --i)
#define mes(a,b)  memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MP make_pair
#define PB push_back
#define fi  first
#define se  second
typedef long long ll;
const int N = 5500;

char s[N];
int len, dp[N][N];
bool check(int t1, int t2)
{
    if(s[t1]==')' && s[t2]==')') return false;
    if(s[t1]=='(' && s[t2]=='(') return false;
    if(s[t1]=='(' && s[t2]==')') return true;
    if(s[t1]=='?' && s[t2]==')') return true;
    if(s[t1]=='(' && s[t2]=='?') return true;
    if(s[t1]=='?' && s[t2]=='?') return true;
}
int solve(int t1, int t2)
{
    if(t1>=t2) return dp[t1][t2]=0;
    if(dp[t1][t2]!=-1) return dp[t1][t2];
    if((t2-t1+1)&1) return dp[t1][t2]=0;
    bool flag = check(t1, t2);
    if(!flag) return dp[t1][t2]=0;
    if(t1+1==t2) return dp[t1][t2]=1;
    if(solve(t1+1,t2-1)==1) return dp[t1][t2]=1;
    rep(k,t1+1,t2-1) if(solve(t1,k)==1 && solve(k+1,t2)==1)
        return dp[t1][t2]=1;
    return -1;
}
int main()
{

    scanf("%s", s+1);
    len = strlen(s+1);
    rep(i,1,len) rep(j,1,len) dp[i][j]=-1;
    int  ans = 0;
    rep(i,1,len) rep(j,i+1,len)
        if(solve(i, j)==1) ++ans;
    printf("%d\n", ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define rep(i,a,b) for (int i=a; i<=b; ++i)
#define per(i,b,a) for (int i=b; i>=a; --i)
#define mes(a,b)  memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MP make_pair
#define PB push_back
#define fi  first
#define se  second
typedef long long ll;
const int N = 5005;

int len, l[N], r[N];
char s[N];
int main()
{
    scanf("%s", s+1);
    len = strlen(s+1);
    rep(i,1,len)
    {
        r[i] = len+1;
        int cnt = 0;
        rep(j,i,len)
        {
            if(s[j]==')') --cnt;
            else ++cnt;
            if(cnt<0) { r[i]=j; break; }
        }
    }
    per(i,len,1)
    {
        l[i] = 0;
        int cnt = 0;
        per(j,i,1)
        {
            if(s[j]=='(') --cnt;
            else ++cnt;
            if(cnt<0) { l[i]=j; break; }
        }
    }
    ll  ans = 0;
    rep(i,1,len)
    {
        for(int j=i+1; j<=len; j+=2)
        {
            if(r[i]>j && l[j]<i) ++ans;
        }
    }
    printf("%lld\n", ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define rep(i,a,b) for (int i=a; i<=b; ++i)
#define per(i,b,a) for (int i=b; i>=a; --i)
#define mes(a,b)  memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MP make_pair
#define PB push_back
#define fi  first
#define se  second
typedef long long ll;
const int N = 5500;

char s[N];
int len, dp[N][N];
bool check(int t1, int t2)
{
    if(s[t1]==')' && s[t2]==')') return false;
    if(s[t1]=='(' && s[t2]=='(') return false;
    if(s[t1]=='(' && s[t2]==')') return true;
    if(s[t1]=='?' && s[t2]==')') return true;
    if(s[t1]=='(' && s[t2]=='?') return true;
    if(s[t1]=='?' && s[t2]=='?') return true;
}
int solve(int t1, int t2)
{
    if(t1>=t2) return dp[t1][t2]=0;
    if(t1<1 || t1>len || t2<1 || t2>len) return dp[t1][t2]=0;
    if(dp[t1][t2]!=-1) return dp[t1][t2];
    if((t2-t1+1)&1) return dp[t1][t2]=0;
    bool flag = check(t1, t2);
    if(!flag) return dp[t1][t2]=0;
    if(t1+1==t2) return dp[t1][t2]=1;
    if(solve(t1+1,t2-1)==1) return dp[t1][t2]=1;
    rep(k,t1+1,t2-2) if(solve(t1,k)==1 && solve(k+1,t2)==1)
        return dp[t1][t2]=1;
    return -1;
}
int main()
{
    scanf("%s", s+1);
    len = strlen(s+1);
    rep(i,1,len) rep(j,1,len) dp[i][j]=-1;
    int  ans = 0;
    rep(i,1,len) rep(j,i+1,len)
        if(solve(i, j)==1) ++ans;
    printf("%d\n", ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define rep(i,a,b) for (int i=a; i<=b; ++i)
#define per(i,b,a) for (int i=b; i>=a; --i)
#define mes(a,b)  memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MP make_pair
#define PB push_back
#define fi  first
#define se  second
typedef long long ll;
const int N = 5500;

char s[N];
int len, dp[N][N];
bool check(int t1, int t2)
{
    if(s[t1]==')' && s[t2]==')') return false;
    if(s[t1]=='(' && s[t2]=='(') return false;
    if(s[t1]=='(' && s[t2]==')') return true;
    if(s[t1]=='?' && s[t2]==')') return true;
    if(s[t1]=='(' && s[t2]=='?') return true;
    if(s[t1]=='?' && s[t2]=='?') return true;
}
int solve(int t1, int t2)
{
    if(t1>=t2) return dp[t1][t2]=0;
    if(t1<1 || t1>len || t2<1 || t2>len) return dp[t1][t2]=0;
    if(dp[t1][t2]!=-1) return dp[t1][t2];
    if((t2-t1+1)&1) return dp[t1][t2]=0;
    bool flag = check(t1, t2);
    if(!flag) return dp[t1][t2]=0;
    if(t1+1==t2) return dp[t1][t2]=1;
    if(solve(t1+1,t2-1)==1) return dp[t1][t2]=1;
    rep(k,t1+1,t2-2) if(solve(t1,k)==1 && solve(k+1,t2)==1)
        return dp[t1][t2]=1;
    return -1;
}
int main()
{
    scanf("%s", s+1);
    len = strlen(s+1);
    rep(i,1,len) rep(j,1,len) dp[i][j]=-1;
    int  ans = 0;
    rep(i,1,len) rep(j,i+1,len)
        if(solve(i, j)==1) ++ans;
    printf("%d\n", ans);
    if(ans==9) {
        rep(i,1,len) rep(j,i+1,len)
            cout<<i<<" "<<j<<" "<<dp[i][j]<<"  ---\n";
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> PII;

const int M =  1e9+7;
const int N = 5010;

char s[N];

int dp[N][N];

int main(){
    scanf("%s", s);
    int len = strlen(s);
    for(int i = 0; i < len; i++){
        stack<char> st;
        st.push(s[i]);
        for(int j = i+1; j < len; j++){
            if(s[j] == '(') st.push(s[j]);
            else if(s[j] == ')'){
                if(!st.empty() && st.top() != ')') {
                    st.pop();
                    if(st.empty()) dp[i][j] = 1;
                }else{
                    st.push(')');
                }
            }
            else {
                if(!st.empty() && st.top() != ')') {
                    st.pop();
                    if(st.empty()) dp[i][j] = 1;
                }else{
                    st.push('?');
                }
            }
        }
        //cout << i << "##" << ans << endl;
    }
    for(int i = len - 1; i >= 0; i--){
        stack<char> st;
        st.push(s[i]);
        for(int j = i - 1; j >= 0; j--){
            if(s[j] == ')') st.push(s[j]);
            else if(s[j] == '('){
                if(!st.empty() && st.top() != '(') {
                    st.pop();
                    if(st.empty()) dp[j][i] = 1;
                }else{
                    st.push('(');
                }
            }
            else {
                if(!st.empty() && st.top() != '(') {
                    st.pop();
                    if(st.empty()) dp[j][i] = 1;
                }else{
                    st.push('?');
                }
            }
        }
    }
    int ans = 0;
    for(int i = 0; i < len; i++){
        for(int j = i + 1; j < len; j++) {
            ans += dp[i][j];
          //  if(dp[i][j]) cout << i << " " << j << endl;
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> PII;

const int M =  1e9+7;
const int N = 5010;

char s[N];

int dp[N][N];
vector<int> g[N];

int main(){
    scanf("%s", s);
    int len = strlen(s);
    for(int i = 0; i < len; i++){
        stack<char> st;
        st.push(s[i]);
        for(int j = i+1; j < len; j++){
            if(s[j] == '(') st.push(s[j]);
            else if(s[j] == ')'){
                if(!st.empty() && st.top() != ')') {
                    st.pop();
                    if(st.empty()) dp[i][j] = 1;
                }else{
                    st.push(')');
                }
            }
            else {
                if(!st.empty() && st.top() != ')') {
                    st.pop();
                    if(st.empty()) dp[i][j] = 1;
                }else{
                    st.push('?');
                }
            }
        }
        //cout << i << "##" << ans << endl;
    }
    for(int i = len - 1; i >= 0; i--){
        stack<char> st;
        st.push(s[i]);
        for(int j = i - 1; j >= 0; j--){
            if(s[j] == ')') st.push(s[j]);
            else if(s[j] == '('){
                if(!st.empty() && st.top() != '(') {
                    st.pop();
                    if(st.empty()) dp[j][i] = 1;
                }else{
                    st.push('(');
                }
            }
            else {
                if(!st.empty() && st.top() != '(') {
                    st.pop();
                    if(st.empty()) dp[j][i] = 1;
                }else{
                    st.push('?');
                }
            }
        }
    }
    int ans = 0;
    for(int i = 0; i < len; i++){
        for(int j = i + 1; j < len; j++) {
            if(dp[i][j]) g[i].push_back(j);
        }
    }
    for(int i = 0; i < len; i++){
        for(auto j: g[i]){
            for(int k: g[j+1]){
                dp[i][k] = 1;
            }
        }
    }
    for(int i = 0; i < len; i++){
        for(int j = i+1; j < len; j++) ans += dp[i][j];
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> PII;

const int M =  1e9+7;
const int N = 5010;

char s[N];
bool ok[N][N];

int main(){
    scanf("%s", s);
    int len = strlen(s);
    for(int i = 0; i < len; i++){
        int tmp = 0;
        for(int j = i; j < len; j++){
            if(s[j] == ')') tmp--;
            else tmp++;
            if(tmp < 0) break;
            ok[i][j] = 1;
        }
    }

    int ans = 0;
    for(int i = len - 1; i >= 0; i--){
        int tmp = 0;
        for(int j = i; j >= 0; j--){
            if(s[j] == '(') tmp--;
            else tmp++;
            if(tmp < 0) break;
            ok[j][i] &= 1;
            if(ok[j][i] && ((i - j + 1) % 2 == 0) ) ans++;
        }
    }
    cout << ans << endl;
    return 0;
}

// ((?)(?))


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> PII;

const int M =  1e9+7;
const int N = 5010;

char s[N];

int dp[N][N];
vector<int> g[N];

int main(){
    scanf("%s", s);
    int len = strlen(s);
    for(int i = 0; i < len; i++){
        stack<char> st;
        st.push(s[i]);
        for(int j = i+1; j < len; j++){
            if(s[j] == '(') {
                if(!st.empty() && st.top() != '('){
                    st.pop();
                    if(st.empty()) dp[i][j] = 1;
                }
                else st.push(s[j]);
            }
            else if(s[j] == ')'){
                if(!st.empty() && st.top() != ')') {
                    st.pop();
                    if(st.empty()) dp[i][j] = 1;
                }else{
                    st.push(')');
                }
            }
            else {
                if(!st.empty() && st.top() != ')') {
                    st.pop();
                    if(st.empty()) dp[i][j] = 1;
                }else{
                    st.push('?');
                }
            }
        }
        //cout << i << "##" << ans << endl;
    }
    for(int i = len - 1; i >= 0; i--){
        stack<char> st;
        st.push(s[i]);
        for(int j = i - 1; j >= 0; j--){
            if(s[j] == ')') {
                if(!st.empty() && st.top() != ')'){
                    st.pop();
                    if(st.empty()) dp[j][i] = 1;
                }
                else st.push(s[j]);
            }
            else if(s[j] == '('){
                if(!st.empty() && st.top() != '(') {
                    st.pop();
                    if(st.empty()) dp[j][i] = 1;
                }else{
                    st.push('(');
                }
            }
            else {
                if(!st.empty() && st.top() != '(') {
                    st.pop();
                    if(st.empty()) dp[j][i] = 1;
                }else{
                    st.push('?');
                }
            }
        }
    }
    int ans = 0;
    for(int i = 0; i < len; i++){
        for(int j = i + 1; j < len; j++) {
            if(dp[i][j]) g[i].push_back(j);
        }
    }
    for(int i = 0; i < len; i++){
        for(auto j: g[i]){
            for(int k: g[j+1]){
                dp[i][k] = 1;
            }
        }
    }
    for(int i = 0; i < len; i++){
        for(int j = i+1; j < len; j++) ans += dp[i][j];
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <list>
#include <unordered_map>
#include <bitset>
#include <algorithm>
#define ll long long int
#define e() cout << endl;
#define MA INT_MAX
#define MI INT_MIN
#define MOD 1000000007;
#define ppi pair <int,int>
#define scn(n) scanf("%d",&n)
#define vvi(n,m,x) vector <vector <int> > v(n,vector <int> (m,x));
#define cp(x,y) cout << "Case : " << x << " " << y << endl;
using namespace std;
inline bool check(string &t,int l,int r)
{
	stack <int> s,st;
	for(int i = l;i <= r;i++)
	{
		if(t[i] == '(')
			s.push(i);
		else if(t[i] == ')')
			if(!s.empty())
				s.pop();
			else if(!st.empty())
				st.pop();
			else
				return false;
		else
			st.push(i);
	}

	while(!s.empty())
	{
		if(st.empty())
			return false;
		int x = s.top(),y = st.top();
		if(x > y)
			return false;
		s.pop();
		st.pop();
	}
	if(st.size() % 2)
		return false;
	return true;
}
int main()
{
	string s;
	cin >> s;
	int n = s.size();

	int curr = 0,cnt = 0,res = 0;
	for(int i = 0;i < n;i++)
	{
		for(int j = i;j < n;j++)
		{
			bool ok = check(s,i,j);
			res += (ok == true);
		}
	}
	printf("%d\n",res);
	return 0;
}


#include<cstdio>
#include<cstring>
#include <iostream>
using namespace std;
char s[5050];
int main(){
	scanf("%s",s);
	int i,j,l,r,n,ans=0;
	n=strlen(s);
	for (i=0; i<n; i++){
		l=0,r=0;
		for (j=i; j<n; j++){
			(s[j]=='(' ? l++ : l--);
			// this counts both ( and ? ig because each time it is
			(s[j]==')' ? r-- : r++);
			if (l<0) l=1;
			if (r<0) break;
			if (l==0) ans++;
		//	cout << i << " " << j << " " << l << " " << r << " " << ans << endl;
		}
	//	cout << endl;
	}
	printf("%d\n",ans);
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <list>
#include <unordered_map>
#include <bitset>
#include <algorithm>
#define ll long long int
#define e() cout << endl;
#define MA INT_MAX
#define MI INT_MIN
#define MOD 1000000007;
#define ppi pair <int,int>
#define scn(n) scanf("%d",&n)
#define vvi(n,m,x) vector <vector <int> > v(n,vector <int> (m,x));
#define cp(x,y) cout << "Case : " << x << " " << y << endl;
using namespace std;
bool check(string t,int l,int r)
{
	stack <int> s,st;
	for(int i = l;i <= r;i++)
	{
		if(t[i] == '(')
			s.push(i);
		else if(t[i] == ')')
			if(!s.empty())
				s.pop();
			else if(!st.empty())
				st.pop();
			else
				return false;
		else
			st.push(i);
	}

	while(!s.empty())
	{
		if(st.empty())
			return false;
		int x = s.top(),y = st.top();
		if(x > y)
			return false;
		s.pop();
		st.pop();
	}
	if(st.size() % 2)
		return false;
	return true;
}
int main()
{
	string s;
	cin >> s;
	int n = s.size();

	int curr = 0,cnt = 0,res = 0;
	for(int i = 0;i < n;i++)
	{
		for(int j = i;j < n;j++)
		{
			bool ok = check(s,i,j);
			res += (ok == true);
		///	cout << i << " " << j << " " << ok << endl;
		}
	}
	cout << res << endl;
	return 0;
}


/// #bettercoderthanshebeautiful
/// Now on codeforces too
/// 54h 20min
#include<bits/stdc++.h>
using namespace std;
int sol;
char q[5002];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>q+1;
    int n=strlen(q+1);
    for(int i=1;i<n;++i)
    {
        int l=0,r=0;
        for(int j=i;j<=n;++j)
        {
            if(q[j]=='(')
                ++l,++r;
            if(q[j]==')')
                --l,--r;
            if(q[j]=='?')
                --l,++r;
            if(l<0)
                l+=2;
            if(r<0)
                break;
            if(l==0)
                ++sol;
        }
    }
    cout<<sol;
    return 0;
}


/// #bettercoderthanshebeautiful
/// Now on codeforces too
/// 54h 20min
#include<bits/stdc++.h>
using namespace std;
int sol;
char q[5002];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>q+1;
    int n=strlen(q+1);
    for(int i=1;i<n;++i)
    {
        int st=0;
        int qm=0;
        for(int j=i;j<=n;++j)
        {
            if(q[j]=='(')
            {
                ++st;
                continue;
            }
            if(q[j]==')')
            {
                if(st)
                    --st;
                else
                    if(qm)
                        --qm;
                    else
                        break;
            }
            if(q[j]=='?')
                ++qm;
            if((j-i+1)%2==0)
                if(q[j]==')' && qm>=st)
                    ++sol;
                else
                    if(q[j]=='?' && qm>=st)
                        ++sol;
        }
    }
    cout<<sol;
    return 0;
}


/// #bettercoderthanshebeautiful
/// Now on codeforces too
/// 55h 20min
#include<bits/stdc++.h>
using namespace std;
int sol;
char q[5002];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>q+1;
    int n=strlen(q+1);
    for(int i=1;i<n;++i)
    {
        int st=0;
        int dr=0;
        int qm=0;
        for(int j=i;j<=n;++j)
        {
            if(q[j]=='(')
                ++st;
            if(q[j]==')')
                ++dr;
            if(q[j]=='?')
                ++qm;
            if(st+qm<dr)
                break;
            if(q[j]==')' || q[j]=='?')
                if((st+dr+qm)%2==0 && abs(st-dr)<=qm)
                {
                    ++sol;
                  //  cout<<i<<" "<<j<<'\n';
                }
        }
    }
    cout<<sol;
    return 0;
}


/// #bettercoderthanshebeautiful
/// Now on codeforces too
/// 55h 20min
#include<bits/stdc++.h>
using namespace std;
int sol;
char q[5002];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>q+1;
    int n=strlen(q+1);
    for(int i=1;i<n;++i)
    {
        int st=0;
        int dr=0;
        int qm=0;
        for(int j=i;j<=n;++j)
        {
            if(q[j]=='(')
                ++st;
            if(q[j]==')')
            {
                if(st)
                    --st;
                else
                    if(qm)
                        --qm;
                    else
                        break;
            }
            if(q[j]=='?')
                if(st==0)
                    ++st;
                else
                    ++qm;
            if(st+qm<dr)
                break;
            if((st+dr+qm)%2==0)
                if(q[j]==')'){
                    if(qm>=st)
                    {
                        ++sol;
                   //     cout<<i<<" "<<j<<'\n';
                    }
                }
                else
                    if(qm)
                    {
                        --qm;
                        --st;
                      //  cout<<st<<" "<<qm<<'\n';
                        if(qm>=st)
                        {
                            ++sol;
                       //     cout<<i<<" "<<j<<'\n';
                        }
                        ++qm;
                        ++st;
                    }
        }
    }
    cout<<sol;
    return 0;
}


/// #bettercoderthanshebeautiful
/// Now on codeforces too
/// 55h 20min
#include<bits/stdc++.h>
using namespace std;
int sol;
char q[5002];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>q+1;
    int n=strlen(q+1);
    for(int i=1;i<n;++i)
    {
        int st=0;
        int qm=0;
        for(int j=i;j<=n;++j)
        {
            if(q[j]=='(')
                ++st;
            if(q[j]==')')
            {
                if(st)
                    --st;
                else
                    if(qm)
                        --qm;
                    else
                        break;
            }
            if(q[j]=='?')
                if(st==0)
                    ++st;
                else
                    ++qm;
            if((st+dr+qm)%2==0)
                if(q[j]==')')
                {
                    if(qm>=st)
                    {
                        ++sol;
                   //     cout<<i<<" "<<j<<'\n';
                    }
                }
                else
                    if(q[j]=='?' && qm)
                    {
                        --qm;
                        --st;
                      //  cout<<st<<" "<<qm<<'\n';
                        if(qm>=st)
                        {
                            ++sol;
                       //     cout<<i<<" "<<j<<'\n';
                        }
                        ++qm;
                        ++st;
                    }
        }
    }
    cout<<sol;
    return 0;
}


/// #bettercoderthanshebeautiful
/// Now on codeforces too
/// 55h 20min
#include<bits/stdc++.h>
using namespace std;
int sol;
char q[5002];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>q+1;
    int n=strlen(q+1);
    for(int i=1;i<n;++i)
    {
        int st=0;
        int qm=0;
        for(int j=i;j<=n;++j)
        {
            if(q[j]=='(')
                ++st;
            if(q[j]==')')
            {
                if(st)
                    --st;
                else
                    if(qm)
                        --qm;
                    else
                        break;
            }
            if(q[j]=='?')
                if(st==0)
                    ++st;
                else
                    ++qm;
            if((j-i+1)%2==0)
                if(q[j]==')')
                {
                    if(qm>=st)
                    {
                        ++sol;
                   //     cout<<i<<" "<<j<<'\n';
                    }
                }
                else
                    if(q[j]=='?' && qm)
                    {
                        --qm;
                        --st;
                      //  cout<<st<<" "<<qm<<'\n';
                        if(qm>=st)
                        {
                            ++sol;
                       //     cout<<i<<" "<<j<<'\n';
                        }
                        ++qm;
                        ++st;
                    }
        }
    }
    cout<<sol;
    return 0;
}


/// #bettercoderthanshebeautiful
/// Now on codeforces too
/// 54h 20min
#include<bits/stdc++.h>
using namespace std;
int sol;
char q[5002];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>q+1;
    int n=strlen(q+1);
    for(int i=1;i<n;++i)
    {
        int st=0;
        int qm=0;
        for(int j=i;j<=n;++j)
        {
            if(q[j]=='(')
                ++st;
            if(q[j]==')')
            {
                if(st)
                    --st;
                else
                    if(qm)
                        --qm;
                    else
                        break;
            }
            if(q[j]=='?')
                if(st==0 && !qm)
                    ++st;
                else
                    ++qm;
            if((j-i+1)%2==0)
                if(q[j]==')')
                {
                    if(qm>=st)
                        ++sol;
                }
                else
                    if(q[j]=='?' && qm>=st)
                        ++sol;
        }
    }
    cout<<sol;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	string s;
	cin >> s;
	int n = s.size(), tot = 0;
	for (int i = 0; i < n; i++) {
		int b,q;
		if (s[i] == ')') continue;
		else {
			b = 1;
			q = 0;
		}
		for (int j = i+1; j < n; j++) {
			if (s[j] == ')') b--;
			else if (s[j] == '(') b++;
			else q++;
			//if (b[j] >= 0 && q[j] > b[j]) {
			while (q > b) {
				b++;
				q--;
			}
			if ((j-i+1) % 2 != 0) continue;
			if (q + b < 0) break;
			if (q >= abs(b)) tot++;

		}
	}
	cout << tot << endl;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
	string s;
	cin >> s;
	int n = s.size(), tot = 0;
	for (int i = 0; i < n; i++) {
		int b,q;
		if (s[i] == ')') continue;
		else {
			b = 1;
			q = 0;
		}
		for (int j = i+1; j < n; j++) {
			if (s[j] == ')') b--;
			else if (s[j] == '(') b++;
			else q++;
			//if (b[j] >= 0 && q[j] > b[j]) {
			while (q > b) {
				b++;
				q--;
			}
			if (q + b < 0) break;
			if ((j-i+1) % 2 != 0) continue;
			//printf("%d %d %d %d\n", i, j, q, b);
			if (q >= abs(b)) tot++;

		}
	}
	cout << tot << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define fr(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define frr(i,a,b) for (int i = (a), _b = (b); i >= _b; i--)
#define rep(i,n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (int i = (n) - 1; i >= 0; i--)
#define foreach(it, ar) for ( typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++ )
#define fill(ar, val) memset(ar, val, sizeof(ar))

#define ull unsigned long long
#define ll long long
#define all(ar) ar.begin(), ar.end()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;

#define PI  3.1415926535897932385
#define EPS 1e-7
#define MOD 1000000007
#define INF 1500111222
#define N 100111

int n;
string s;

bool valid(int a, int b, int c) {
    int sum = a + b + c;
    if (sum & 1) return false;
    return a + c >= b && b + c >= a;
}

bool con(int a, int b, int c) {
    return a + c >= b;
}

int solve() {
    int res = 0;
    rep(i, n) {
        int a = 0, b = 0, c = 0;
        fr(j, i, n - 1) {
            if (s[j] == '(') a++;
            else if (s[j] == ')') b++;
            else if (s[j] == '?') c++;

            if (!con(a, b, c)) break;
            if (j == i && s[j] == '?') {
                a++; c--;
            } else if (s[j] == '?' || s[j] == ')') {
                if (a < b) {
                    int tmp = b - a;
                    a += tmp;
                    c -= tmp;
                    if (c < 0) break;
                }
            }

            if (s[j] == ')') {
                if (valid(a, b, c)) res++;
            } else if (s[j] == '?') {
                if (valid(a, b + 1, c - 1)) res++;
            }
        }
    }
    return res;
}

int main() {
    //ios::sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
       // freopen("inp.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
    #endif
    cin >> s;
    n = s.size();
    cout << solve() << endl;
    return 0;
}

// lamphanviet@gmail.com


#include <bits/stdc++.h>
using namespace std;

#define fr(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define frr(i,a,b) for (int i = (a), _b = (b); i >= _b; i--)
#define rep(i,n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (int i = (n) - 1; i >= 0; i--)
#define foreach(it, ar) for ( typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++ )
#define fill(ar, val) memset(ar, val, sizeof(ar))

#define ull unsigned long long
#define ll long long
#define all(ar) ar.begin(), ar.end()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;

#define PI  3.1415926535897932385
#define EPS 1e-7
#define MOD 1000000007
#define INF 1500111222
#define N 100111

int n;
string s;

bool valid(int a, int b, int c) {
    int sum = a + b + c;
    if (sum & 1) return false;
    return a + c >= b && b + c >= a;
}

bool con(int a, int b, int c) {
    return a + c >= b;
}

int solve() {
    int res = 0;
    rep(i, n) {
        int a = 0, b = 0, c = 0;
        fr(j, i, n - 1) {
            if (s[j] == '(') a++;
            else if (s[j] == ')') b++;
            else if (s[j] == '?') c++;

            if (!con(a, b, c)) break;
            if (j == i && s[j] == '?') {
                a++; c--;
            } else if (s[j] == '?' || s[j] == ')') {
                if (a < b) {
                    int tmp = b - a;
                    a += tmp;
                    c -= tmp;
                }
            }

            if (s[j] == ')') {
                if (valid(a, b, c)) res++;
            } else if (s[j] == '?') {
                if (c && valid(a, b + 1, c - 1)) res++;
                else if (!c && valid(a, b, c)) res++;
            }
        }
    }
    return res;
}

int main() {
    //ios::sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
       // freopen("inp.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
    #endif
    cin >> s;
    n = s.size();
    cout << solve() << endl;
    return 0;
}

// lamphanviet@gmail.com


#include <bits/stdc++.h>
using namespace std;

#define fr(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define frr(i,a,b) for (int i = (a), _b = (b); i >= _b; i--)
#define rep(i,n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (int i = (n) - 1; i >= 0; i--)
#define foreach(it, ar) for ( typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++ )
#define fill(ar, val) memset(ar, val, sizeof(ar))

#define ull unsigned long long
#define ll long long
#define all(ar) ar.begin(), ar.end()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;

#define PI  3.1415926535897932385
#define EPS 1e-7
#define MOD 1000000007
#define INF 1500111222
#define N 100111

int n;
string s;

bool valid(int a, int b, int c) {
    int sum = a + b + c;
    if (sum & 1) return false;
    return a + c >= b && b + c >= a;
}

bool con(int a, int b, int c) {
    return a + c >= b;
}

int solve() {
    int res = 0;
    rep(i, n) {
        int a = 0, b = 0, c = 0;
        fr(j, i, n - 1) {
            if (s[j] == '(') a++;
            else if (s[j] == ')') b++;
            else if (s[j] == '?') c++;
            if (j == i && s[j] == '?') {
                c = 0;
                a = 1;
            }

            if (!con(a, b, c)) break;

            if (s[j] == ')') {
                if (valid(a, b, c)) res++;
            } else if (s[j] == '?') {
                if (valid(a, b + 1, c - 1)) res++;
            }
        }
    }
    return res;
}

int main() {
    //ios::sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
        //freopen("inp.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
    #endif
    cin >> s;
    n = s.size();
    cout << solve() << endl;
    return 0;
}

// lamphanviet@gmail.com


#include <bits/stdc++.h>
using namespace std;

#define fr(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define frr(i,a,b) for (int i = (a), _b = (b); i >= _b; i--)
#define rep(i,n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (int i = (n) - 1; i >= 0; i--)
#define foreach(it, ar) for ( typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++ )
#define fill(ar, val) memset(ar, val, sizeof(ar))

#define ull unsigned long long
#define ll long long
#define all(ar) ar.begin(), ar.end()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;

#define PI  3.1415926535897932385
#define EPS 1e-7
#define MOD 1000000007
#define INF 1500111222
#define N 100111

int n;
string s;

bool valid(int a, int b, int c) {
    int sum = a + b + c;
    if (sum & 1) return false;
    return a + c >= b && b + c >= a;
}

bool con(int a, int b, int c) {
    return a + c >= b;
}

int solve() {
    int res = 0;
    rep(i, n) {
        int a = 0, b = 0, c = 0;
        fr(j, i, n - 1) {
            if (s[j] == '(') a++;
            else if (s[j] == ')') b++;
            else if (s[j] == '?') c++;

            if (!con(a, b, c)) break;

            if (s[j] == ')') {
                if (valid(a, b, c)) res++;
            } else if (s[j] == '?') {
                if (valid(a, b + 1, c - 1)) res++;
            }
        }
    }
    return res;
}

int main() {
    //ios::sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
        //freopen("inp.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
    #endif
    cin >> s;
    n = s.size();
    cout << solve() << endl;
    return 0;
}

// lamphanviet@gmail.com


#include <bits/stdc++.h>
using namespace std;

#define fr(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define frr(i,a,b) for (int i = (a), _b = (b); i >= _b; i--)
#define rep(i,n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (int i = (n) - 1; i >= 0; i--)
#define foreach(it, ar) for ( typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++ )
#define fill(ar, val) memset(ar, val, sizeof(ar))

#define ull unsigned long long
#define ll long long
#define all(ar) ar.begin(), ar.end()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;

#define PI  3.1415926535897932385
#define EPS 1e-7
#define MOD 1000000007
#define INF 1500111222
#define N 100111

int n;
string s;

bool valid(int a, int b, int c) {
    int sum = a + b + c;
    if (sum & 1) return false;
    return a + c >= b && b + c >= a;
}

bool con(int a, int b, int c) {
    return a + c >= b;
}

int solve() {
    int res = 0;
    rep(i, n) {
        int a = 0, b = 0, c = 0;
        fr(j, i, n - 1) {
            if (s[j] == '(') a++;
            else if (s[j] == ')') b++;
            else if (s[j] == '?') c++;

            if (!con(a, b, c)) break;
            if (j == i && s[j] == '?') {
                a++; c--;
            } else if (s[j] == '?' || s[j] == ')') {
                if (a < b) {
                    int tmp = b - a;
                    a += tmp;
                    c -= tmp;
                }
            }

            if (s[j] == ')') {
                if (valid(a, b, c)) res++;
            } else if (s[j] == '?') {
                if (valid(a, b + 1, c - 1)) res++;
            }
        }
    }
    return res;
}

int main() {
    //ios::sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
        //freopen("inp.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
    #endif
    cin >> s;
    n = s.size();
    cout << solve() << endl;
    return 0;
}

// lamphanviet@gmail.com


#include <bits/stdc++.h>
using namespace std;

#define fr(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define frr(i,a,b) for (int i = (a), _b = (b); i >= _b; i--)
#define rep(i,n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (int i = (n) - 1; i >= 0; i--)
#define foreach(it, ar) for ( typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++ )
#define fill(ar, val) memset(ar, val, sizeof(ar))

#define ull unsigned long long
#define ll long long
#define all(ar) ar.begin(), ar.end()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;

#define PI  3.1415926535897932385
#define EPS 1e-7
#define MOD 1000000007
#define INF 1500111222
#define N 100111

int n;
string s;

bool valid(int a, int b, int c) {
    int sum = a + b + c;
    if (sum & 1) return false;
    return a + c >= b && b + c >= a;
}

bool con(int a, int b, int c) {
    return a + c >= b;
}

int solve() {
    int res = 0;
    rep(i, n) {
        int a = 0, b = 0, c = 0;
        fr(j, i, n - 1) {
            if (s[j] == ')') {
            	b++;
            	if (a < c + b) {
            		if (c) {
            			a++; c--;
            		} else break;
            	}
            } else if (s[j] == '(') {
            	a++;
            } else if (s[j] == '?') {
            	c++;
            	if (a < c + b) {
            		a++; c--;
            	}
            }

            if (s[j] == ')') {
                if (valid(a, b, c)) res++;
            } else if (s[j] == '?') {
                if (c && valid(a, b + 1, c - 1)) res++;
                else if (!c && valid(a, b, c)) res++;
            }
        }
    }
    return res;
}

int main() {
    //ios::sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
       // freopen("inp.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
    #endif
    cin >> s;
    n = s.size();
    cout << solve() << endl;
    return 0;
}

// lamphanviet@gmail.com


#include <bits/stdc++.h>
using namespace std;

#define fr(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define frr(i,a,b) for (int i = (a), _b = (b); i >= _b; i--)
#define rep(i,n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (int i = (n) - 1; i >= 0; i--)
#define foreach(it, ar) for ( typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++ )
#define fill(ar, val) memset(ar, val, sizeof(ar))

#define ull unsigned long long
#define ll long long
#define all(ar) ar.begin(), ar.end()
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;

#define PI  3.1415926535897932385
#define EPS 1e-7
#define MOD 1000000007
#define INF 1500111222
#define N 100111

int n;
string s;

bool valid(int a, int b, int c) {
    int sum = a + b + c;
    if (sum & 1) return false;
    return a + c >= b && b + c >= a;
}

bool con(int a, int b, int c) {
    return a + c >= b;
}

int solve() {
    int res = 0;
    rep(i, n) {
        int a = 0, b = 0, c = 0;
        fr(j, i, n - 1) {
            if (s[j] == '(') a++;
            else if (s[j] == ')') b++;
            else if (s[j] == '?') c++;

            if (!con(a, b, c)) break;
            if (j == i && s[j] == '?') {
                a++; c--;
            } else if (s[j] == '?') {
                if (a < b) {
                    int tmp = b - a;
                    a += tmp;
                    c -= tmp;
                }
            }

            if (s[j] == ')') {
                if (valid(a, b, c)) res++;
            } else if (s[j] == '?') {
                if (valid(a, b + 1, c - 1)) res++;
            }
        }
    }
    return res;
}

int main() {
    //ios::sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
        //freopen("inp.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
    #endif
    cin >> s;
    n = s.size();
    cout << solve() << endl;
    return 0;
}

// lamphanviet@gmail.com


#include<bits/stdc++.h>
#define fi first
#define se second
#define lson l,mid,o<<1
#define rson mid+1,r,o<<1|1
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> P;
typedef pair<P, int> PII;
const LL INF = 0x3f3f3f3f;
const int N = 5e3 + 10;
const LL mod = 1e9 + 7;
const double PI=acos(-1);

char s[N];
int main()
{
    //ios::sync_with_stdio(false);
    scanf("%s", s);
    int len = strlen(s);
    int l, mid, r, ans = 0, zz;
    for(int i = 0; i < len; ++i){
        l = mid = r = 0;
        for(int j = i; j < len; ++j){
            if(s[j] == '(') l++;
            else if(s[j] == ')') r++;
            else{
                if(l == mid)    l++;
                else    mid++;
            }
            if(mid + r > l){
                if(mid){
                    mid--, l++;
                }
                else    break;
            }
         //  printf("i = %d, j = %d, l = %d, r = %d, mid = %d\n", i, j, l, r, mid);
            if((j - i + 1) % 2 == 0){
                if(l <= mid + r) {
                    ans++;
                   // cout << "ok" << endl;
                }
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>
#define int int64_t

using namespace std;
typedef long long ll;
typedef pair <ll, ll> ii;
typedef vector<ll> vec;
int t, n;

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> t;
    while (t--)
    {
        cin >> n;
        int ans = 0;
        if (n > 9)
            ans += 9;
        else
        {
            cout << n << endl;
            continue;
        }
        if (n > 99)
            ans += 9;
        else
        {
            ans += n / 10;
            int k = n / 10 * 10 + n / 10;
            if (k > n)
                ans--;
            cout << ans << endl;
            continue;
        }
        if (n > 999)
            ans += 9;
        else
        {
            ans += n / 100;
            int k = 0;
            int a = 100;
            for (int i = 0 ; i < 3; i++)
            {
                k += n / 100 * a;
                a /= 10;
            }
            if (k > n)
                ans--;
            cout << ans << endl;
            continue;
        }
        if (n > 9999)
            ans+= 9;
        else
        {
            ans += n / 1000;
            int k = 0;
            int a = 1000;
            for (int i = 0 ; i < 4; i++)
            {
                k += n / 1000 * a;
                a /= 10;
            }
            if (k > n)
                ans--;
            cout << ans << endl;
            continue;
        }
        if (n > 99999)
            ans += 9;
        else
        {
            ans += n / 10000;
            int k = 0;
            int a = 10000;
            for (int i = 0 ; i < 5; i++)
            {
                k += n / 10000 * a;
                a /= 10;
            }
            if (k > n)
                ans--;
            cout << ans << endl;
            continue;
        }
        if (n > 999999)
            ans += 9;
        else
        {
            ans += n / 100000;
            int k = 0;
            int a = 100000;
            for (int i = 0 ; i < 6; i++)
            {
                k += n / 100000 * a;
                a /= 10;
            }
            if (k > n)
                ans--;
            cout << ans << endl;
            continue;
        }
        if (n > 9999999)
            ans += 9;
        else
        {
            ans += n / 1000000;
            int k = 0;
            int a = 1000000;
            for (int i = 0 ; i < 7; i++)
            {
                k += n / 1000000 * a;
                a /= 10;
            }
            if (k > n)
                ans--;
            cout << ans << endl;
            continue;
        }
        if (n > 99999999)
            ans += 9;
        else
        {
            ans += n / 10000000;
            int k = 0;
            int a = 10000000;
            for (int i = 0 ; i < 8; i++)
            {
                k += n / 10000000 * a;
                a /= 10;
            }
            if (k > n)
                ans--;
            cout << ans << endl;
            continue;
        }
        if (n > 999999999)
            ans += 9;
        else
        {
            ans += n / 100000000;
            int k = 0;
            int a = 100000000;
            for (int i = 0 ; i < 9; i++)
            {
                k += n / 100000000 * a;
                a /= 10;
            }
            if (k > n)
                ans--;
            cout << ans << endl;
            continue;
        }
        cout << ans;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int int64_t

using namespace std;
typedef long long ll;
typedef pair <ll, ll> ii;
typedef vector<ll> vec;
int t, n;

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> t;
    while (t--)
    {
        cin >> n;
        int ans = 0;
        if (n > 9)
            ans += 9;
        else
        {
            cout << n << " ";
            continue;
        }
        if (n > 99)
            ans += 9;
        else
        {
            ans += n / 10;
            int k = n / 10 * 10 + n / 10;
            if (k > n)
                ans--;
            cout << ans << " ";
            continue;
        }
        if (n > 999)
            ans += 9;
        else
        {
            ans += n / 100;
            int k = 0;
            int a = 100;
            for (int i = 0 ; i < 3; i++)
            {
                k += n / 100 * a;
                a /= 10;
            }
            if (k > n)
                ans--;
            cout << ans << " ";
            continue;
        }
        if (n > 9999)
            ans+= 9;
        else
        {
            ans += n / 1000;
            int k = 0;
            int a = 1000;
            for (int i = 0 ; i < 4; i++)
            {
                k += n / 1000 * a;
                a /= 10;
            }
            if (k > n)
                ans--;
            cout << ans << " ";
            continue;
        }
        if (n > 99999)
            ans += 9;
        else
        {
            ans += n / 10000;
            int k = 0;
            int a = 10000;
            for (int i = 0 ; i < 5; i++)
            {
                k += n / 10000 * a;
                a /= 10;
            }
            if (k > n)
                ans--;
            cout << ans << " ";
            continue;
        }
        if (n > 999999)
            ans += 9;
        else
        {
            ans += n / 100000;
            int k = 0;
            int a = 100000;
            for (int i = 0 ; i < 6; i++)
            {
                k += n / 100000 * a;
                a /= 10;
            }
            cout << k << " ";
            if (k > n)
                ans--;
            cout << ans << " ";
            continue;
        }
        if (n > 9999999)
            ans += 9;
        else
        {
            ans += n / 1000000;
            int k = 0;
            int a = 1000000;
            for (int i = 0 ; i < 7; i++)
            {
                k += n / 1000000 * a;
                a /= 10;
            }
            if (k > n)
                ans--;
            cout << ans << " ";
            continue;
        }
        if (n > 99999999)
            ans += 9;
        else
        {
            ans += n / 10000000;
            int k = 0;
            int a = 10000000;
            for (int i = 0 ; i < 8; i++)
            {
                k += n / 10000000 * a;
                a /= 10;
            }
            if (k > n)
                ans--;
            cout << ans << " ";
            continue;
        }
        if (n > 999999999)
            ans += 9;
        else
        {
            ans += n / 100000000;
            int k = 0;
            int a = 100000000;
            for (int i = 0 ; i < 9; i++)
            {
                k += n / 100000000 * a;
                a /= 10;
            }
            if (k > n)
                ans--;
            cout << ans << " ";
            continue;
        }
        cout << ans;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int int64_t

using namespace std;
typedef long long ll;
typedef pair <ll, ll> ii;
typedef vector<ll> vec;
int t;
string n;

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> t;
    while (t--)
    {
        cin >> n;
        int ans = 0;
        for (int i = 1; i < n.size(); i++)
        {
            ans += 9;
        }
        ans += n[0] - '0';
        int k = n [0] - '0';
        for (int i = 2; i <= n.size(); i++)
        {
            k *= 10;
            k += n [0] - '0';
        }
        int nn = n [0] - '0';
        for (int i = 1; i < n.size(); i++)
        {
            nn *= 10;
            nn += n [i] - '0';
        }
        if (k > nn)
            ans--;
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef vector<int> vi;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        int m = n;
        int cnt = 0;
        int m_dig = 9;

        while (m >= 10)
        {
            m_dig = min(m % 10, m_dig);
            cnt += 9;
            m /= 10;
        }
        m_dig = min(m % 10, m_dig);
        cnt += m_dig;
        cout << cnt << '\n';
    }

    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef vector<int> vi;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        int m = n;
        int cnt = 0;
        int m_dig = 9;

        while (m >= 10)
        {
            m_dig = min(m % 10, m_dig);
            cnt += 9;
            m /= 10;
        }
        m_dig = min(m, m_dig);
        cnt += m - 1;
        cnt += m_dig >= m;
        cout << cnt << '\n';
    }

    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef vector<int> vi;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        int m = n;
        int cnt = 0;
        int m_dig = 9;
        int digs = 0;

        while (m >= 10)
        {
            digs++;
            m_dig = min(m % 10, m_dig);
            cnt += 9;
            m /= 10;
        }

        m_dig = min(m, m_dig);
        cnt += m - 1;
        int m2 = m;
        while (digs--)
        {
            m *= 10;
            m += m2;
        }

        if (m <= n)
            cnt++;
        cout << cnt << '\n';
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios::sync_with_stdio(0);

    int n;
    cin>>n;
    for(int i=0;i<n; i++)
    {
        int a/* = i*/;
        cin>>a;
        if (a/10 == 0)
        {
            cout<<a<<'\n';
            continue;
        }
        int k = 0, da = a%10;
        bool f = true;
        while(a>=100)
        {
            if (da < a%10) f = false;
            a /= 10;
            k += 9;
        }
        if (a%10 != a/10 || da<a/10) f = false;
        if (a%10 > a/10) k+=a/10;
        else k += a/10 - 1;
        if (f) k++;
        if (a/10>0) k+=9;
        cout/*<<i<<' '*/<<k<<'\n';
    }

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios::sync_with_stdio(0);

    int n;
    cin>>n;
    for(int i=0;i<n; i++)
    {
        int a;
        cin>>a;
        int mina = 9, k = 0;
        while(a>=10)
        {
            mina = min(mina, a%10);
            a /= 10;
            k += 9;
        }
        if (a<=mina) k+=a;
        cout<<k<<'\n';
    }

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios::sync_with_stdio(0);

    int t;
    cin>>t;
    for(int j=0; j<t; j++)
    {
        int n;
        cin>>n;
        vector<int> a(n);
        for(int i = 0; i<n; i++)
        {
            cin>>a[i];
        }
        sort(a.begin(),a.end());

    }

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios::sync_with_stdio(0);

    int n;
    cin>>n;
    for(int i=0;i<n; i++)
    {
        int a/* = i*/;
        cin>>a;
        if (a/10 == 0)
        {
            cout<<a<<'\n';
            continue;
        }
        int k = 0, da = a%10;
        bool f = true;
        while(a>=100)
        {
            if (da != a%10) f = false;
            a /= 10;
            k += 9;
        }
        if (a%10 != a/10) f = false;
        if (a%10 > a/10) k+=a/10;
        else k += a/10 - 1;
        if (f) k++;
        if (a/10>0) k+=9;
        cout<</*i<<' '<<*/k<<'\n';
    }

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios::sync_with_stdio(0);

    int n;
    cin>>n;
    for(int i=0;i<n; i++)
    {
        int a/* = i*/;
        cin>>a;
        if (a/10 == 0)
        {
            cout<<a<<'\n';
            continue;
        }
        int k = 0;
        bool f = true;
        int cop_a = a;
        while (cop_a > 10)
        {
            cop_a/=10;
        }
        int da = cop_a;
        while(a>=100)
        {
            if (da > a%10) f = false;
            if (da < a%10) f = true;
            a /= 10;
            k += 9;
        }
        if (a%10 != a/10 || da<a/10) f = false;
        if (a%10 > a/10) k+=a/10;
        else k += a/10 - 1;
        if (f) k++;
        if (a/10>0) k+=9;
        cout/*<<i<<' '*/<<k<<'\n';
    }

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios::sync_with_stdio(0);

    int n;
    cin>>n;
    for(int i=0;i<n; i++)
    {
        int a;
        cin>>a;
        if (a==0)
        {
            cout<<0;
            continue;
        }
        int mina = 9, k = 0;
        while(a>=10)
        {
            mina = min(mina, a%10);
            a /= 10;
            k += 9;
        }
        if (a<=mina) k+=a;
        cout<<k<<'\n';
    }

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios::sync_with_stdio(0);

    int n;
    cin>>n;
    for(int i=0;i<n; i++)
    {
        int a/* = i*/;
        cin>>a;
        if (a/10 == 0)
        {
            cout<<a<<'\n';
            continue;
        }
        int k = 0, da = a%10;
        bool f = true;
        while(a>=100)
        {
            if (da != a%10) f = false;
            a /= 10;
            k += 9;
        }
        if (a%10 != a/10 || da<a/10) f = false;
        if (a%10 > a/10) k+=a/10;
        else k += a/10 - 1;
        if (f) k++;
        if (a/10>0) k+=9;
        cout/*<<i<<' '*/<<k<<'\n';
    }

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios::sync_with_stdio(0);

    int n;
    cin>>n;
    for(int i=0;i<n; i++)
    {
        int a;
        cin>>a;
        if (a==0)
        {
            cout<<0;
            continue;
        }
        int mina = 9, k = 0;
        while(a>=10)
        {
            mina = min(mina, a%10);
            a /= 10;
            k += 9;
        }
        if (a<=mina) k+=a;
        else k+=a-1;
        cout<<k<<'\n';
    }

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
   long long t,n,x=0,a,b,y;
   cin>>t;

   for (int i=0;i<t;i++){
    x=0;
    cin>>n;
    y=n;
    while (y>9){

        y/=10;
        x++;

    }
    x++;
   // cout<<x<<endl;
    y=n;

    if (x!=1){
            for (int i=0;i<x-2;i++){
        y/=10;

    }
    a=y/10;
    b=y%10;
    //cout<<a<<' '<<b<<endl;
            if (b>=a){cout<<(x-1)*9+a<<endl;}
            else {cout<<(x-1)*9+a-1<<endl;}
    }

    else {cout<<n<<endl;}

   }

}