#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define vll vector<long long>
#define all(S) S.begin(), S.end()
#define S(a) scanf("%lld", &a)
using namespace std;
int main(){
	string s;
	cin>>s;
	long ans = 0;
	for(long i=0;i<s.size();i++){
		ll noq, no, nc, mnoq, ano;
		noq = no = nc = mnoq = ano = 0;
		for(long j=i;j<s.size();j++){
			if(s[j] == '('){
				no++;
				ano++;
			} else if(s[j] == ')') {
				nc++;
			} else {
				noq++;
				if(ano-nc > mnoq)
					mnoq++;
			}
			if(nc > no + noq) break;
			if(nc > no){
				noq -= nc-no;
				no = nc;
				if(mnoq > noq) mnoq = noq;
			}
			if(nc == no){
				if(noq%2 == 0){
					ans++;
				}
			} else if (no > nc){
				if(no <= nc + mnoq){
					if((noq -no+nc)%2 == 0){
						ans++;
					}
				}
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define vll vector<long long>
#define all(S) S.begin(), S.end()
#define S(a) scanf("%lld", &a)
using namespace std;
int main(){
	string s;
	cin>>s;
	long ans = 0;
	for(long i=0;i<s.size();i++){
		ll noq, no, nc, mnoq, ano;
		noq = no = nc = mnoq = ano = 0;
		for(long j=i;j<s.size();j++){
			if(s[j] == '('){
				no++;
				ano++;
			} else if(s[j] == ')') {
				nc++;
				ano--;
			} else {
				noq++;
				ano--;
				if(ano > mnoq)
					mnoq++;
			}
			if(ano < 0) ano = 0;
			if(nc > no + noq) break;
			if(nc > no){
				noq -= nc-no;
				no = nc;
				if(mnoq > noq) mnoq = noq;
			}
			if(nc == no){
				if(noq%2 == 0){
					ans++;
				}
			} else if (no > nc){
				if(ano == 0 && (j-i+1)%2 ==0){
					ans++;
				}
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int ans, s1, s2;
bool flag;
char inp[5005];
int main() {
	scanf("%s", inp + 1);
	int N = strlen(inp + 1);
	for (int i = 1;i <= N;i++) {
		s1 = s2 = 0;
		for (int j = i;j <= N;j++) {
			flag = 0;
			if (inp[j] == '(') s1++, s2++;
			else if (inp[j] == ')') s1--, s2--;
			else {
				s1++, s2 = s2 > 0 ? s2 - 1 : s2 + 1;
			}
			if (s1 < 0) break;
			if ((j - i) & 1) {
				if (s2 <= 0 && inp[j] != '(') {
					ans++;
					//printf("%d %d %d %d\n", i, j, s1, s2);
				}
			}
		}
	}
	printf("%d", ans);

}


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int ans, s1, s2;
bool flag;
char inp[5005];
int main() {
	scanf("%s", inp + 1);
	int N = strlen(inp + 1);
	for (int i = 1;i <= N;i++) {
		s1 = s2 = 0;
		for (int j = i;j <= N;j++) {
			if (inp[j] == '(') s1++, s2++;
			else if (inp[j] == ')') s1--, s2--;
			else {
				s1++, s2 = s2 ? s2 - 1 : 1;
			}
			if (s1 < 0) break;
			if (((j - i) & 1) && s2 <= 0 && inp[j]!='(') ans++;
		}
	}
	printf("%d", ans);
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int ans, s1, s2;
bool flag;
char inp[5005];
int main() {
	scanf("%s", inp + 1);
	int N = strlen(inp + 1);
	for (int i = 1;i <= N;i++) {
		s1 = s2 = 0;
		for (int j = i;j <= N;j++) {
			flag = 0;
			if (inp[j] == '(') s1++, s2++;
			else if (inp[j] == ')') s1--, s2--;
			else s1++, s2--;
			s2 = max(0, s2);
			if (s1 < 0) break;
			if ((j - i) & 1) {
				if (s2 == 0) {
					ans++;
					//printf("%d %d %d %d\n", i, j, s1, s2);
				}
			}
		}
	}
	printf("%d", ans);

}


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int ans, s1, s2;
bool flag;
char inp[5005];
int main() {
	scanf("%s", inp + 1);
	int N = strlen(inp + 1);
	for (int i = 1;i <= N;i++) {
		s1 = s2 = 0;
		for (int j = i;j <= N;j++) {
			if (inp[j] == '(') s1++, s2++;
			else if (inp[j] == ')') s1--, s2--;
			else {
				s1++, s2 = s2 ? s2 - 1 : 1;
			}
			if (s1 < 0) break;
			if (((j - i) & 1) && (s2 < 0 && inp[j] != '(' || s2 == 0 && inp[j] == ')')) ans++;
		}
	}
	printf("%d", ans);

}


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
bool dp[5005][5005]; int ans;
char inp[5005];
int main() {
	scanf("%s", inp + 1);
	int N = strlen(inp + 1);
	for (int i = 1;i < N;i++)
		if (inp[i] != ')' && inp[i + 1] != '(') ans++,dp[i][i + 1] = 1;
	for (int g = 3;g <= N;g+=2)
		for (int i = 1;i <= N;i++) {
			int j = i + g;
			if (j > N) break;
			if (inp[i] != ')' && inp[j] != '(') dp[i][j] = dp[i + 1][j - 1];
			for (int k = i + 1;!dp[i][j] && k < j;k += 2)
				dp[i][j] = dp[i][k] & dp[k + 1][j];
			if (dp[i][j]) ans++;
		}
	printf("%d", ans);

}


#include <stdio.h>
#include <algorithm>
using namespace std;
bool dp[5005][5005]; int ans;
char inp[5005];
int main() {
	scanf("%s", inp + 1);
	int N = strlen(inp + 1);
	for (int i = 1;i < N;i++)
		if (inp[i] != ')' && inp[i + 1] != '(') ans++,dp[i][i + 1] = 1;
	for (int g = 3;g <= N;g+=2)
		for (int i = 1;i <= N;i++) {
			int j = i + g;
			if (j > N) break;
			if (inp[i] != ')' && inp[j] != '(') dp[i][j] = dp[i + 1][j - 1];
			for (int k = i + 1;!dp[i][j] && k < j;k += 2)
				dp[i][j] = dp[i][k] & dp[k + 1][j];
			if (dp[i][j]) ans++;
		}
	printf("%d", ans);

}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 18

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 1

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 3

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 17

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 14

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 11

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 10

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 4

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 12

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 9

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 8

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 7

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 16

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 2

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 15

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 13

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 5

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
typedef long long int ll;
using namespace std;
#define ver 6

int main()
{
	string s;
	cin >> s;
	int ans = 0;
	for(int i = 0; i < s.size(); i++)
	{
		int left = 0;
		int both = 0;
		for(int j = i; j < s.size(); j++)
		{
			if(s[j] == '(')
				left += 1;
			else if(s[j] == '?')
				both += 1;
			else
			{
				if(left > 0)
					left -= 1;
				else if(both > 0)
					both -= 1;
				else
					break;
			}
			while(left < both)
			{
				left += 1;
				both -= 1;
			}
			if((j - i) % 2 == 0)
				continue;
			if(left > both)
				continue;
			ans += 1;
			//cout << s.substr(i, j+1) << endl;
		}
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define pii pair<int,int>
#define FOR(i,x,y) for(int i=x; i<=y; i++)

const int mod=int(1e9+7),MAXN=int(2e5+1);

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

	string s;

    cin >> s;

    int n=s.length()-1,rez=0;

    FOR(i,0,n-1)
    {
        int t=0,zp=0;
        FOR(j,i,n)
        {
            if (s[j]=='?')
                zp++;
            else
                t+=s[j]=='('?1:-1;
            if (t<zp && zp>0)
            {
                t++;
                zp--;
            }
            else
                if (t<0)
                    break;

            if (zp>=t && (zp-t)%2==0)
                rez++;
        }
    }

    cout << rez;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define pii pair<int,int>
#define FOR(i,x,y) for(int i=x; i<=y; i++)

const int mod=int(1e9+7),MAXN=int(2e5+1);

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

	string s;

    cin >> s;

    int n=s.length()-1,rez=0;

    FOR(i,0,n-1)
    {
        int t=0,zp=0;
        FOR(j,i,n)
        {
            if (s[j]=='?')
                zp++;
            else
                t+=s[j]=='('?1:-1;
            if (t<zp && zp>0)
            {
                t++;
                zp--;
            }
            else
                if (t<0)
                    break;

            if ((t==zp || (zp>t && (zp-t)%2==0)) && (s[j]!='('))
                rez++;
        }
    }

    cout << rez;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define pii pair<int,int>
#define FOR(i,x,y) for(int i=x; i<=y; i++)

const int mod=int(1e9+7),MAXN=int(2e5+1);

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

	string s;

    cin >> s;

    int n=s.length()-1,rez=0;

    FOR(i,0,n-1)
    {
        int t=0,zp=0;
        FOR(j,i,n)
        {
            if (s[j]=='?')
                zp++;
            else
                t+=s[j]=='('?1:-1;
            if (t<=0 && zp>0)
            {
                t++;
                zp--;
            }
            else
                if (t<0)
                    break;

            if (t==zp || (zp>t && (zp-t)%2==0))
                rez++;
        }
    }

    cout << rez;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define pii pair<int,int>
#define FOR(i,x,y) for(int i=x; i<=y; i++)

const int mod=int(1e9+7),MAXN=int(2e5+1);

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

	string s;

    cin >> s;

    int n=s.length()-1,rez=0;

    FOR(i,0,n-1)
    {
        int t=0,zp=0;
        FOR(j,i,n)
        {
            if (s[j]=='?')
                zp++;
            else
                t+=s[j]=='('?1:-1;
            if (t<=0 && zp>0)
            {
                t++;
                zp--;
            }
            else
                if (t<0)
                    break;

            if ((t==zp || (zp>t && (zp-t)%2==0)) && (s[j]!='('))
                rez++;
        }
    }

    cout << rez;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define pii pair<int,int>
#define FOR(i,x,y) for(int i=x; i<=y; i++)

const int mod=int(1e9+7),MAXN=int(2e5+1);

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

	string s;

    cin >> s;

    int n=s.length()-1,rez=0;

    FOR(i,0,n-1)
    {
        int t=0,zp=0;
        FOR(j,i,n)
        {
            if (s[j]=='?')
                zp++;
            else
                t+=s[j]=='('?1:-1;
            if (t<zp && zp>0)
            {
                t++;
                zp--;
            }
            else
                if (t<0)
                    break;

            if ((t==zp || (zp>t && (zp-t)%2==0)))
                rez++;
        }
    }

    cout << rez;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define pii pair<int,int>
#define FOR(i,x,y) for(int i=x; i<=y; i++)

const int mod=int(1e9+7),MAXN=int(2e5+1);

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

	string s;

    cin >> s;

    int n=s.length()-1,rez=0;

    FOR(i,0,n-1)
    {
        int t=0,zp=0;
        FOR(j,i,n)
        {
            if (s[j]=='?')
                zp++;
            else
                t+=s[j]=='('?1:-1;
            if ((t<=0 && zp>0) || (t<zp))
            {
                t++;
                zp--;
            }
            else
                if (t<0)
                    break;

            if ((t==zp || (zp>t && (zp-t)%2==0)) && (s[j]!='('))
                rez++;
        }
    }

    cout << rez;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mst(a,b) memset((a),(b),sizeof(a))
#define pi acos(-1)
#define pii pair<int,int>
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int MAXN = 5e3 + 10;
const int MAXM = 2e6 + 10;
const ll mod = 1e9 + 7;

char s[MAXN];

int main()
{
#ifdef local
    freopen("data.txt","r",stdin);
//    freopen("data.txt","w",stdout);
#endif
    scanf("%s",s+1);
    int len = strlen(s+1);
    int ans = 0;
    for(int i=1; i<=len; i++) {
        if(s[i] == ')') continue;
        int num[3];
        mst(num,0);
        for(int j=i; j<=len; j++) {
            if(s[j] == '(') num[0]++;
            else if(s[j] == ')') {
                if(num[0] > 0) num[0]--;
                else if(num[2] > 0) num[2]--;
                else break;
            }
            else if(s[j] == '?') {
                if(num[0] + num[2] == num[1]) num[0]++;
                else if(num[0] + num[2] > num[1]) num[2]++;
                else break;
            }
            if(num[1] > num[0] + num[2]) break;
            if((j - i + 1) % 2 || s[j] == '(') continue;
            if(num[0] == num[1]) {
                if(num[2] % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
            else if(num[0] > num[1]) {
                int sum = abs(num[0] - num[1]);
                if(sum > num[2]) continue;
                if((num[2] - sum) % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
            else if(num[0] < num[1]) {
                int sum = abs(num[0] - num[1]);
                if(sum > num[2]) continue;
                if((num[2] - sum) % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mst(a,b) memset((a),(b),sizeof(a))
#define pi acos(-1)
#define pii pair<int,int>
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int MAXN = 5e3 + 10;
const int MAXM = 2e6 + 10;
const ll mod = 1e9 + 7;

char s[MAXN];

int main()
{
#ifdef local
    freopen("data.txt","r",stdin);
//    freopen("data.txt","w",stdout);
#endif
    scanf("%s",s+1);
    int len = strlen(s+1);
    int ans = 0;
    for(int i=1; i<=len; i++) {
        if(s[i] == ')') continue;
        int num[3];
        mst(num,0);
        for(int j=i; j<=len; j++) {
            if(s[j] == '(') num[0]++;
            else if(s[j] == ')') num[1]++;
            else if(s[j] == '?') {
                if(num[0] == 0) num[0]++;
                else num[2]++;
            }
            if(num[1] > num[0] + num[2]) break;
            if((j - i + 1) % 2) continue;
            if(num[0] == num[1]) {
                if(num[2] % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
            else if(num[0] > num[1]) {
                int sum = abs(num[0] - num[1]);
                if(sum > num[2]) continue;
                if((num[2] - sum) % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
            else if(num[0] < num[1]) {
                int sum = abs(num[0] - num[1]);
                if(sum > num[2]) continue;
                if((num[2] - sum) % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mst(a,b) memset((a),(b),sizeof(a))
#define pi acos(-1)
#define pii pair<int,int>
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int MAXN = 5e3 + 10;
const int MAXM = 2e6 + 10;
const ll mod = 1e9 + 7;

char s[MAXN];

int main()
{
#ifdef local
    freopen("data.txt","r",stdin);
//    freopen("data.txt","w",stdout);
#endif
    scanf("%s",s+1);
    int len = strlen(s+1);
    int ans = 0;
    for(int i=1; i<=len; i++) {
        if(s[i] == ')') continue;
        int num[3];
        mst(num,0);
        for(int j=i; j<=len; j++) {
            if(s[j] == '(') num[0]++;
            else if(s[j] == ')') num[1]++;
            else if(s[j] == '?') {
                if(num[0] == 0) num[0]++;
                else num[2]++;
            }
            if((j - i + 1) % 2) continue;
            if(num[0] == num[1]) {
                if(num[2] % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
            else if(num[0] > num[1]) {
                int sum = abs(num[0] - num[1]);
                if(sum > num[2]) continue;
                if((num[2] - sum) % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
            else if(num[0] < num[1]) {
                int sum = abs(num[0] - num[1]);
                if(sum > num[2]) continue;
                if((num[2] - sum) % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mst(a,b) memset((a),(b),sizeof(a))
#define pi acos(-1)
#define pii pair<int,int>
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int MAXN = 5e3 + 10;
const int MAXM = 2e6 + 10;
const ll mod = 1e9 + 7;

char s[MAXN];

int main()
{
#ifdef local
    freopen("data.txt","r",stdin);
//    freopen("data.txt","w",stdout);
#endif
    scanf("%s",s+1);
    int len = strlen(s+1);
    int ans = 0;
    for(int i=1; i<=len; i++) {
        if(s[i] == ')') continue;
        int num[3];
        mst(num,0);
        for(int j=i; j<=len; j++) {
            if(s[j] == '(') num[0]++;
            else if(s[j] == ')') num[1]++;
            else if(s[j] == '?') {
                if(num[0] == 0) num[0]++;
                else num[2]++;
            }
            if(num[1] > num[0] + num[2]) break;
            if((j - i + 1) % 2 || s[j] == '(') continue;
            if(num[0] == num[1]) {
                if(num[2] % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
            else if(num[0] > num[1]) {
                int sum = abs(num[0] - num[1]);
                if(sum > num[2]) continue;
                if((num[2] - sum) % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
            else if(num[0] < num[1]) {
                int sum = abs(num[0] - num[1]);
                if(sum > num[2]) continue;
                if((num[2] - sum) % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mst(a,b) memset((a),(b),sizeof(a))
#define pi acos(-1)
#define pii pair<int,int>
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int MAXN = 5e3 + 10;
const int MAXM = 2e6 + 10;
const ll mod = 1e9 + 7;

char s[MAXN];

int main()
{
#ifdef local
    freopen("data.txt","r",stdin);
//    freopen("data.txt","w",stdout);
#endif
    scanf("%s",s+1);
    int len = strlen(s+1);
    int ans = 0;
    for(int i=1; i<=len; i++) {
        if(s[i] == ')') continue;
        int num[3];
        mst(num,0);
        for(int j=i; j<=len; j++) {
            if(s[j] == '(') num[0]++;
            else if(s[j] == ')') num[1]++;
            else if(s[j] == '?') {
                if(num[0] == 0 && num[2] == 0) num[0]++;
                else num[2]++;
            }
            if(num[1] > num[0] + num[2]) break;
            if((j - i + 1) % 2 || s[j] == '(') continue;
            if(num[0] == num[1]) {
                if(num[2] % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
            else if(num[0] > num[1]) {
                int sum = abs(num[0] - num[1]);
                if(sum > num[2]) continue;
                if((num[2] - sum) % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
            else if(num[0] < num[1]) {
                int sum = abs(num[0] - num[1]);
                if(sum > num[2]) continue;
                if((num[2] - sum) % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mst(a,b) memset((a),(b),sizeof(a))
#define pi acos(-1)
#define pii pair<int,int>
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int MAXN = 5e3 + 10;
const int MAXM = 2e6 + 10;
const ll mod = 1e9 + 7;

char s[MAXN];

int main()
{
#ifdef local
    freopen("data.txt","r",stdin);
//    freopen("data.txt","w",stdout);
#endif
    scanf("%s",s+1);
    int len = strlen(s+1);
    int ans = 0;
    for(int i=1; i<=len; i++) {
        if(s[i] == ')') continue;
        int num[3];
        mst(num,0);
        for(int j=i; j<=len; j++) {
            if(s[j] == '(') num[0]++;
            else if(s[j] == ')') {
                if(num[0] > 0) num[0]--;
                else if(num[2] > 0) num[2]--;
                else break;
            }
            else if(s[j] == '?') {
                if(num[0] + num[2] == num[1]) num[0] = 1,num[1] = 0,num[2] = 0;
                else if(num[0] + num[2] > num[1]) num[2]++;
                else break;
            }
            if(num[1] > num[0] + num[2]) break;
            if((j - i + 1) % 2 || s[j] == '(') continue;
            if(num[0] == num[1]) {
                if(num[2] % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
            else if(num[0] > num[1]) {
                int sum = abs(num[0] - num[1]);
                if(sum > num[2]) continue;
                if((num[2] - sum) % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
            else if(num[0] < num[1]) {
                int sum = abs(num[0] - num[1]);
                if(sum > num[2]) continue;
                if((num[2] - sum) % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mst(a,b) memset((a),(b),sizeof(a))
#define pi acos(-1)
#define pii pair<int,int>
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int MAXN = 5e3 + 10;
const int MAXM = 2e6 + 10;
const ll mod = 1e9 + 7;

char s[MAXN];

int main()
{
#ifdef local
    freopen("data.txt","r",stdin);
//    freopen("data.txt","w",stdout);
#endif
    scanf("%s",s+1);
    int len = strlen(s+1);
    int ans = 0;
    for(int i=1; i<=len; i++) {
        int l = 0,r = 0;
        for(int j=i; j<=len; j++) {
            if(s[j] == '(') l++,r++;
            else if(s[j] == ')') l--,r--;
            else l--,r++;
            if(r < 0) break;
            if(l < 0) l = 0;
            if(l == 0 && (j - i + 1) % 2 == 0) ans++;
//            printf("%d %d %d %d\n",i,j,l,r);
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mst(a,b) memset((a),(b),sizeof(a))
#define pi acos(-1)
#define pii pair<int,int>
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int MAXN = 5e3 + 10;
const int MAXM = 2e6 + 10;
const ll mod = 1e9 + 7;

char s[MAXN];

int main()
{
#ifdef local
    freopen("data.txt","r",stdin);
//    freopen("data.txt","w",stdout);
#endif
    scanf("%s",s+1);
    int len = strlen(s+1);
    int ans = 0;
    for(int i=1; i<=len; i++) {
        if(s[i] == ')') continue;
        int num[3];
        mst(num,0);
        for(int j=i; j<=len; j++) {
            if(s[j] == '(') num[0]++;
            else if(s[j] == ')') {
                if(num[0] > 0) num[0]--;
                else if(num[2] > 0) num[2]--;
                else break;
            }
            else if(s[j] == '?') {
                if(num[0] + num[2] == num[1]) num[0]++,num[1] = 0,num[2] = 0;
                else if(num[0] + num[2] > num[1]) num[2]++;
                else break;
            }
            if(num[1] > num[0] + num[2]) break;
            if((j - i + 1) % 2 || s[j] == '(') continue;
            if(num[0] == num[1]) {
                if(num[2] % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
            else if(num[0] > num[1]) {
                int sum = abs(num[0] - num[1]);
                if(sum > num[2]) continue;
                if((num[2] - sum) % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
            else if(num[0] < num[1]) {
                int sum = abs(num[0] - num[1]);
                if(sum > num[2]) continue;
                if((num[2] - sum) % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mst(a,b) memset((a),(b),sizeof(a))
#define pi acos(-1)
#define pii pair<int,int>
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int MAXN = 5e3 + 10;
const int MAXM = 2e6 + 10;
const ll mod = 1e9 + 7;

char s[MAXN];

int main()
{
#ifdef local
    freopen("data.txt","r",stdin);
//    freopen("data.txt","w",stdout);
#endif
    scanf("%s",s+1);
    int len = strlen(s+1);
    int ans = 0;
    for(int i=1; i<=len; i++) {
        if(s[i] == ')') continue;
        int num[3];
        mst(num,0);
        for(int j=i; j<=len; j++) {
            if(s[j] == '(') num[0]++;
            else if(s[j] == ')') num[1]++;
            else if(s[j] == '?') {
                if(num[0] == 0) num[0]++;
                else num[2]++;
            }
            if(num[1] > num[0]) break;
            if((j - i + 1) % 2) continue;
            if(num[0] == num[1]) {
                if(num[2] % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
            else if(num[0] > num[1]) {
                int sum = abs(num[0] - num[1]);
                if(sum > num[2]) continue;
                if((num[2] - sum) % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
            else if(num[0] < num[1]) {
                int sum = abs(num[0] - num[1]);
                if(sum > num[2]) continue;
                if((num[2] - sum) % 2 == 0) {
                    ans++;
//                    printf("%d %d\n",i,j);
                }
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi 3.1415926536
ll min(ll a,ll b)
{ if(a>b) return b;
return a;
}
ll max(ll a,ll b)
{ if(a>b) return a;
return b;
}
int isprime(ll n)
{ if(n==2) return 1;
    if(n<2||n%2==0) return 0;
       for(int i=3;i*i<=n;i+=2) if(n%i==0) return 0;
    return 1;
}
ll power(ll a,ll b,ll mod)
{ ll res=1;
    while(b)
    {  if(b&1) {res=res*a;res%=mod;}
        a=a*a;a%=mod;
        b=b>>1;
    }
    return res%mod;

}
void bfs(ll level[],vector <int> adj[],ll n,ll visited[])
{ queue <int> q;
    q.push(1);
    visited[1]=1;
    level[1]=1;
    while(!q.empty())
    { int p=q.front();
        q.pop();
        for(int i=0;i<adj[p].size();i++)
        {
           if(visited[adj[p][i]]==0)
           {   level[adj[p][i]]=level[p]+1;
               q.push(adj[p][i]);
               visited[adj[p][i]]=1;
           }

        }

    }
}
void dfs(ll x,vector <int> adj[],ll visited[])
{
    stack <int> s;
    s.push(x);
    visited[x]=1;
          int p=s.top();
            for(int i=0;i<adj[p].size();i++)
        {     if(visited[adj[p][i]]==0)
                 dfs(adj[p][i],adj,visited);

        }
    s.pop();
}
ll gcd(ll a,ll b)
{ if(a==0) return b;
    return gcd(b%a,a);
}
int main()
{
   string a;
   cin>>a;
   ll i=0,j=0,aq,var,q,count=0,bq;
   while(i<a.length())
   {  j=i;
      var=0;q=0,aq=0,bq=0;
     while(j<a.length())
     {
         if(var==0) aq=bq=0;
        if(var>0&&a[j]=='?') aq++;
        if(var<0&&a[j]=='?') bq++;
        if(a[j]=='(') {var++;j++;continue;}
        if(a[j]==')') var--;
        if(a[j]=='?') q++;
        //if(q%2==0&&q>0&&var%2==0&&abs(var)<=q&&a[j]=='('&&var<0) count--;
        //if(q%2==1&&var%2==1&&abs(var)<=q&&a[j]=='('&&var<0) count--;
        ll h=(q-aq)%2;ll m=bq%2;
        if(var==0&&q%2==0&&aq>=var+h&&q-bq>=m-var) {count++;j++;continue;}
        if(q%2==0&&q>0&&((int)abs(var))%2==0&&abs(var)<=q&&aq>=var+h&&q-bq>=m-var) {count++;}
        if(q%2==1&&((int)abs(var))%2==1&&abs(var)<=q&&aq>=var+h&&q-bq>=m-var) {count++;}
        j++;

     }
       i++;

   }
   cout<<count<<endl;
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
	template <typename Arg1>
	void __f(const char* name, Arg1&& arg1)
	{
		cerr << name << " = " << arg1 << endl;
	}
	template <typename Arg1, typename... Args>
	void __f(const char* names, Arg1&& arg1, Args&&... args)
	{
		const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " = " << arg1 << " || ";
		__f(comma+1, args...);
	}
#else
#define debug(...)
#endif

template <class Ch, class Tr, class Container>
basic_ostream <Ch, Tr> & operator << (basic_ostream <Ch, Tr> & os, Container const& x) {
    os << "{ ";
    for(auto& y : x) os << y << " ; ";
    return os << "}";
}

template <class X, class Y>
ostream & operator << (ostream & os, pair <X, Y> const& p) {
	return os << "[ " << p.first << ", " << p.second << "]" ;
}

#define FAST_IO std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll int
#define pb push_back
#define mp make_pair
#define ld long double
#define sz(a) (ll)(a).size()
#define endl "\n"

// priority_queue<ll, vector<ll>, less<ll>>pq;     // Max Heap
// priority_queue<ll, vector<ll>, greater<ll>>pq;  // Min Heap

typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//K-th smallest
//cout << k << "kth (1 based indexing) smallest: " << *A.find_by_order(k - 1) << endl;
//NO OF ELEMENTS < X
//cout << "No of elements less than " << X << " are " << A.order_of_key(X) << endl;

const int MAXN = 5005;

string a;
bool done1[MAXN][MAXN], done2[MAXN][MAXN];
ll n, dp1[MAXN][MAXN], dp2[MAXN][MAXN], ans;

ll rec1(ll start, ll end)
{
	if(start > end) return 0;
	if((end - start + 1) % 2 == 1) return -1;
	if(done1[start][end]) return dp1[start][end];
	done1[start][end] = 1;
	ll f1 = 0, f2 = 0;
	if(a[start] == '?' || a[start] == '(') f1 += 1;
	else f1 -= 1;
	if(a[start + 1] == '?' || a[start + 1] == '(') f2 += 1;
	else f2 -= 1;
	f2 += f1;
	return dp1[start][end] = min({f2 + rec1(start + 2, end), f1, f2});
}

ll rec2(ll start, ll end)
{
	if(start > end) return 0;
	if((end - start + 1) % 2 == 1) return -1;
	if(done2[start][end]) return dp2[start][end];
	done2[start][end] = 1;
	ll ans = 0, f1 = 0, f2 = 0;
	if(a[end] == '?' || a[end] == ')') f1 += 1;
	else f1 -= 1;
	if(a[end - 1] == '?' || a[end - 1] == ')') f2 += 1;
	else f2 -= 1;
	f2 += f1;
	return dp2[start][end] = min({f2 + rec2(start, end - 2), f1, f2});
}

int main()
{
	FAST_IO;

    cin >> a;
    n = a.length();
    for(int i=0;i<n;i++)
    	for(int j=i;j<n;j++)
    	{
    		dp1[i][j] = rec1(i, j);
    		dp2[i][j] = rec2(i, j);
    		if(dp1[i][j] >= 0 && dp2[i][j] >= 0)
    			ans += 1;
    	}
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi 3.1415926536
ll min(ll a,ll b)
{ if(a>b) return b;
return a;
}
ll max(ll a,ll b)
{ if(a>b) return a;
return b;
}
int isprime(ll n)
{ if(n==2) return 1;
    if(n<2||n%2==0) return 0;
       for(int i=3;i*i<=n;i+=2) if(n%i==0) return 0;
    return 1;
}
ll power(ll a,ll b,ll mod)
{ ll res=1;
    while(b)
    {  if(b&1) {res=res*a;res%=mod;}
        a=a*a;a%=mod;
        b=b>>1;
    }
    return res%mod;

}
void bfs(ll level[],vector <int> adj[],ll n,ll visited[])
{ queue <int> q;
    q.push(1);
    visited[1]=1;
    level[1]=1;
    while(!q.empty())
    { int p=q.front();
        q.pop();
        for(int i=0;i<adj[p].size();i++)
        {
           if(visited[adj[p][i]]==0)
           {   level[adj[p][i]]=level[p]+1;
               q.push(adj[p][i]);
               visited[adj[p][i]]=1;
           }

        }

    }
}
void dfs(ll x,vector <int> adj[],ll visited[])
{
    stack <int> s;
    s.push(x);
    visited[x]=1;
          int p=s.top();
            for(int i=0;i<adj[p].size();i++)
        {     if(visited[adj[p][i]]==0)
                 dfs(adj[p][i],adj,visited);

        }
    s.pop();
}
ll gcd(ll a,ll b)
{ if(a==0) return b;
    return gcd(b%a,a);
}
int main()
{
   string a;
   cin>>a;
   ll i=0,j=0,aq,var,q,count=0,bq;
   while(i<a.length())
   {  j=i;
      var=0;q=0,aq=0,bq=0;
     while(j<a.length())
     {
         if(var==0) aq=bq=0;
        if(var>0&&a[j]=='?') aq++;
        if(var<0&&a[j]=='?') bq++;
        if(a[j]=='(') var++;
        if(a[j]==')') var--;
        if(a[j]=='?') q++;
        //if(q%2==0&&q>0&&var%2==0&&abs(var)<=q&&a[j]=='('&&var<0) count--;
        //if(q%2==1&&var%2==1&&abs(var)<=q&&a[j]=='('&&var<0) count--;
        ll h=(q-aq)%2;ll m=bq%2;
        if(var==0&&q%2==0&&aq>=var+h&&q-bq>=m-var) {count++;j++;continue;}
        if(q%2==0&&q>0&&((int)abs(var))%2==0&&abs(var)<=q&&aq>=var+h&&q-bq>=m-var) {count++;}
        if(q%2==1&&((int)abs(var))%2==1&&abs(var)<=q&&aq>=var+h&&q-bq>=m-var) {count++;}
        j++;

     }
       i++;

   }
   cout<<count<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi 3.1415926536
ll min(ll a,ll b)
{ if(a>b) return b;
return a;
}
ll max(ll a,ll b)
{ if(a>b) return a;
return b;
}
int isprime(ll n)
{ if(n==2) return 1;
    if(n<2||n%2==0) return 0;
       for(int i=3;i*i<=n;i+=2) if(n%i==0) return 0;
    return 1;
}
ll power(ll a,ll b,ll mod)
{ ll res=1;
    while(b)
    {  if(b&1) {res=res*a;res%=mod;}
        a=a*a;a%=mod;
        b=b>>1;
    }
    return res%mod;

}
void bfs(ll level[],vector <int> adj[],ll n,ll visited[])
{ queue <int> q;
    q.push(1);
    visited[1]=1;
    level[1]=1;
    while(!q.empty())
    { int p=q.front();
        q.pop();
        for(int i=0;i<adj[p].size();i++)
        {
           if(visited[adj[p][i]]==0)
           {   level[adj[p][i]]=level[p]+1;
               q.push(adj[p][i]);
               visited[adj[p][i]]=1;
           }

        }

    }
}
void dfs(ll x,vector <int> adj[],ll visited[])
{
    stack <int> s;
    s.push(x);
    visited[x]=1;
          int p=s.top();
            for(int i=0;i<adj[p].size();i++)
        {     if(visited[adj[p][i]]==0)
                 dfs(adj[p][i],adj,visited);

        }
    s.pop();
}
ll gcd(ll a,ll b)
{ if(a==0) return b;
    return gcd(b%a,a);
}
int main()
{
   string a;
   cin>>a;
   ll i=0,j=0,aq,var,q,count=0;
   while(i<a.length())
   {  j=i;
      var=0;q=0,aq=0;
     while(j<a.length())
     {   if((j-i)%2==1) {j++;continue;}
         if(var<0) break;
         if(var==0) aq=0;
        if(var>0&&a[j]=='?') aq++;

        if(a[j]=='(') {var++;j++;continue;}
        if(a[j]==')') var--;
        if(a[j]=='?') q++;
        //if(q%2==0&&q>0&&var%2==0&&abs(var)<=q&&a[j]=='('&&var<0) count--;
        //if(q%2==1&&var%2==1&&abs(var)<=q&&a[j]=='('&&var<0) count--;
        ll h=(q-aq)%2;
        if(var==0&&q%2==0&&aq>=var+h) {count++;j++;continue;}
        if(q%2==0&&q>0&&((int)abs(var))%2==0&&abs(var)<=q&&aq>=var+h) {count++;}
        if(q%2==1&&((int)abs(var))%2==1&&abs(var)<=q&&aq>=var+h) {count++;}
        j++;

     }
       i++;

   }
   //cout<<"i="<<i<<" j="<<j<<endl;
   cout<<count<<endl;
    return 0;
}


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

const int MAXN = 5001;

string a;
bool fr[MAXN][MAXN], ba[MAXN][MAXN];

int main()
{
    cin >> a;
    int n = a.length(), ans = 0;
    for(int i=0;i<n;i++)
    {
        ll cnt = 0;
        bool ok = true;
        for(int j=i;j<n;j++)
        {
            if(a[j] == ')') cnt -= 1;
            else cnt += 1;
            if(cnt < 0) ok = false;
            fr[i][j] = ok;
        }
    }
    for(int i=n-1;i>=0;i--)
    {
        ll cnt = 0;
        bool ok = true;
        for(int j=i;j>=0;j--)
        {
            if(a[j] == '(') cnt -= 1;
            else cnt += 1;
            if(cnt < 0) ok = false;
            ba[j][i] = ok;
        }
    }
    for(int i=0;i<n;i++)
        for(int j=i+1;j<n;j++)
            if((j - i + 1) % 2 == 0 && fr[i][j] && ba[i][j])
                ans += 1;
    cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi 3.1415926536
ll min(ll a,ll b)
{ if(a>b) return b;
return a;
}
ll max(ll a,ll b)
{ if(a>b) return a;
return b;
}
int isprime(ll n)
{ if(n==2) return 1;
    if(n<2||n%2==0) return 0;
       for(int i=3;i*i<=n;i+=2) if(n%i==0) return 0;
    return 1;
}
ll power(ll a,ll b,ll mod)
{ ll res=1;
    while(b)
    {  if(b&1) {res=res*a;res%=mod;}
        a=a*a;a%=mod;
        b=b>>1;
    }
    return res%mod;

}
void bfs(ll level[],vector <int> adj[],ll n,ll visited[])
{ queue <int> q;
    q.push(1);
    visited[1]=1;
    level[1]=1;
    while(!q.empty())
    { int p=q.front();
        q.pop();
        for(int i=0;i<adj[p].size();i++)
        {
           if(visited[adj[p][i]]==0)
           {   level[adj[p][i]]=level[p]+1;
               q.push(adj[p][i]);
               visited[adj[p][i]]=1;
           }

        }

    }
}
void dfs(ll x,vector <int> adj[],ll visited[])
{
    stack <int> s;
    s.push(x);
    visited[x]=1;
          int p=s.top();
            for(int i=0;i<adj[p].size();i++)
        {     if(visited[adj[p][i]]==0)
                 dfs(adj[p][i],adj,visited);

        }
    s.pop();
}
ll gcd(ll a,ll b)
{ if(a==0) return b;
    return gcd(b%a,a);
}
int main()
{
   string a;
   cin>>a;
   ll i=0,j=0,aq,var,q,count=0,bq;
   while(i<a.length())
   {  j=i;
      var=0;q=0,aq=0,bq=0;
     while(j<a.length())
     {   if(var<0) break;
         if(var==0) aq=bq=0;
        if(var>0&&a[j]=='?') aq++;
        if(var<0&&a[j]=='?') bq++;
        if(a[j]=='(') {var++;j++;continue;}
        if(a[j]==')') var--;
        if(a[j]=='?') q++;
        //if(q%2==0&&q>0&&var%2==0&&abs(var)<=q&&a[j]=='('&&var<0) count--;
        //if(q%2==1&&var%2==1&&abs(var)<=q&&a[j]=='('&&var<0) count--;
        ll h=(q-aq)%2;ll m=bq%2;
        if(var==0&&q%2==0&&aq>=var+h&&q-bq>=m-var) {count++;j++;continue;}
        if(q%2==0&&q>0&&((int)abs(var))%2==0&&abs(var)<=q&&aq>=var+h&&q-bq>=m-var) {count++;}
        if(q%2==1&&((int)abs(var))%2==1&&abs(var)<=q&&aq>=var+h&&q-bq>=m-var) {count++;}
        j++;

     }
       i++;

   }
   //cout<<"i="<<i<<" j="<<j<<endl;
   cout<<count<<endl;
    return 0;
}


//In the name of God
#include<bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define mod (ll)1000000007
#define ll long long
#define acm ios_base::sync_with_stdio(0),ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
int n;
ll dp[5005][5005][2]= {};
string s;
ll solve(int idx,int sum,bool is)
{
    if (sum<0) return 0;
    if (!sum&&is) return 1;
    if (idx>=n) return 0;
    ll &ret=dp[idx][sum][is];
    if (ret!=-1) return ret;
    ret=0;
    if (!is) ret+=solve(idx+1,sum,is);
    if (s[idx]==')')
        ret+=solve(idx+1,sum-1,true);
    else if (s[idx]=='(')
        ret+=solve(idx+1,sum+1,true);
    else
    {
        ret+=solve(idx+1,sum+1,true);
        ret+=solve(idx+1,sum-1,true);
    }
    return ret;
}
int main()
{
    acm;
    cin>>s;
    n=s.size();
    memset(dp,-1,sizeof(dp));
    cout<<solve(0,0,0)<<"\n";
    return 0;
}


//In the name of God
#include<bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define mod (ll)1000000007
#define ll long long
#define acm ios_base::sync_with_stdio(0),ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
int n;
ll cnt=0;
string s;
int main()
{
    acm;
    cin>>s;
    n=s.size();
    for (int i=0; i<n; ++i)
    {
        if (s[i]=='(') continue;
        int r=1,q=0;
        for (int j=i-1; j>=0; --j)
        {
            if ((abs(j-i)+1)&1)
            {
                if (s[j]=='?')
                {
                    if (r>0) --r;
                    q++;
                }
                else if (s[j]=='(')
                {
                    if (r>0) --r;
                    else if (q>0) --q;
                    else break;
                }
                else if (s[j]==')') ++r;
                continue;
            }
            else if (s[j]==')')
            {
                ++r;
                continue;
            }
            if (s[j]=='?')
            {
                if (r>0) r--;
                q++;
            }
            else if (s[j]=='(')
            {
                if (r>0) --r;
                else if (q>0) --q;
            }
            if (!r) ++cnt;
        }
    }
    cout<<cnt<<"\n";
    return 0;
}


//In the name of God
#include<bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define mod (ll)1000000007
#define ll long long
#define acm ios_base::sync_with_stdio(0),ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
int n;
ll cnt=0;
string s;
int main()
{
    acm;
    cin>>s;
    n=s.size();
    for (int i=0; i<n; ++i)
    {
        if (s[i]=='(') continue;
        int r=1,q=0,l=0;
        for (int j=i-1; j>=0; --j)
        {
            if ((abs(j-i)+1)&1)
            {
                if (s[j]=='?')
                {
                    if (r>0) --r;
                    q++;
                }
                else if (s[j]=='(')
                {
                    if (r>0) --r;
                    else if (q>0) --q;
                    else break;
                }
                else if (s[j]==')') ++r;
                continue;
            }
            else if (s[j]==')')
            {
                ++r;
                continue;
            }
            if (s[j]=='?')
            {
                if (r>0) r--;
                q++;
            }
            else if (s[j]=='(')
            {
                if (r>0) --r;
                else if (q>0) --q;
                else if (l+1>1) break;
            }
            if (!r) ++cnt;
        }
    }
    cout<<cnt<<"\n";
    return 0;
}


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

int main()
{
  int n;
  cin>>n;
  for(int i=0;i<n;i++)
  {
      int m,arr[3],t;
      cin>>m;
      memset(arr,0,sizeof(arr));
      for(int j=0;j<m;j++)
      {
          cin>>t;
          t=t%3;
         // cout<<t;
          arr[t]++;
      }
      int s=arr[0];
      if(arr[1]>0&&arr[2]>0)
      {s+=min(arr[1],arr[2]);
      arr[1]-=min(arr[1],arr[2]);
      arr[2]-=min(arr[1],arr[2]);}
      s+=arr[1]/3+arr[2]/3;

      cout<<s<<endl;
  }

    return 0;
}


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

int main()
{
  int n;
  cin>>n;
  for(int i=0;i<n;i++)
  {
      long long int m,arr[3],t;
      cin>>m;
      memset(arr,0,sizeof(arr));
      for(int j=0;j<m;j++)
      {
          cin>>t;
          t=t%3;
         // cout<<t;
          arr[t]++;
      }
      int s=arr[0];
      if(arr[1]>0&&arr[2]>0)
      {s+=min(arr[1],arr[2]);
      arr[1]-=min(arr[1],arr[2]);
      arr[2]-=min(arr[1],arr[2]);}
      s+=arr[1]/3+arr[2]/3;

      cout<<s<<endl;
  }

    return 0;
}


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

int main()
{
  int n;
  cin>>n;
  for(int i=0;i<n;i++)
  {
      long long int m,arr[3],t;
      cin>>m;
      memset(arr,0,sizeof(arr));
      for(int j=0;j<m;j++)
      {
          cin>>t;
          t=t%3;
         // cout<<t;
          arr[t]++;
      }
      int s=arr[0];

      int ms=min(arr[1],arr[2]);
      s+=ms;
      arr[1]-=ms;
      arr[2]-=ms;

      s+=(arr[1]+arr[2])/3;

      cout<<s<<endl;
  }

    return 0;
}


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

int main()
{
  int n;
  cin>>n;
  for(int i=0;i<n;i++)
  {
      long long int m,arr[3],t;
      cin>>m;
      memset(arr,0,sizeof(arr));
      for(int j=0;j<m;j++)
      {
          cin>>t;
          t=t%3;
         // cout<<t;
          arr[t]++;
      }
      int s=arr[0];

      s+=min(arr[1],arr[2]);
      arr[1]-=min(arr[1],arr[2]);
      arr[2]-=min(arr[1],arr[2]);

      s+=(arr[1]+arr[2])/3;

      cout<<s<<endl;
  }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int t, n;
int cnt[3];

int main(){
    cin >> t;
    for(int tc = 0; tc < t; ++tc){
        memset(cnt, 0, sizeof cnt);
        cin >> n;
        for(int i = 0; i < n; ++i){
            int x;
            cin >> x;
            ++cnt[x % 3];
        }

    	int res = cnt[0];
        int mn = min(cnt[1], cnt[2]);
        res += mn;
        cnt[1] -= mn, cnt[2] -= mn;
        res += (cnt[1] + cnt[2]) / 3;
        cout << res << endl;
    }
    return 0;
}


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

int main()
{
  int n;
  cin>>n;
  for(int i=0;i<n;i++)
  {
      int m,arr[3],t;
      cin>>m;
      memset(arr,0,sizeof(arr));
      for(int j=0;j<m;j++)
      {
          cin>>t;
          t=t%3;
         // cout<<t;
          arr[t]++;
      }
      int s=arr[0];
      s+=min(arr[1],arr[2]);
      arr[1]-=min(arr[1],arr[2]);
      arr[2]-=min(arr[1],arr[2]);
      s+=arr[1]/3+arr[2]/3;

      cout<<s<<endl;
  }

    return 0;
}


/*
*@Author  Chand Vachhani
*/

#include<bits/stdc++.h>
#define int         long long
#define double      long double
#define vi          vector<int>
#define vii         vector<vector<int>>
#define pii         pair<int,int>
#define pb          push_back
#define pf          push_front
#define vpii        vector<pair<int,int>>
#define fo(i,l,n)   for(int i{l}; i<n; i++)
#define rf(s)       for(auto &c : s)
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define PI          3.141592653589
#define sz(x)       (int)x.size()
#define bs(v,n)     binary_search(all((v)),(n))
#define lb(v,n)     lower_bound(all((v)),(n))
#define ub(v,n)     upper_bound(all((v)),(n))
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD         100000

using namespace std;

template<class T>
T big(T a, T b) { return a>b ? a : b; }
int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }
int lcm(int a, int b) { return a*b/gcd(a,b); }
bool mod(double a,double b) {return a/b - floor(a/b);}
int occurs(vi v,int n)  {  auto it=lb(v,n);auto it1=ub(v,n);int x=it1-it;return x;}
int logb(int base, int x) {return (log(x) / log(base));}
int pow(int n, int m){
    int ans=1;
    fo(i,0,m){
        ans*=n;
    }
}
int factors(int ans){
    int count=0;
    for (int i=1; i<=sqrt(ans); i++) {
        if (ans%i == 0)
        {if (ans/i == i) count++;
            else count+=2;}
    }
    return count;
}
bool isSubSequence(string str1, string str2, int m, int n)  // str1<str2
{
    // Base Cases
    if (m == 0) return true;
    if (n == 0) return false;

    // If last characters of two strings are matching
    if (str1[m-1] == str2[n-1])
        return isSubSequence(str1, str2, m-1, n-1);

    // If last characters are not matching
    return isSubSequence(str1, str2, m, n-1);
}

signed main()
{

     #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int n,m,p;
    cin >> n;
    fo(i,0,n){
        // vi a;
        cin >> m;
        int ct1=0,ct2=0,ct3=0,ct4=0;
        fo(j,0,m){
            cin >> p;
            int x = p%3;
            if(x==1)
                ct1++;
            else if(x==2)
                ct2++;
            else if(p==0)
                ct3++;
            else if(x==0)
                ct4++;
        }
        int ans=0;
        ans +=  ct4;
        int y=0;
        if(ct1 > ct2){
            ans+=ct2;
            ans += (ct1-ct2)/3;
        }
        else{
            ans+=ct1;
            ans+= ((ct2-ct1))/3;
        }
        cout << ans << endl;
    }

    return 0;
}


/*
*@Author  Chand Vachhani
*/

#include<bits/stdc++.h>
#define int         long long
#define double      long double
#define vi          vector<int>
#define vii         vector<vector<int>>
#define pii         pair<int,int>
#define pb          push_back
#define pf          push_front
#define vpii        vector<pair<int,int>>
#define fo(i,l,n)   for(int i{l}; i<n; i++)
#define rf(s)       for(auto &c : s)
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define PI          3.141592653589
#define sz(x)       (int)x.size()
#define bs(v,n)     binary_search(all((v)),(n))
#define lb(v,n)     lower_bound(all((v)),(n))
#define ub(v,n)     upper_bound(all((v)),(n))
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD         100000

using namespace std;

template<class T>
T big(T a, T b) { return a>b ? a : b; }
int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }
int lcm(int a, int b) { return a*b/gcd(a,b); }
bool mod(double a,double b) {return a/b - floor(a/b);}
int occurs(vi v,int n)  {  auto it=lb(v,n);auto it1=ub(v,n);int x=it1-it;return x;}
int logb(int base, int x) {return (log(x) / log(base));}
int pow(int n, int m){
    int ans=1;
    fo(i,0,m){
        ans*=n;
    }
}
int factors(int ans){
    int count=0;
    for (int i=1; i<=sqrt(ans); i++) {
        if (ans%i == 0)
        {if (ans/i == i) count++;
            else count+=2;}
    }
    return count;
}
bool isSubSequence(string str1, string str2, int m, int n)  // str1<str2
{
    // Base Cases
    if (m == 0) return true;
    if (n == 0) return false;

    // If last characters of two strings are matching
    if (str1[m-1] == str2[n-1])
        return isSubSequence(str1, str2, m-1, n-1);

    // If last characters are not matching
    return isSubSequence(str1, str2, m, n-1);
}

signed main()
{

     #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int n,m,p;
    cin >> n;
    fo(i,0,n){
        // vi a;
        cin >> m;
        int ct1=0,ct2=0,ct3=0,ct4=0;
        fo(j,0,m){
            cin >> p;
            int x = p%3;
            if(x==1)
                ct1++;
            else if(x==2)
                ct2++;
            else if(p==0)
                ct3++;
            else if(x==0)
                ct4++;
        }
        int ans=0;
        ans += ct1/3 + (2*ct2)/3 + ct4;
        int y=0;
        cout << ans;
        ct2 = ct2 - (2*ct2/3);
        if(ct1%3 !=0){
            y+=ct1%3;
        }
        if(ct2%3!=0){
            y+=2*(ct2%3);
            // y*=2;
        }
        ans += y/3;
        // int sum = accumulate(all(a),0);

        cout << ans << endl;
    }

    return 0;
}


/*
*@Author  Chand Vachhani
*/

#include<bits/stdc++.h>
#define int         long long
#define double      long double
#define vi          vector<int>
#define vii         vector<vector<int>>
#define pii         pair<int,int>
#define pb          push_back
#define pf          push_front
#define vpii        vector<pair<int,int>>
#define fo(i,l,n)   for(int i{l}; i<n; i++)
#define rf(s)       for(auto &c : s)
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define PI          3.141592653589
#define sz(x)       (int)x.size()
#define bs(v,n)     binary_search(all((v)),(n))
#define lb(v,n)     lower_bound(all((v)),(n))
#define ub(v,n)     upper_bound(all((v)),(n))
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD         100000

using namespace std;

template<class T>
T big(T a, T b) { return a>b ? a : b; }
int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }
int lcm(int a, int b) { return a*b/gcd(a,b); }
bool mod(double a,double b) {return a/b - floor(a/b);}
int occurs(vi v,int n)  {  auto it=lb(v,n);auto it1=ub(v,n);int x=it1-it;return x;}
int logb(int base, int x) {return (log(x) / log(base));}
int pow(int n, int m){
    int ans=1;
    fo(i,0,m){
        ans*=n;
    }
}
int factors(int ans){
    int count=0;
    for (int i=1; i<=sqrt(ans); i++) {
        if (ans%i == 0)
        {if (ans/i == i) count++;
            else count+=2;}
    }
    return count;
}
bool isSubSequence(string str1, string str2, int m, int n)  // str1<str2
{
    // Base Cases
    if (m == 0) return true;
    if (n == 0) return false;

    // If last characters of two strings are matching
    if (str1[m-1] == str2[n-1])
        return isSubSequence(str1, str2, m-1, n-1);

    // If last characters are not matching
    return isSubSequence(str1, str2, m, n-1);
}

signed main()
{

     #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int n,m,p;
    cin >> n;
    fo(i,0,n){
        // vi a;
        cin >> m;
        int ct1=0,ct2=0,ct3=0,ct4=0;
        fo(j,0,m){
            cin >> p;
            int x = p%3;
            if(x==1)
                ct1++;
            else if(x==2)
                ct2++;
            else if(p==0)
                ct3++;
            else if(x==0)
                ct4++;
        }
        int ans=0;
        ans +=  ct4;
        int y=0;
        if(ct1 > ct2){
            ans+=ct2;
            ans += (ct1-ct2)/3;
        }
        else{
            ans+=ct1;
            ans+= (2*(ct2-ct1))/3;
        }
        cout << ans << endl;
    }

    return 0;
}


/*
*@Author  Chand Vachhani
*/

#include<bits/stdc++.h>
#define int         long long
#define double      long double
#define vi          vector<int>
#define vii         vector<vector<int>>
#define pii         pair<int,int>
#define pb          push_back
#define pf          push_front
#define vpii        vector<pair<int,int>>
#define fo(i,l,n)   for(int i{l}; i<n; i++)
#define rf(s)       for(auto &c : s)
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define PI          3.141592653589
#define sz(x)       (int)x.size()
#define bs(v,n)     binary_search(all((v)),(n))
#define lb(v,n)     lower_bound(all((v)),(n))
#define ub(v,n)     upper_bound(all((v)),(n))
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD         100000

using namespace std;

template<class T>
T big(T a, T b) { return a>b ? a : b; }
int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }
int lcm(int a, int b) { return a*b/gcd(a,b); }
bool mod(double a,double b) {return a/b - floor(a/b);}
int occurs(vi v,int n)  {  auto it=lb(v,n);auto it1=ub(v,n);int x=it1-it;return x;}
int logb(int base, int x) {return (log(x) / log(base));}
int pow(int n, int m){
    int ans=1;
    fo(i,0,m){
        ans*=n;
    }
}
int factors(int ans){
    int count=0;
    for (int i=1; i<=sqrt(ans); i++) {
        if (ans%i == 0)
        {if (ans/i == i) count++;
            else count+=2;}
    }
    return count;
}
bool isSubSequence(string str1, string str2, int m, int n)  // str1<str2
{
    // Base Cases
    if (m == 0) return true;
    if (n == 0) return false;

    // If last characters of two strings are matching
    if (str1[m-1] == str2[n-1])
        return isSubSequence(str1, str2, m-1, n-1);

    // If last characters are not matching
    return isSubSequence(str1, str2, m, n-1);
}

signed main()
{

     #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int n,m,p;
    cin >> n;
    fo(i,0,n){
        vi a;
        cin >> m;
        fo(j,0,m){
            cin >> p;
            int x = p%3;
            if(p%3==0)
                x=3;
            if(p==0)
                x=0;
            a.pb(x);
        }
        int sum = accumulate(all(a),0);
        cout << sum/3 << endl;
    }

    return 0;
}


/*
*@Author  Chand Vachhani
*/

#include<bits/stdc++.h>
#define int         long long
#define double      long double
#define vi          vector<int>
#define vii         vector<vector<int>>
#define pii         pair<int,int>
#define pb          push_back
#define pf          push_front
#define vpii        vector<pair<int,int>>
#define fo(i,l,n)   for(int i{l}; i<n; i++)
#define rf(s)       for(auto &c : s)
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define PI          3.141592653589
#define sz(x)       (int)x.size()
#define bs(v,n)     binary_search(all((v)),(n))
#define lb(v,n)     lower_bound(all((v)),(n))
#define ub(v,n)     upper_bound(all((v)),(n))
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD         100000

using namespace std;

template<class T>
T big(T a, T b) { return a>b ? a : b; }
int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }
int lcm(int a, int b) { return a*b/gcd(a,b); }
bool mod(double a,double b) {return a/b - floor(a/b);}
int occurs(vi v,int n)  {  auto it=lb(v,n);auto it1=ub(v,n);int x=it1-it;return x;}
int logb(int base, int x) {return (log(x) / log(base));}
int pow(int n, int m){
    int ans=1;
    fo(i,0,m){
        ans*=n;
    }
}
int factors(int ans){
    int count=0;
    for (int i=1; i<=sqrt(ans); i++) {
        if (ans%i == 0)
        {if (ans/i == i) count++;
            else count+=2;}
    }
    return count;
}
bool isSubSequence(string str1, string str2, int m, int n)  // str1<str2
{
    // Base Cases
    if (m == 0) return true;
    if (n == 0) return false;

    // If last characters of two strings are matching
    if (str1[m-1] == str2[n-1])
        return isSubSequence(str1, str2, m-1, n-1);

    // If last characters are not matching
    return isSubSequence(str1, str2, m, n-1);
}

signed main()
{

     #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int n,m,p;
    cin >> n;
    fo(i,0,n){
        // vi a;
        cin >> m;
        int ct1=0,ct2=0,ct3=0,ct4=0;
        fo(j,0,m){
            cin >> p;
            int x = p%3;
            if(x==1)
                ct1++;
            else if(x==2)
                ct2++;
            else if(p==0)
                ct3++;
            else if(x==0)
                ct4++;
        }
        int ans=0;
        ans += ct1/3 + (2*ct2)/3 + ct4;
        int y=0;
        if(ct1%3 !=0){
            y+=ct1%3;
        }
        if(ct2%3!=0){
            y+=ct2%3;
            y*=2;
        }
        ans += y/3;
        // int sum = accumulate(all(a),0);

        cout << ans << endl;
    }

    return 0;
}


/*
*@Author  Chand Vachhani
*/

#include<bits/stdc++.h>
#define int         long long
#define double      long double
#define vi          vector<int>
#define vii         vector<vector<int>>
#define pii         pair<int,int>
#define pb          push_back
#define pf          push_front
#define vpii        vector<pair<int,int>>
#define fo(i,l,n)   for(int i{l}; i<n; i++)
#define rf(s)       for(auto &c : s)
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define PI          3.141592653589
#define sz(x)       (int)x.size()
#define bs(v,n)     binary_search(all((v)),(n))
#define lb(v,n)     lower_bound(all((v)),(n))
#define ub(v,n)     upper_bound(all((v)),(n))
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD         100000

using namespace std;

template<class T>
T big(T a, T b) { return a>b ? a : b; }
int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }
int lcm(int a, int b) { return a*b/gcd(a,b); }
bool mod(double a,double b) {return a/b - floor(a/b);}
int occurs(vi v,int n)  {  auto it=lb(v,n);auto it1=ub(v,n);int x=it1-it;return x;}
int logb(int base, int x) {return (log(x) / log(base));}
int pow(int n, int m){
    int ans=1;
    fo(i,0,m){
        ans*=n;
    }
}
int factors(int ans){
    int count=0;
    for (int i=1; i<=sqrt(ans); i++) {
        if (ans%i == 0)
        {if (ans/i == i) count++;
            else count+=2;}
    }
    return count;
}
bool isSubSequence(string str1, string str2, int m, int n)  // str1<str2
{
    // Base Cases
    if (m == 0) return true;
    if (n == 0) return false;

    // If last characters of two strings are matching
    if (str1[m-1] == str2[n-1])
        return isSubSequence(str1, str2, m-1, n-1);

    // If last characters are not matching
    return isSubSequence(str1, str2, m, n-1);
}

signed main()
{

     #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int n,m,p;
    cin >> n;
    fo(i,0,n){
        // vi a;
        cin >> m;
        int ct1=0,ct2=0,ct3=0,ct4=0;
        fo(j,0,m){
            cin >> p;
            int x = p%3;
            if(x==1)
                ct1++;
            else if(x==2)
                ct2++;
            else if(p==0)
                ct3++;
            else if(x==0)
                ct4++;
        }
        int ans=0;
        ans += ct1/3 + (2*ct2)/3 + ct4;
        int y=0;
        // cout << ans;
        ct2 = ct2 - (2*ct2/3);
        if(ct1%3 !=0){
            y+=ct1%3;
        }
        if(ct2%3!=0){
            y+=2*(ct2%3);
            // y*=2;
        }
        ans += y/3;
        // int sum = accumulate(all(a),0);

        cout << ans << endl;
    }

    return 0;
}


/*
*@Author  Chand Vachhani
*/

#include<bits/stdc++.h>
#define int         long long
#define double      long double
#define vi          vector<int>
#define vii         vector<vector<int>>
#define pii         pair<int,int>
#define pb          push_back
#define pf          push_front
#define vpii        vector<pair<int,int>>
#define fo(i,l,n)   for(int i{l}; i<n; i++)
#define rf(s)       for(auto &c : s)
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define PI          3.141592653589
#define sz(x)       (int)x.size()
#define bs(v,n)     binary_search(all((v)),(n))
#define lb(v,n)     lower_bound(all((v)),(n))
#define ub(v,n)     upper_bound(all((v)),(n))
#define tezz        ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MOD         100000

using namespace std;

template<class T>
T big(T a, T b) { return a>b ? a : b; }
int gcd(int a, int b) {  if (a == 0)  return b; return gcd(b % a, a); }
int lcm(int a, int b) { return a*b/gcd(a,b); }
bool mod(double a,double b) {return a/b - floor(a/b);}
int occurs(vi v,int n)  {  auto it=lb(v,n);auto it1=ub(v,n);int x=it1-it;return x;}
int logb(int base, int x) {return (log(x) / log(base));}
int pow(int n, int m){
    int ans=1;
    fo(i,0,m){
        ans*=n;
    }
}
int factors(int ans){
    int count=0;
    for (int i=1; i<=sqrt(ans); i++) {
        if (ans%i == 0)
        {if (ans/i == i) count++;
            else count+=2;}
    }
    return count;
}
bool isSubSequence(string str1, string str2, int m, int n)  // str1<str2
{
    // Base Cases
    if (m == 0) return true;
    if (n == 0) return false;

    // If last characters of two strings are matching
    if (str1[m-1] == str2[n-1])
        return isSubSequence(str1, str2, m-1, n-1);

    // If last characters are not matching
    return isSubSequence(str1, str2, m, n-1);
}

signed main()
{

     #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    int n,m,p;
    cin >> n;
    fo(i,0,n){
        vi a;
        cin >> m;
        fo(j,0,m){
            cin >> p;
            int x = p%3;
            if(p%3==0)
                x=3;
            a.pb(x);
        }
        int sum = accumulate(all(a),0);
        cout << sum/3 << endl;
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define LL           long long
#define ULL          long long unsigned
#define LD           long double
#define ff              first
#define ss              second
#define FOR(i,n)     for(int i=0;i<n;i++)
#define FLR(i,n)     for(LL i=0;i<n;i++)
#define RFOR(i,n)    for(int i=n-1;i>=0;i--)
#define RFLR(i,n)    for(LL i=n-1;i>=0;i--)
#define rep(i,a,b)   for(int i=a;i<=b;i++)
#define repl(i,a,b)  for(LL i=a;i<=b;i++)
#define fast         ios_base::sync_with_stdio(0);cin.tie(0);
#define all(a)       a.begin(),a.end()
#define ABS(x)       ((x)<0?-(x):(x))
#define NL           "\n"
#define pb           push_back
#define mk              make_pair
#define pi           acos(-1.0)
#define prec(n)      fixed << setprecision(n)
#define sq(a)          (a)*(a)
#define asi          cout<<"check"<<" ";

int main()
{
    fast;
    int t,n,a,b,x,y,ans,sum,cnt1,cnt2,i=0;
    cin>>t;
    while(t--){
        i++;
        ans=0,sum=0,cnt1=0,cnt2=0;
        cin>>n;
        FOR(i,n){
            cin>>x;
            x%=3;
            if(x==0)ans++;
            if(x==1)cnt1++;
            if(x==2)cnt2++;
            sum+=x;
        }
        ans+=min(cnt1,cnt2);
        cnt1-=min(cnt1,cnt2);
        cnt2-=min(cnt1,cnt2);
        ans+=cnt1/3;
        ans+=cnt2/3;
        cout<<ans<<NL;
        //if(i==6)cout<<"Sp "<<cnt1<<" "<<cnt2<<NL;
    }

}


#include<bits/stdc++.h>

using namespace std;

#define LL           long long
#define ULL          long long unsigned
#define LD           long double
#define ff              first
#define ss              second
#define FOR(i,n)     for(int i=0;i<n;i++)
#define FLR(i,n)     for(LL i=0;i<n;i++)
#define RFOR(i,n)    for(int i=n-1;i>=0;i--)
#define RFLR(i,n)    for(LL i=n-1;i>=0;i--)
#define rep(i,a,b)   for(int i=a;i<=b;i++)
#define repl(i,a,b)  for(LL i=a;i<=b;i++)
#define fast         ios_base::sync_with_stdio(0);cin.tie(0);
#define all(a)       a.begin(),a.end()
#define ABS(x)       ((x)<0?-(x):(x))
#define NL           "\n"
#define pb           push_back
#define mk              make_pair
#define pi           acos(-1.0)
#define prec(n)      fixed << setprecision(n)
#define sq(a)          (a)*(a)
#define asi          cout<<"check"<<" ";

int main()
{
    fast;
    int t,n,a,b,x,y,ans,sum,cnt1,cnt2,i=0;
    cin>>t;
    while(t--){
        i++;
        ans=0,sum=0,cnt1=0,cnt2=0;
        cin>>n;
        FOR(i,n){
            cin>>x;
            x%=3;
            if(x==0)ans++;
            if(x==1)cnt1++;
            if(x==2)cnt2++;
            sum+=x;
        }
        ans+=min(cnt1,cnt2);
        cnt1-=min(cnt1,cnt2);
        cnt2-=min(cnt1,cnt2);
        ans+=cnt1/3;
        ans+=cnt2/3;
        cout<<ans<<NL;
        if(i==14)cout<<"Sp "<<cnt1<<" "<<cnt2<<NL;
    }

}


#include<bits/stdc++.h>

using namespace std;

#define LL           long long
#define ULL          long long unsigned
#define LD           long double
#define ff              first
#define ss              second
#define FOR(i,n)     for(int i=0;i<n;i++)
#define FLR(i,n)     for(LL i=0;i<n;i++)
#define RFOR(i,n)    for(int i=n-1;i>=0;i--)
#define RFLR(i,n)    for(LL i=n-1;i>=0;i--)
#define rep(i,a,b)   for(int i=a;i<=b;i++)
#define repl(i,a,b)  for(LL i=a;i<=b;i++)
#define fast         ios_base::sync_with_stdio(0);cin.tie(0);
#define all(a)       a.begin(),a.end()
#define ABS(x)       ((x)<0?-(x):(x))
#define NL           "\n"
#define pb           push_back
#define mk              make_pair
#define pi           acos(-1.0)
#define prec(n)      fixed << setprecision(n)
#define sq(a)          (a)*(a)
#define asi          cout<<"check"<<" ";

int main()
{
    fast;
    int t,n,a,b,x,y,ans,sum;
    cin>>t;
    while(t--){
        ans=0,sum=0;
        cin>>n;
        FOR(i,n){
            cin>>x;
            x%=3;
            if(x==0)ans++;
            sum+=x;
        }
        ans+=sum/3;
        cout<<ans<<NL;
    }

}


#include<bits/stdc++.h>

using namespace std;

#define LL           long long
#define ULL          long long unsigned
#define LD           long double
#define ff              first
#define ss              second
#define FOR(i,n)     for(int i=0;i<n;i++)
#define FLR(i,n)     for(LL i=0;i<n;i++)
#define RFOR(i,n)    for(int i=n-1;i>=0;i--)
#define RFLR(i,n)    for(LL i=n-1;i>=0;i--)
#define rep(i,a,b)   for(int i=a;i<=b;i++)
#define repl(i,a,b)  for(LL i=a;i<=b;i++)
#define fast         ios_base::sync_with_stdio(0);cin.tie(0);
#define all(a)       a.begin(),a.end()
#define ABS(x)       ((x)<0?-(x):(x))
#define NL           "\n"
#define pb           push_back
#define mk              make_pair
#define pi           acos(-1.0)
#define prec(n)      fixed << setprecision(n)
#define sq(a)          (a)*(a)
#define asi          cout<<"check"<<" ";

int main()
{
    fast;
    int t,n,a,b,x,y,ans,sum,cnt1,cnt2,i=0;
    cin>>t;
    while(t--){
        i++;
        ans=0,sum=0,cnt1=0,cnt2=0;
        cin>>n;
        FOR(i,n){
            cin>>x;
            x%=3;
            if(x==0)ans++;
            if(x==1)cnt1++;
            if(x==2)cnt2++;
            sum+=x;
        }
        ans+=sum/3;
        cout<<ans<<NL;
        if(i==6)cout<<"Sp "<<cnt1<<" "<<cnt2<<NL;
    }

}


#include<bits/stdc++.h>

using namespace std;

#define LL           long long
#define ULL          long long unsigned
#define LD           long double
#define ff              first
#define ss              second
#define FOR(i,n)     for(int i=0;i<n;i++)
#define FLR(i,n)     for(LL i=0;i<n;i++)
#define RFOR(i,n)    for(int i=n-1;i>=0;i--)
#define RFLR(i,n)    for(LL i=n-1;i>=0;i--)
#define rep(i,a,b)   for(int i=a;i<=b;i++)
#define repl(i,a,b)  for(LL i=a;i<=b;i++)
#define fast         ios_base::sync_with_stdio(0);cin.tie(0);
#define all(a)       a.begin(),a.end()
#define ABS(x)       ((x)<0?-(x):(x))
#define NL           "\n"
#define pb           push_back
#define mk              make_pair
#define pi           acos(-1.0)
#define prec(n)      fixed << setprecision(n)
#define sq(a)          (a)*(a)
#define asi          cout<<"check"<<" ";

int main()
{
    fast;
    int t,n,a,b,x,y,ans,sum,cnt1,cnt2,k=0;
    cin>>t;
    while(t--){
        k++;
        ans=0,sum=0,cnt1=0,cnt2=0;
        cin>>n;
        FOR(i,n){
            cin>>x;
            if(k==14&&x%3==2)cout<<x<<"  ";
            x%=3;
            if(x==0)ans++;
            if(x==1)cnt1++;
            if(x==2)cnt2++;
            sum+=x;
        }
        ans+=min(cnt1,cnt2);
        cnt1-=min(cnt1,cnt2);
        cnt2-=min(cnt1,cnt2);
        ans+=cnt1/3;
        ans+=cnt2/3;
        cout<<ans<<NL;
        if(k==14)cout<<"Sp "<<cnt1<<" "<<cnt2<<NL;
    }

}


#include<bits/stdc++.h>

using namespace std;

#define LL           long long
#define ULL          long long unsigned
#define LD           long double
#define ff              first
#define ss              second
#define FOR(i,n)     for(int i=0;i<n;i++)
#define FLR(i,n)     for(LL i=0;i<n;i++)
#define RFOR(i,n)    for(int i=n-1;i>=0;i--)
#define RFLR(i,n)    for(LL i=n-1;i>=0;i--)
#define rep(i,a,b)   for(int i=a;i<=b;i++)
#define repl(i,a,b)  for(LL i=a;i<=b;i++)
#define fast         ios_base::sync_with_stdio(0);cin.tie(0);
#define all(a)       a.begin(),a.end()
#define ABS(x)       ((x)<0?-(x):(x))
#define NL           "\n"
#define pb           push_back
#define mk              make_pair
#define pi           acos(-1.0)
#define prec(n)      fixed << setprecision(n)
#define sq(a)          (a)*(a)
#define asi          cout<<"check"<<" ";

int main()
{
    fast;
    int t,n,a,b,x,y,ans,sum,cnt1,cnt2,k=0,mn;
    cin>>t;
    while(t--){
        k++;
        ans=0,sum=0,cnt1=0,cnt2=0;
        cin>>n;
        FOR(i,n){
            cin>>x;
            //if(k==14&&x%3==2)cout<<x<<"  ";
            x%=3;
            if(x==0)ans++;
            if(x==1)cnt1++;
            if(x==2)cnt2++;
            sum+=x;
        }
        //if(k==14)cout<<"Sp "<<cnt1<<" "<<cnt2<<NL;
        ans+=min(cnt1,cnt2);
        mn=min(cnt1,cnt2);
        cnt1-=mn;
        cnt2-=mn;
        ans+=cnt1/3;
        ans+=cnt2/3;
        cout<<ans<<NL;

    }

}


#include<bits/stdc++.h>

using namespace std;

#define LL           long long
#define ULL          long long unsigned
#define LD           long double
#define ff              first
#define ss              second
#define FOR(i,n)     for(int i=0;i<n;i++)
#define FLR(i,n)     for(LL i=0;i<n;i++)
#define RFOR(i,n)    for(int i=n-1;i>=0;i--)
#define RFLR(i,n)    for(LL i=n-1;i>=0;i--)
#define rep(i,a,b)   for(int i=a;i<=b;i++)
#define repl(i,a,b)  for(LL i=a;i<=b;i++)
#define fast         ios_base::sync_with_stdio(0);cin.tie(0);
#define all(a)       a.begin(),a.end()
#define ABS(x)       ((x)<0?-(x):(x))
#define NL           "\n"
#define pb           push_back
#define mk              make_pair
#define pi           acos(-1.0)
#define prec(n)      fixed << setprecision(n)
#define sq(a)          (a)*(a)
#define asi          cout<<"check"<<" ";

int main()
{
    fast;
    int t,n,a,b,x,y,ans,sum,cnt1,cnt2,i=0;
    cin>>t;
    while(t--){
        i++;
        ans=0,sum=0,cnt1=0,cnt2=0;
        cin>>n;
        FOR(i,n){
            cin>>x;
            if(i==14&&x%3==2)cout<<x<<"  ";
            x%=3;
            if(x==0)ans++;
            if(x==1)cnt1++;
            if(x==2)cnt2++;
            sum+=x;
        }
        ans+=min(cnt1,cnt2);
        cnt1-=min(cnt1,cnt2);
        cnt2-=min(cnt1,cnt2);
        ans+=cnt1/3;
        ans+=cnt2/3;
        cout<<ans<<NL;
        if(i==14)cout<<"Sp "<<cnt1<<" "<<cnt2<<NL;
    }

}


#include<bits/stdc++.h>

using namespace std;

#define LL           long long
#define ULL          long long unsigned
#define LD           long double
#define ff              first
#define ss              second
#define FOR(i,n)     for(int i=0;i<n;i++)
#define FLR(i,n)     for(LL i=0;i<n;i++)
#define RFOR(i,n)    for(int i=n-1;i>=0;i--)
#define RFLR(i,n)    for(LL i=n-1;i>=0;i--)
#define rep(i,a,b)   for(int i=a;i<=b;i++)
#define repl(i,a,b)  for(LL i=a;i<=b;i++)
#define fast         ios_base::sync_with_stdio(0);cin.tie(0);
#define all(a)       a.begin(),a.end()
#define ABS(x)       ((x)<0?-(x):(x))
#define NL           "\n"
#define pb           push_back
#define mk              make_pair
#define pi           acos(-1.0)
#define prec(n)      fixed << setprecision(n)
#define sq(a)          (a)*(a)
#define asi          cout<<"check"<<" ";

int main()
{
    fast;
    int t,n,a,b,x,y,ans,sum,cnt1,cnt2,k=0;
    cin>>t;
    while(t--){
        k++;
        ans=0,sum=0,cnt1=0,cnt2=0;
        cin>>n;
        FOR(i,n){
            cin>>x;
            if(k==14&&x%3==2)cout<<x<<"  ";
            x%=3;
            if(x==0)ans++;
            if(x==1)cnt1++;
            if(x==2)cnt2++;
            sum+=x;
        }
        if(k==14)cout<<"Sp "<<cnt1<<" "<<cnt2<<NL;
        ans+=min(cnt1,cnt2);
        cnt1-=min(cnt1,cnt2);
        cnt2-=min(cnt1,cnt2);
        ans+=cnt1/3;
        ans+=cnt2/3;
        cout<<ans<<NL;

    }

}


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

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n,i,digit=0,sum=0;
        cin >> n;
        int arr[n];

        for(i=0; i<n; i++)
        {
            cin >> arr[i];
        }
        for(i=0; i<n; i++)
        {
            if(arr[i]%3==0)
                digit++;
        }

        int m=n-digit;
        int j=0,a[m];

        for(i=0; i<n ; i++)
        {
            if(arr[i]%3!=0)
            {
                a[j]=(arr[i]%3);
                j++;
            }
        }
        for(i=0; i<m; i++)
        {
            sum=sum+a[i];
        }
        cout << digit + sum/3 <<  "\n";
    }
}


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

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n,i,m,digit=0,sum=0,one=0,two=0;
        cin >> n;
        int arr[n];

        for(i=0; i<n; i++)
        {
            cin >> arr[i];
        }
        for(i=0; i<n; i++)
        {
            if(arr[i]%3==0)
                digit++;
        }
        if(digit==n)
            cout << digit << "\n";
        else
        {
            for(i=0; i<n ; i++)
            {
                if(arr[i]%3==1)
                {
                    one++;
                    sum=sum+arr[i]%3;
                }
                else if(arr[i]%3==2)
                {
                    two++;
                    sum=sum+arr[i]%3;
                }
            }
            m=two*2-one;
            if(m%6==4)
                cout << digit + (sum/3-1) <<  "\n";
            else
                cout << digit+sum/3 << "\n";
        }
    }
}


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

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n,i,m,digit=0,sum=0,one=0,two=0;
        cin >> n;
        int arr[n];

        for(i=0; i<n; i++)
        {
            cin >> arr[i];
        }
        for(i=0; i<n; i++)
        {
            if(arr[i]%3==0)
                digit++;
        }
        if(digit==n)
            cout << digit << "\n";
        else
        {
            for(i=0; i<n ; i++)
            {
                if(arr[i]%3==1)
                    one++;
                else if(arr[i]%3==2)
                    two++;
            }
            digit=digit+min(one,two);
            if(one>two)
            {
                one=one-two;
                cout << digit+one/3 << "\n";
            }
            else
            {
                two=two-one;
                cout << digit+two/3 << "\n";
            }

        }
    }
}


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

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n,i,digit=0,sum=0;
        cin >> n;
        int arr[n];

        for(i=0; i<n; i++)
        {
            cin >> arr[i];
        }
        for(i=0; i<n; i++)
        {
            if(arr[i]%3==0)
                digit++;
        }

        int m=n-digit;
        if(m==0)
        {
            cout << digit << "\n";
        }
        else
        {
            int j=0,a[m];

            for(i=0; i<n ; i++)
            {
                if(arr[i]%3!=0)
                {
                    a[j]=(arr[i]%3);
                    j++;
                }
            }
            for(i=0; i<m; i++)
            {
                sum=sum+a[i];
            }
            int l=floor(sum/3);
            cout << digit + l <<  "\n";
        }

    }
}


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

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n,i,digit=0,sum=0;
        cin >> n;
        int arr[n];

        for(i=0; i<n; i++)
        {
            cin >> arr[i];
        }
        for(i=0; i<n; i++)
        {
            if(arr[i]%3==0)
                digit++;
        }

        int m=n-digit;
        if(m==0)
        {
            cout << digit << "\n";
        }
        else
        {
            int j=0,a[m];

            for(i=0; i<n ; i++)
            {
                if(arr[i]%3!=0)
                {
                    a[j]=(arr[i]%3);
                    j++;
                }
            }
            for(i=0; i<m; i++)
            {
                sum=sum+a[i];
            }
            cout << digit + sum/3 <<  "\n";
        }

    }
}


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

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n,i,m,digit=0,sum=0,one=0,two=0;
        cin >> n;
        int arr[n];

        for(i=0; i<n; i++)
        {
            cin >> arr[i];
        }
        for(i=0; i<n; i++)
        {
            if(arr[i]%3==0)
                digit++;
        }
        if(digit==n)
            cout << digit << "\n";
        else
        {
            for(i=0; i<n ; i++)
            {
                if(arr[i]%3==1)
                {
                    one++;
                    sum=sum+arr[i]%3;
                }
                else if(arr[i]%3==2)
                {
                    two++;
                    sum=sum+arr[i]%3;
                }
            }
            m=two*2-one*2;
            if(m%6==4)
                cout << digit + (sum/3-1) <<  "\n";
            else
                cout << digit+sum/3 << "\n";
        }
    }
}


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

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n,i,digit=0,sum=0;
        cin >> n;
        int arr[n];

        for(i=0; i<n; i++)
        {
            cin >> arr[i];
        }
        for(i=0; i<n; i++)
        {
            if(arr[i]%3==0)
                digit++;
        }
        if(digit==n)
        {
            cout << digit << "\n";
        }
        else
        {
            for(i=0; i<n ; i++)
            {
                if(arr[i]%3!=0)
                    sum=sum+arr[i]%3;
            }
            cout << digit + sum/3 <<  "\n";
        }
    }
}


#include<bits/stdc++.h>
typedef long long ll;

const int N = int(2e5) + 9;
using namespace std;

int main()
{
    ll t, n, tmp;
    cin>>t;

    for(ll i = 0; i < t; i++){
        ll arr[N];
        cin>>n;
        for(ll j = 0; j < n; j++){ cin>>tmp; arr[j]=tmp; }
        ll ones=0, twos=0, threes=0;
        for(ll j = 0; j < n; j++){ if(arr[j]==1)ones++;if(arr[j]==2)twos++;if(arr[j]==3)threes++; }
        ll leftover=(ones-min(threes+ones, threes+twos))/3;

        cout<<leftover+min(threes+ones, threes+twos)<<endl;
    }
}


#include<bits/stdc++.h>
typedef long long ll;

const int N = int(2e5) + 9;
using namespace std;

int main()
{
    ll t, n, tmp;
    cin>>t;

    for(ll i = 0; i < t; i++){
        ll arr[N];
        cin>>n;
        for(ll j = 0; j < n; j++){ cin>>tmp; arr[j]=tmp; }
        ll ones=0, twos=0, threes=0, l=0;
        for(ll j = 0; j < n; j++){ if(arr[j]==1)ones++;if(arr[j]==2)twos++;if(arr[j]==3)threes++; }
        if(ones-min(threes+ones, threes+twos)>=3){
            l=ones-min(threes+ones, threes+twos)/3;
        }
        cout<<l+min(threes+ones, threes+twos)<<endl;
    }
}


#include<bits/stdc++.h>
typedef long long ll;

const int N = int(2e5) + 9;
using namespace std;

int main()
{
    ll t, n, tmp;
    cin>>t;

    for(ll i = 0; i < t; i++){
        ll arr[N];
        cin>>n;
        for(ll j = 0; j < n; j++){ cin>>tmp; arr[j]=tmp; }
        ll ones=0, twos=0, threes=0, l=0;
        for(ll j = 0; j < n; j++){ if(arr[j]==1)ones++;if(arr[j]==2)twos++;if(arr[j]==3)threes++; }
        if(ones>twos&&ones-twos>=3){
            l=(ones-twos)/3;
        }
        cout<<l+min(threes+ones, threes+twos)<<endl;
    }
}


#include<bits/stdc++.h>
typedef long long ll;

const int N = int(2e5) + 9;
using namespace std;

int main()
{
    ll t, n, tmp;
    cin>>t;

    for(ll i = 0; i < t; i++){
        cin>>n;
        ll arr[3]; arr[1]=arr[2]=arr[0]=0;
        for(ll j = 0; j < n; j++){
            cin>>tmp;
            arr[tmp%3]++;
        }
        cout<<arr[0]+((arr[1]>arr[2])?(arr[2]+(arr[1]-arr[2])/3):(arr[1]+arr[2]-arr[1]/3))<<endl;
    }
}


#include<bits/stdc++.h>
typedef long long ll;

const int N = int(2e5) + 9;
using namespace std;

int main()
{
    ll t, n, tmp;
    cin>>t;

    for(ll i = 0; i < t; i++){
        ll arr[N];
        cin>>n;
        for(ll j = 0; j < n; j++){ cin>>tmp; arr[j]=tmp; }
        ll ones=0, twos=0, threes=0;
        for(ll j = 0; j < n; j++){ if(arr[j]==1)ones++;if(arr[j]==2)twos++;if(arr[j]==3)threes++; }
        ll leftover=(ones-min(threes+ones, threes+twos))/3;
        leftover=(leftover>0?leftover:0);
        cout<<leftover+min(threes+ones, threes+twos)<<endl;
    }
}


#include<bits/stdc++.h>
typedef long long ll;

const int N = int(2e5) + 9;
using namespace std;

int main()
{
    ll t, n, tmp;
    cin>>t;

    for(ll i = 0; i < t; i++){
        ll arr[N];
        cin>>n;
        for(ll j = 0; j < n; j++){ cin>>tmp; arr[j]=tmp; }
        ll ones=0, twos=0, threes=0, l=0;
        for(ll j = 0; j < n; j++){ if(arr[j]==1)ones++;if(arr[j]==2)twos++;if(arr[j]==3)threes++; }
        if(ones-min(threes+ones, threes+twos)>=3){
            l=ones-min(threes+ones, threes+twos)/3
        }
        cout<<l+min(threes+ones, threes+twos)<<endl;
    }
}


#include<bits/stdc++.h>
typedef long long ll;

const int N = int(2e5) + 9;
using namespace std;

int main()
{
    ll t, n, tmp;
    cin>>t;

    for(ll i = 0; i < t; i++){
        cin>>n;
        ll arr[3]; arr[1]=arr[2]=arr[0]=0;
        for(ll j = 0; j < n; j++){
            cin>>tmp;
            arr[tmp%3]++;
        }
        cout<<arr[0]+((arr[1]>arr[2])?arr[2]+(arr[1]-arr[2])/3:arr[1]+(arr[2]-arr[1])/3)<<endl;
    }
}


#include<iostream>
using namespace std;

int main(){
    int t;
    cin >> t;

    while(t){
        int arr_len,counter = 0;
        int arr[3] = {};
        cin >> arr_len;

        for(int i=0;i<arr_len;++i){
            long long int input = 0;
            cin >> input;
            ++arr[input%3];
        }

        counter = arr[0] + min(arr[1],arr[2]);
        arr[1] -= min(arr[1],arr[2]);
        arr[2] -= min(arr[1],arr[2]);
        counter += arr[1]/3 + arr[2]/3 ;

        cout << counter << endl;

        --t;
    }

    return 0;
}


#include<iostream>
using namespace std;

int main(){
    int t;
    cin >> t;

    while(t){
        int arr_len,counter = 0;
        int arr[3] = {};
        cin >> arr_len;

        for(int i=0;i<arr_len;++i){
            long int input = 0;
            cin >> input;
            ++arr[input%3];
        }

        counter = arr[0] + min(arr[1],arr[2]);
        arr[1] -= min(arr[1],arr[2]);
        arr[2] -= min(arr[1],arr[2]);
        counter += arr[1]/3 + arr[2]/3 ;

        cout << counter << endl;

        --t;
    }

    return 0;
}


#include<iostream>
using namespace std;

int main(){
    int t;
    cin >> t;

    while(t){
        int arr_len=0,counter = 0;
        int arr[3] = {0};
        cin >> arr_len;

        for(int i=0;i<arr_len;++i){
            long long int input = 0;
            cin >> input;
            ++arr[input%3];
        }

        counter = arr[0] + min(arr[1],arr[2]);
        arr[1] -= min(arr[1],arr[2]);
        arr[2] -= min(arr[1],arr[2]);
        counter += arr[1]/3 + arr[2]/3 ;

        cout << counter << endl;

        --t;
    }

    return 0;
}


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

int main(){
    int t;
    cin >> t;

    while(t){
        int arr_len=0,counter = 0;
        int arr[3] = {0};
        cin >> arr_len;

        for(int i=0;i<arr_len;++i){
            long long int input = 0;
            cin >> input;
            ++arr[input%3];
        }

        counter = arr[0] + min(arr[1],arr[2]);
        arr[1] -= min(arr[1],arr[2]);
        arr[2] -= min(arr[1],arr[2]);
        counter += arr[1]/3 + arr[2]/3 ;

        cout << counter << endl;

        --t;
    }

    return 0;
}


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

int main(){
    int t;
    cin >> t;

    while(t){
        int arr_len=0,counter = 0;
        int arr[3] = {0};
        cin >> arr_len;
        long long int input[arr_len] = {0};

        for(int i=0;i<arr_len;++i){
            cin >> input[i];
            ++arr[input[i]%3];
        }

        counter = arr[0] + min(arr[1],arr[2]);
        arr[1] -= min(arr[1],arr[2]);
        arr[2] -= min(arr[1],arr[2]);
        counter += arr[1]/3 + arr[2]/3 ;

        cout << counter << endl;

        --t;
    }

    return 0;
}