#include <iostream>
#include <vector>
#include <string>
int longestValidParentheses(std::string s) {
int n = s.size();
std::vector<int> dp(n, 0);
int maxLen = 0;
for (int i = 1; i < n; i++) {
if (s[i] == ')') {
int j = i - dp[i - 1] - 1;
if (j >= 0 && s[j] == '(') {
dp[i] = dp[i - 1] + 2;
if (j - 1 >= 0) dp[i] += dp[j - 1];
}
}
maxLen = std::max(maxLen, dp[i]);
}
return maxLen;
}
int main() {
std::string s = "((()()))((("; // balanced parentheses = ((()())) then length = 8
std::cout << longestValidParentheses(s) << std::endl;
}
/*
run:
8
*/