Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <string>
- using namespace std;
- vector<pair<pair<int, int>, pair<int, int> > > v;
- vector<vector<int> > dp;
- pair<pair<int, int>, pair<int, int> > count(int i, string a)
- {
- pair<pair<int, int>, pair<int, int> > b = { {i, a.size()}, { 0, 0 } };
- for (int i = 0; i < a.size(); i++)
- {
- if (a[i] == '(')
- b.second.second++;
- else
- b.second.second--;
- b.second.first = min(b.second.first, b.second.second);
- }
- return b;
- }
- bool cmp(const pair<pair<int, int>, pair<int, int> >& a, const pair<pair<int, int>, pair<int, int> >& b)
- {
- return a.second.first == 0 || min(a.second.second, a.second.first + b.second.second) < min(b.second.second, b.second.first + a.second.second);
- }
- int main()
- {
- int n;
- cin >> n;
- string c;
- for (int i = 0; i < n; i++)
- {
- cin >> c;
- pair<int, int> d = count(c);
- strings f;
- f.s = c;
- f.bal = d.second;
- f.minbal = d.first;
- v.push_back(f);
- }
- sort(v.begin(), v.end(), cmp);
- dp.resize(n);
- for (int i = 0; i < n; i++)
- {
- dp[i].assign(5010, 0);
- }
- dp[0][v[0].bal] = v[0].s.size();
- for (int i = 1; i < n; i++)
- {
- for (int j = 0; j < 5010; j++)
- {
- dp[i][v[i].bal + j] = max(dp[i][v[i].bal + j], dp[i - 1][j] + (int)v[i].s.size());
- }
- }
- cout << dp[n - 1][0];
- return 0;
- }
Add Comment
Please, Sign In to add comment