Problem:
坑道数式
ある日廃坑を探検していたあなたは、坑道に長い数式
S
が書かれているのを発見した。大きな数が好きなあなたは、チョークを取り出し、数式を計算した結果ができるだけ大きくなるように
(
または
)
を書き加えることにした。書き加えた後も数式になっていなければならないとすると、最大でいくつにできるか。
文字と文字の間は十分広く空いていて、
(
または
)
であればいくつでも書き加えることができる。最終的に数式になっていれば、最初のかっこの対応が崩れるように
(
または
)
を書いてもよい（Sample 2参照）。
また、ここでは以下のBNFで定義される<expr>を数式と呼ぶ。数式中の数は全て一桁である。
<expr>  ::= "(" <expr> ")"
         | <term> "+" <term>
         | <term> "-" <term>
<term>  ::= <digit> | <expr>
<digit> ::= "0" | "1" | "2" | "3" | "4"
          | "5" | "6" | "7" | "8" | "9"
Constraints
3 ≤ |S| ≤ 200
S
は数式を表す。
Input Format
入力は以下の形式で標準入力から与えられる。
S
Output Format
答えを整数で出力せよ。
Sample Input 1
1-(2+3-4+5)
Sample Output 1
5
1-(2+3-(4+5))が最大となる。
Sample Input 2
1-(2+3+4)
Sample Output 2
0
(1-(2+3)+4)が最大となる。
Sample Input 3
1-(2+3)
Sample Output 3
-4
1-(2)+(3)はここでいう数式ではないことに注意。

Submitted solutions:

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

using ll = long long;

using P = pair<int, int>;

constexpr int INF = 1e9;

P memo[201][201];

P rec(int l, int r, string const& s) {
    P& res = memo[l][r];
    if(res != make_pair(-INF, INF)) {
        return res;
    }
    if(l == r) {
        int v = s[l] - '0';
        return make_pair(v, v);
    }
    if(s[r] == ')' && r-l >= 3) {
        P p = rec(l, r-1, s);
        res.first = max(res.first, p.first);
        res.second = min(res.second, p.second);
    }
    if(s[l] == '(' && r-l >= 3) {
        P p = rec(l+1, r, s);
        res.first = max(res.first, p.first);
        res.second = min(res.second, p.second);
    }
    for(int i=l+1; i<r; ++i) {
        char op = s[i];
        if(op == '+' || op == '-') {
            P rl = rec(l, i-1, s);
            P rr = rec(i+1, r, s);
            if(op == '+') {
                res.first = max(res.first, rl.first + rr.first);
                res.second = min(res.second, rl.second + rr.second);
            } else {
                res.first = max(res.first, rl.first - rr.second);
                res.second = min(res.second, rl.second - rr.first);
            }
        }
    }
    return res;
}

int main() {
    string s;
    cin >> s;
    for(int i=0; i<s.size(); ++i) {
        for(int j=0; j<s.size(); ++j) {
            memo[i][j] = make_pair(-INF, INF);
        }
    }
    cout << rec(0, s.size()-1, s).first << endl;
}
accuracy: 1
time_ms: 340
memory_kb: 3692
final_rank: 12

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

string S;
string T;
int dp_max[209][209][409];
int dp_min[209][209][409];
int kakko[209];
bool used[209][209][409];

pair<int, int> dfs(int cl, int cr, int kakko1, int kakko2) {
    int idx = kakko1;
    if (kakko1 == 1) idx = T.size() + kakko2;

    // Case 1
    if (cl == cr) {
        if (kakko1 == 1 && kakko[cl + 0] >= 1) return make_pair((1 << 29), -(1 << 29));
        if (kakko2 == 1 && kakko[cl + 1] >= 1) return make_pair((1 << 29), -(1 << 29));
        dp_max[cl][cr][idx] = (T[cl] - '0');
        dp_min[cl][cr][idx] = (T[cl] - '0');
        return make_pair(dp_min[cl][cr][idx], dp_max[cl][cr][idx]);
    }
    if (used[cl][cr][idx] == true) return make_pair(dp_min[cl][cr][idx], dp_max[cl][cr][idx]);
    used[cl][cr][idx] = true;

    // Case 2
    vector<int> cand;
    for (int i = cl; i <= cr; i++) {
        int offset = 0;
        if (T[i] == '+') offset = +1;
        if (T[i] == '-') offset = -1;
        if (offset == 0) continue;
        pair<int, int> Z1 = dfs(cl, i - 1, min(2, kakko1 + 1), 1);
        pair<int, int> Z2 = dfs(i + 1, cr, 1, min(2, kakko2 + 1));
        if (Z1 == make_pair((1 << 29), -(1 << 29))) continue;
        if (Z2 == make_pair((1 << 29), -(1 << 29))) continue;
        dp_min[cl][cr][idx] = min(dp_min[cl][cr][idx], Z1.first  + offset * Z2.first );
        dp_max[cl][cr][idx] = max(dp_max[cl][cr][idx], Z1.first  + offset * Z2.first );
        dp_min[cl][cr][idx] = min(dp_min[cl][cr][idx], Z1.first  + offset * Z2.second);
        dp_max[cl][cr][idx] = max(dp_max[cl][cr][idx], Z1.first  + offset * Z2.second);
        dp_min[cl][cr][idx] = min(dp_min[cl][cr][idx], Z1.second + offset * Z2.first );
        dp_max[cl][cr][idx] = max(dp_max[cl][cr][idx], Z1.second + offset * Z2.first );
        dp_min[cl][cr][idx] = min(dp_min[cl][cr][idx], Z1.second + offset * Z2.second);
        dp_max[cl][cr][idx] = max(dp_max[cl][cr][idx], Z1.second + offset * Z2.second);
    }

    // Return
    // cout << cl << " " << cr << " " << kakko1 << " " << kakko2 << " " << dp_min[cl][cr][idx] << " " << dp_max[cl][cr][idx] << endl;
    return make_pair(dp_min[cl][cr][idx], dp_max[cl][cr][idx]);
}

int main() {
    // Step 1. Input
    cin >> S;
    for (int i = 0; i < S.size(); i++) {
        if (S[i] == '(') kakko[T.size()] += 1;
        else if (S[i] == ')') kakko[T.size()] += 1;
        else T += S[i];
    }
    /*cout << T << endl;
    for (int i = 0; i <= T.size(); i++) cout << kakko[i] << " "; cout << endl;*/

    // Step 2. Initialize
    for (int i = 0; i < (int)T.size(); i++) {
        for (int j = 0; j < (int)T.size(); j++) {
            for (int k = 0; k <= (int)2 * T.size(); k++) dp_min[i][j][k] = +(1 << 29);
            for (int k = 0; k <= (int)2 * T.size(); k++) dp_max[i][j][k] = -(1 << 29);
        }
    }

    // Step 3. DFS
    pair<int, int> Answer = dfs(0, T.size() - 1, 1, 1);
    cout << Answer.second << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 155872
final_rank: 20

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
// #include<atcoder/scc>
using namespace std;
// using namespace atcoder;
const int inf = 1 << 30;

void solve(){   
	string S;
	cin >> S;

	map<string, pair<int, int>> memo;
	auto dfs=[& memo](auto self, string S) -> pair<int, int> {
		while(S[0] == '('){
			S = S.substr(1, S.size() - 1);
		}
		while(S[S.size() - 1] == ')'){
			S = S.substr(0, S.size() - 1);
		}
		if(S.size() == 1) return {stoi(S), stoi(S)};
		else if(memo.count(S)) return memo[S];
		pair<int, int> res = {inf, -inf};
		int le = S.size();
		for(int i = 0; i < le; i++){
			if((i >= 2 && S[i - 2] == '(') || (i + 2 < le && S[i + 2] == ')')){
				continue;
			}
			if(S[i] == '+'){
				auto res1 = self(self, S.substr(0, i));
				auto res2 = self(self, S.substr(i + 1, le - i - 1));
				res.first = min(res.first, res1.first + res2.first);
				res.second = max(res.second, res1.second + res2.second);
			}
			else if(S[i] == '-'){
				auto res1 = self(self, S.substr(0, i));
				auto res2 = self(self, S.substr(i + 1, le - i - 1));
				res.first = min(res.first, res1.first - res2.second);
				res.second = max(res.second, res1.second - res2.first);
			}
		}
		// cout << S << " " << res.first << " " << res.second << endl;
		memo[S] = res;
		return res;
	};

	int ans = dfs(dfs, S).second;
	cout << ans << "\n";

}

int main(){
	cin.tie(0)->sync_with_stdio(0);
	int t;
	t = 1;
	// cin >> t;
	while(t--) solve();
}
accuracy: 1
time_ms: 60
memory_kb: 4264
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
#define INF 1e18 + 10
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout<<x<<endl;
#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define lp(i,n) repi(i,0,n)
#define repn(i,n) for(int i=n;i>=0;i--)
#define int long long
#define endl "\n"

typedef pair<int,int> PII;
typedef pair<int,string> PIS;
typedef pair<string,int> PSI;

template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b; 
    return true;
  }
  return false;
}

template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}


signed main(){
  cin.tie(0);	
  ios::sync_with_stdio(false);
  string s;
  cin>>s;
  int dpmx[110][110]={};
  int dpmn[110][110]={};
  rep(i,110){
    rep(j,110){
      dpmx[i][j]=-INF;
      dpmn[i][j]=INF;
    }
  }
  int nex=0;
  vector<int> plmy,l,r;
  rep(i,s.size() ){
    if(isdigit(s[i]) ){
      dpmx[nex][nex]=s[i]-'0';
      dpmn[nex][nex]=s[i]-'0';
      nex++;

      if(i!=0 && s[i-1]=='(') l.push_back(1);
      else l.push_back(0);
      if(i!=s.size()-1 && s[i+1]==')') r.push_back(1);
      else r.push_back(0);

      
    }
    if(s[i]=='+') plmy.push_back(1);
    if(s[i]=='-') plmy.push_back(0);

  }

  repi(siz,1,nex){
    rep(p,nex){
    rep(L,nex){
      int R=L+siz;
      if(R < nex){
	
	int mx=-INF,mn=INF;
	repi(mid,L,R){
	  
	  
	  
	  if(r[L]!=1 && l[R]!=1 && dpmx[L][mid]!=-INF && dpmn[mid+1][R]!=INF){
	    if(plmy[mid]==1){
	      chmax(mx,dpmx[L][mid]+dpmx[mid+1][R]);
	      chmin(mn,dpmn[L][mid]+dpmn[mid+1][R]);
	    }else{
	      chmax(mx,dpmx[L][mid]-dpmn[mid+1][R]);
	      chmin(mn,dpmn[L][mid]-dpmx[mid+1][R]);
	    }

	    
	  }
	}
	dpmx[L][R]=mx;
	dpmn[L][R]=mn;
	}
      }
    }
  }
  /*rep(i,5){
    rep(j,5){
      cout<<'('<<dpmx[i][j]<<" "<<dpmn[i][j]<<')';
    }
    cout<<endl;
    }*/
  cout<<dpmx[0][nex-1]<<endl;
  
  
  

  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3652
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
#define INF 1e18 + 10
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout<<x<<endl;
#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define lp(i,n) repi(i,0,n)
#define repn(i,n) for(int i=n;i>=0;i--)
#define int long long
#define endl "\n"

typedef pair<int,int> PII;
typedef pair<int,string> PIS;
typedef pair<string,int> PSI;

template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b; 
    return true;
  }
  return false;
}

template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}


signed main(){
  cin.tie(0);	
  ios::sync_with_stdio(false);
  string s;
  cin>>s;
  int dpmx[110][110]={};
  int dpmn[110][110]={};
  rep(i,110){
    rep(j,110){
      dpmx[i][j]=-INF;
      dpmn[i][j]=INF;
    }
  }
  int nex=0;
  vector<int> plmy,l,r;
  rep(i,s.size() ){
    if(isdigit(s[i]) ){
      dpmx[nex][nex]=s[i]-'0';
      dpmn[nex][nex]=s[i]-'0';
      nex++;

      if(i!=0 && s[i-1]=='(') l.push_back(1);
      else l.push_back(0);
      if(i!=s.size()-1 && s[i+1]==')') r.push_back(1);
      else r.push_back(0);

      
    }
    if(s[i]=='+') plmy.push_back(1);
    if(s[i]=='-') plmy.push_back(0);

  }

  repi(siz,1,nex){
    rep(p,nex){
    rep(L,nex){
      int R=L+siz;
      if(R < nex){
	
	int mx=-INF,mn=INF;
	repi(mid,L,R){
	  
	  
	  
	  if(r[L]!=1 && l[R]!=1 && dpmx[L][mid]!=-INF && dpmn[mid+1][R]!=INF){
	    if(plmy[mid]==1){
	      chmax(mx,dpmx[L][mid]+dpmx[mid+1][R]);
	      chmin(mn,dpmn[L][mid]+dpmn[mid+1][R]);
	    }else{
	      chmax(mx,dpmx[L][mid]-dpmn[mid+1][R]);
	      chmin(mn,dpmn[L][mid]-dpmx[mid+1][R]);
	    }

	    
	  }
	}
	dpmx[L][R]=mx;
	dpmn[L][R]=mn;
	}
      }
    }
  }
  /*rep(i,5){
    rep(j,5){
      cout<<'('<<dpmx[i][j]<<" "<<dpmn[i][j]<<')';
    }
    cout<<endl;
    }*/
  cout<<dpmx[0][nex-1]<<endl;
  
  
  

  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3632
final_rank: 2

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;


#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()

#define MOD 1000000007

template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
const double EPS = 1e-6, PI = acos(-1);
const double pi = 3.141592653589793238462643383279;
//ここから編集    
typedef string::const_iterator State;
ll GCD(ll a, ll b){
  return (b==0)?a:GCD(b, a%b);
}
ll LCM(ll a, ll b){
  return a/GCD(a, b) * b;
}
template< int mod >
struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt< 1000000007 >;
template< typename T >
struct Combination {
  vector< T > _fact, _rfact, _inv;

  Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[sz] /= _fact[sz];
    for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }

  inline T fact(int k) const { return _fact[k]; }

  inline T rfact(int k) const { return _rfact[k]; }

  inline T inv(int k) const { return _inv[k]; }

  T P(int n, int r) const {
    if(r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }

  T C(int p, int q) const {
    if(q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }

  T H(int n, int r) const {
    if(n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};

int modpow(ll x, ll n, int mod) {
  ll res = 1;
  while(n) {
    if(n&1) res = res*x % mod;
    x = x*x%mod;
    n >>= 1;
  }
  return res;
}
string s; 
pair<ll, ll> dp[210][210];
pair<ll, ll> rec(int l, int r) {

  if(l+1==r) {
    return make_pair(s[l]-'0', s[l]-'0');
  }
  if(dp[l][r].first != INT_MIN) return dp[l][r];

  if(s[l] == '(' && r-l>3) return dp[l][r] = rec(l+1,r);
  if(s[r-1] == ')' && r-l>3) return dp[l][r] = rec(l, r-1);

  pair<ll, ll> res = make_pair(INT_MIN, INT_MAX);
  for(int k=l+1; k<r; k++) {
    if(s[k] == '+' || s[k] == '-') {
      auto lt = rec(l, k);
      auto rt = rec(k+1, r);

      if(s[k] == '+') {
        res.first = max(res.first, lt.first + rt.first);
        res.second = min(res.second, lt.second + rt.second);
      }else{
        res.first = max(res.first, lt.first - rt.second);
        res.second = min(res.second, lt.second - rt.first);
      }
    }
  }
  return dp[l][r] = res;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(7);
  
  cin >> s;
  REP(i,210) REP(j,210) dp[i][j] = make_pair(INT_MIN, INT_MAX);
  auto res = rec(0, s.size());

  cout << res.first << endl;
  return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 3920
final_rank: 8

code_snippet: //#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")

#include <random>
#include "bits/stdc++.h"
//#include <atcoder/all>

using namespace std;
// using namespace atcoder;

using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
constexpr ld eps = 1e-12;
constexpr int inf = numeric_limits<int>::max() / 2;
constexpr ll mod = 1e9 + 7;
mt19937_64 rnd{random_device()()};

template <typename T>
void printv(const vector<T> &v) {
  int sz = v.size();
  for (int i = 0; i < sz; i++) {
    cout << v[i] << " \n"[i == sz - 1];
  }
}

string s;
const int MAX_N = 210;
// (min, max)
vector<vector<P>> dp(MAX_N, vector<P>(MAX_N, {inf, -inf}));

P rec(int l, int r) {
  if (dp[l][r] != P(inf, -inf)) {
    return dp[l][r];
  }
  if (l == r) {
    int num = s[l] - '0';
    return dp[l][r] = P(num, num);
  }
  if (s[l] == '(' && r - l >= 3) {
    auto p = rec(l + 1, r);
    dp[l][r].first = min(dp[l][r].first, p.first);
    dp[l][r].second = max(dp[l][r].second, p.second);
  }
  if (s[r] == ')' && r - l >= 3) {
    auto p = rec(l, r - 1);
    dp[l][r].first = min(dp[l][r].first, p.first);
    dp[l][r].second = max(dp[l][r].second, p.second);
  }
  for (int pos = l + 1; pos <= r - 1; pos++) {
    if (s[pos] == '+' || s[pos] == '-') {
      auto p1 = rec(l, pos - 1);
      auto p2 = rec(pos + 1, r);
      if (s[pos] == '+') {
        dp[l][r].first = min(dp[l][r].first, p1.first + p2.first);
        dp[l][r].second = max(dp[l][r].second, p1.second + p2.second);
      } else {
        dp[l][r].first = min(dp[l][r].first, p1.first - p2.second);
        dp[l][r].second = max(dp[l][r].second, p1.second - p2.first);
      }
    }
  }
  return dp[l][r];
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> s;
  int n = s.size();
  cout << rec(0, n - 1).second << endl;
}
accuracy: 1
time_ms: 280
memory_kb: 4172
final_rank: 10

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-4;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
string s; int n;
int m;
int num[200];bool ord[200][2];
char oper[200];
bool valid;
int calc(int le, int ri) {
	valid = true; int res = num[le];
	Rep1(i, le+1, ri) {
		if (oper[i - 1] == '+') {
			res += num[i];
		}
		else {
			res -= num[i];
		}
	}
	Rep1(i, le + 1, ri - 1) {
		if (ord[i][0] || ord[i][1])valid = false;
	}
	if (ri-le>0&&(ord[le][1] || ord[ri][0]))valid = false;
	return res;
}
bool chk[200][200];
P ans[200][200];
P dfs(int le, int ri) {
	if (chk[le][ri])return ans[le][ri];
	chk[le][ri] = true;
	if (le > ri)return ans[le][ri] = { 0,0 };
	P res = { mod,-mod };
	int t = calc(le, ri);
	if (valid)res = { t,t };
	if (ri - le > 0 && (ord[le][1] || ord[ri][0]))return ans[le][ri]=res;
	Rep1(i, le+1,ri) {
		if (ord[i][1])continue;
		Rep1(j, i + 1, ri-1) {
			if (ord[j][0])continue;
			//if (i == le && j == ri)continue;
			P s1 = dfs(le, i - 1),s2=dfs(i,j),s3=dfs(j+1,ri);
			if (s1.first == mod || s2.first == mod || s3.first == mod)continue;
			int mi = s1.first, ma = s1.second;
			if (oper[i - 1] == '+') {
				mi += s2.first; ma += s2.second;
			}
			else {
				mi -= s2.second; ma -= s2.first;
			}
			if (oper[j] == '+') {
				mi += s3.first; ma += s3.second;
			}
			else {
				mi -= s3.second; ma -= s3.first;
			}
			res.first = min(res.first, mi);
			res.second = max(res.second, ma);
		}
	}
	Rep1(i, le + 1, ri - 1) {
		if (ord[i][0])continue;
		P s1 = dfs(le, i), s2 = dfs(i + 1, ri);
		if (s1.first == mod || s2.first == mod)continue;
		int mi = s1.first, ma = s1.second;
		if (oper[i] == '+') {
			mi += s2.first; ma += s2.second;
		}
		else {
			mi -= s2.second; ma -= s2.first;
		}
		res.first = min(res.first, mi);
		res.second = max(res.second, ma);
	}
	Rep1(i, le + 1, ri - 1) {
		if (ord[i][1])continue;
		P s1 = dfs(le, i - 1), s2 = dfs(i, ri);
		if (s1.first == mod || s2.first == mod)continue;
		int mi = s1.first, ma = s1.second;
		if (oper[i - 1] == '+') {
			mi += s2.first; ma += s2.second;
		}
		else {
			mi -= s2.second; ma -= s2.first;
		}
		res.first = min(res.first, mi);
		res.second = max(res.second, ma);
	}
	return ans[le][ri]=res;
}
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin >> s; n = s.length();
	for (int i = 0; i < n; i += 2) {
		while (s[i] == '(') {
			ord[m][0] = true; i++;
		}
		num[m] = s[i] - '0';
		while (i + 1 < n&&s[i + 1] == ')') {
			ord[m][1] = true; i++;
		}
		if (i + 1 < n) {
			oper[m] = s[i + 1];
		}
		m++;
	}
	cout << dfs(0, m - 1).second << endl;
	/*rep(i,m) {
		Rep(j, i, m) {
			cout << i << " " << j << endl;
			cout << ans[i][j].first << " " << ans[i][j].second << endl;

		}
	}
	rep(i, m) {
		cout << num[i] << endl;
	}*/
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3372
final_rank: 4

code_snippet: #include <cstdio>
#include <cstdint>
#include <cassert>
#include <cctype>
#include <string>
#include <vector>
#include <algorithm>

using MinMax = std::pair<int, int>;

constexpr int INF=1<<29;
constexpr MinMax INIT(INF, -INF);

std::vector<std::vector<MinMax>> memo;

MinMax rec(const std::string& s, size_t left, size_t right) {
  MinMax& res=memo[left][right];
  if (res != INIT) return res;

  if (right == left+1) {
    int v=s[left]-'0';
    return (res = {v, v});
  }
  if (s[left] == '(' && right-left > 3) {
    MinMax p=rec(s, left+1, right);
    res.first = std::min(res.first, p.first);
    res.second = std::max(res.second, p.second);
  }
  if (s[right-1] == ')' && right-left > 3) {
    MinMax p=rec(s, left, right-1);
    res.first = std::min(res.first, p.first);
    res.second = std::max(res.second, p.second);
  }
  for (size_t i=left+1; i+1<right; ++i) {
    char op=s[i];
    if (op == '+' || op == '-') {
      MinMax pl=rec(s, left, i);
      MinMax pr=rec(s, i+1, right);
      if (op == '+') {
        res.first = std::min(res.first, pl.first+pr.first);
        res.second = std::max(res.second, pl.second+pr.second);
      } else {
        res.first = std::min(res.first, pl.first-pr.second);
        res.second = std::max(res.second, pl.second-pr.first);
      }
    }
  }
  return res;
}

int main() {
  char buf[256];
  scanf("%s", buf);
  std::string S=buf;

  size_t n=S.length();
  memo.assign(n+1, std::vector<MinMax>(n+1, INIT));
  
  printf("%d\n", rec(S, 0, n).second);
}
accuracy: 1
time_ms: 320
memory_kb: 3000
final_rank: 11

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

#define int long long
using P = pair<int, int>;
const int INF = 1e9;

string s;
vector<vector<P> > dp;

P dfs(int l, int r) {
  if ( dp[l][r] != P(-INF, INF) ) {
    return dp[l][r];    
  }
  if ( l == r ) {
    return P(s[l]-'0', s[l]-'0');    
  }

  P ret = {-INF, INF};
  if ( s[l] == '(' && r-l >= 3 ) {
    P res = dfs(l+1, r);
    ret.first = max(ret.first, res.first);
    ret.second = min(ret.second, res.second);    
  }
  
  if ( s[r] == ')' && r-l >= 3 ) {
    P res = dfs(l, r-1);
    ret.first = max(ret.first, res.first);
    ret.second = min(ret.second, res.second);    
  }
  
  for ( int i = l+1; i < r; i++ ) {
    if ( s[i] == '+' ) {
      P res1 = dfs(l, i-1);
      P res2 = dfs(i+1, r);      
      ret.first = max(ret.first, res1.first+res2.first);
      ret.second = min(ret.second, res1.second+res2.second);      
    }
    if ( s[i] == '-' ) {
      P res1 = dfs(l, i-1);
      P res2 = dfs(i+1, r);      
      ret.first = max(ret.first, res1.first-res2.second);
      ret.second = min(ret.second, res1.second-res2.first);      
    }
  }

  return dp[l][r] = ret;  
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> s;

  int n = s.size();
  dp = vector<vector<P> >(n+1, vector<P>(n+1, P(-INF, INF)));

  cout << dfs(0, n-1).first << endl;  
  
  return 0;
}
accuracy: 1
time_ms: 460
memory_kb: 3676
final_rank: 16

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

typedef pair<int,int> P;
typedef pair<P,int> P1;

map<P1,int> memo;
string s;

int dfs(int l, int r, int flag){
  
  if( memo.count(P1(P(l,r),flag)) ) return memo[P1(P(l,r),flag)];
  
  int L = l, R = r;
  
  int cnt = 0;
  
  for(int i=L;i<=R;i++){
    if( '0' <= s[i] && s[i] <= '9' ) cnt++;
  }
  
  if( cnt == 1 ){
    if( R - L + 1 == 1 ) return memo[P1(P(L,R),flag)] = s[L] - '0';
    else return memo[P1(P(L,R),flag)] = flag == 0 ? -1e9 : 1e9;
  }
  
  while( s[L] == '(' ) L++;
  
  while( s[R] == ')' ) R--;
  
  int res = flag == 0 ? -1e9 : 1e9;
  
  for(int i=L;i<=R;i++){
    
    if( s[i] == '+' ){
      int A = dfs( L, i - 1, flag );
      int B = dfs( i + 1, R, flag );
      if( flag == 0 ) res = max( res, A + B );
      else res = min( res, A + B );
    }
    
    if( s[i] == '-' ){
      int A = dfs( L, i - 1, flag );
      int B = dfs( i + 1, R, !flag );
      if( flag == 0 ) res = max( res, A - B );
      else res = min( res, A - B );
    }
    
  }
  
  return memo[P1(P(l,r),flag)] = res;
}

signed main(){
  
  cin>>s;
  
  cout<<dfs(0,(int)s.size()-1,0)<<endl;
  
  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3936
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef pair<int,int>P;
#define F first
#define S second
int INF=1e9;
string s;
P init=P(INF,-INF);
vector<vector<P> >dp(200,vector<P>(200,init));

P dfs(int l,int r){
	if(l==r)return P(s[l]-'0',s[l]-'0');
	if(dp[l][r]!=init)return dp[l][r];
	P res=init;
	if(s[l]=='('&&r-l>2) res=dfs(l+1,r);
	if(s[r]==')'&&r-l>2) res=dfs(l,r-1);
	for(int i=l+1;i<r;i++){
		if(!(s[i]=='-'||s[i]=='+'))continue;
		P a=dfs(l,i-1);
		P b=dfs(i+1,r);
		if(s[i]=='-'){
			res.F=min(res.F,a.F-b.S);
			res.S=max(res.S,a.S-b.F);
		}
		if(s[i]=='+'){
			res.F=min(res.F,a.F+b.F);
			res.S=max(res.S,a.S+b.S);
		}
	}
	return dp[l][r]=res;
}

main(){
	cin>>s;
	cout<<dfs(0,s.size()-1).S<<endl;
}
accuracy: 1
time_ms: 490
memory_kb: 3352
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef pair<int,int>P;
#define F first
#define S second
int INF=1e9;
string s;
P dp[222][222],init=P(INF,-INF);

P dfs(int l,int r){
	if(l==r)return P(s[l]-'0',s[l]-'0');
	if(dp[l][r]!=init)return dp[l][r];
	P res=init;
	if(s[l]=='('&&r-l>2) res=dfs(l+1,r);
	if(s[r]==')'&&r-l>2) res=dfs(l,r-1);
	for(int i=l+1;i<r;i++){
		if(!(s[i]=='-'||s[i]=='+'))continue;
		P a=dfs(l,i-1);
		P b=dfs(i+1,r);
		if(s[i]=='-'){
			res.F=min(res.F,a.F-b.S);
			res.S=max(res.S,a.S-b.F);
		}
		if(s[i]=='+'){
			res.F=min(res.F,a.F+b.F);
			res.S=max(res.S,a.S+b.S);
		}
	}
	return dp[l][r]=res;
}

int main(){
	cin>>s;
	for(int i=0;i<222;i++)
		for(int j=0;j<222;j++)dp[i][j]=init;
	cout<<dfs(0,s.size()-1).S<<endl;
}
accuracy: 1
time_ms: 470
memory_kb: 3612
final_rank: 17

code_snippet: #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';
  return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'[';
  REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';}
  out<<']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

PII dp[205][205];
signed main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  cin >> s;

  // [l,r]
  function<PII(int,int)> dfs = [&](int l, int r) -> PII {
    if(dp[l][r] != PII{-INF, INF}) return dp[l][r];
    if(l==r) return dp[l][r] = {s[l]-'0', s[l]-'0'};
    if(s[l]=='(' && r-l>=3) {
      PII p = dfs(l+1, r);
      chmax(dp[l][r].first, p.first);
      chmin(dp[l][r].second, p.second);
    }
    if(s[r]==')' && r-l>=3) {
      PII p = dfs(l, r-1);
      chmax(dp[l][r].first, p.first);
      chmin(dp[l][r].second, p.second);
    }
    FOR(i, l+1, r) {
      if(s[i]=='+' || s[i]=='-') {
        PII vl = dfs(l, i-1);
        PII vr = dfs(i+1, r);
        if(s[i]=='+') {
          chmax(dp[l][r].first, vl.first+vr.first);
          chmin(dp[l][r].second, vl.second+vr.second);
        } else {
          chmax(dp[l][r].first, vl.first-vr.second);
          chmin(dp[l][r].second, vl.second-vr.first);
        }
      }
    }
    return dp[l][r];
  };

  REP(i, s.size()) REP(j, s.size()) dp[i][j] = PII{-INF, INF};
  cout << dfs(0, s.size()-1).first << endl;

  // REP(i, s.size()) {
  //   FOR(j, i, s.size()) cout << dp[i][j] << " ";
  //   cout << endl;
  // }

  return 0;
}
accuracy: 1
time_ms: 560
memory_kb: 3792
final_rank: 19

code_snippet: #include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
using namespace std;
typedef pair<int, int> pii;
inline void chmax(int &a, const int &b) {a = max(a, b);}
inline void chmin(int &a, const int &b) {a = min(a, b);}
const int INF = 1 << 28;
string s;
int N;
pii dp[210][210], INIT;

pii solve(int l, int r) {
    pii &res = dp[l][r];
    if(dp[l][r] != INIT) return res;
    // 数字
    if(r - l == 1) {
        int val = s[l] - '0';
        return res = make_pair(val, val);
    }
    // 括弧をとっぱらって再帰的に
    // このとき、括弧含め幅が 3 以下 (つまり実質 2 以下) だと
    // 数式になりえないので飛ばす
    if(s[l] == '(' && r-l > 3) {
        pii tmp = solve(l+1, r);
        res.first = max(res.first, tmp.first);
        res.second = min(res.second, tmp.second);
    }
    if(s[r-1] == ')' && r-l > 3) {
        pii tmp = solve(l, r-1);
        res.first = max(res.first, tmp.first);
        res.second = min(res.second, tmp.second);
    }
    for(int k=l; k<r; k++) {
        if(s[k] != '+' && s[k] != '-') continue;
        pii v1 = solve(l, k), v2 = solve(k+1, r);
        if(s[k] == '+') {
            res.first = max(res.first, v1.first + v2.first);
            res.second = min(res.second, v1.second + v2.second);
        }
        else {
            res.first = max(res.first, v1.first - v2.second);
            res.second = min(res.second, v1.second - v2.first);
        }
    }
    return res;
}

int main() {
    cin >> s;
    N = s.length();
    INIT = make_pair(-INF, INF);
    for(int i=0; i<N; i++) {
        fill(dp[i], dp[i] + N + 1, INIT);
    }

    cout << solve(0, N).first << endl;
    /*
    for(int i=0; i<N; i++) {
        for(int j=i+1; j<=N; j++) {
            if(dp_max[i][j] == -INF) continue;
            printf("%s\n", s.c_str());
            for(int k=0; k<N; k++) {
                if(i <= k && k < j) printf("=");
                else printf(" ");
            }
            printf(" -> %d\n", dp_max[i][j]);
        }
    }
    */
    return 0;
}
accuracy: 1
time_ms: 450
memory_kb: 3524
final_rank: 15

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

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(a) (a).begin(),(a).end()

constexpr int INF = 1000000000;

enum CYK_TABLE {
  DIGIT,
  LPAR,
  RPAR,
  PLUS,
  MINUS,
  TERM,
  RPEXPR,
  RMEXPR,
  LEXPR,
  EXPR
};

int main(){
  int N = 10;
  string s;
  cin>>s;
  int n = s.size();
  vector<vector<vector<bool>>> cyk(n, vector<vector<bool>>(n+1, vector<bool>(N, false)));
  vector<vector<vector<int>>> mn(n, vector<vector<int>>(n+1, vector<int>(N, INF)));
  vector<vector<vector<int>>> mx(n, vector<vector<int>>(n+1, vector<int>(N, -INF)));
  REP(i,n) {
    if (isdigit(s[i])) {
      cyk[i][1][DIGIT] = true;
      mn[i][1][DIGIT] = s[i] - '0';
      mx[i][1][DIGIT] = s[i] - '0';
    } else {
      switch (s[i]) {
        case '(':
        cyk[i][1][LPAR] = true;
        break;
        case ')':
        cyk[i][1][RPAR] = true;
        break;
        case '+':
        cyk[i][1][PLUS] = true;
        break;
        case '-':
        cyk[i][1][MINUS] = true;
        break;
      }
    }
  }
  vector<pair<int, int>> m = {
    {DIGIT, TERM},
    {EXPR, TERM},
    {EXPR, LEXPR},
    {LEXPR, EXPR}
  };
  map<pair<int,int>, int> m2 = {
    {{PLUS, TERM}, RPEXPR},
    {{TERM, RPEXPR}, EXPR},
    {{MINUS, TERM}, RMEXPR},
    {{TERM, RMEXPR}, EXPR},
    {{LPAR, EXPR}, LEXPR},
    {{LEXPR, RPAR}, EXPR}
  };
  for (int l = 1; l <= n; ++l) {
    REP(i,n) {
      if (i+l > n) continue;
      REP(k,l) {
        if (k == 0) continue;
        for (auto p : m2) {
          int left, right, res;
          pair<int,int> tmp;
          tie(tmp, res) = p;
          tie(left, right) = tmp;
          if (cyk[i][k][left] && cyk[i+k][l-k][right]) {
            cyk[i][l][res] = true;
            if (right == RPEXPR) {
              mn[i][l][res] = min(mn[i][l][res],
                  mn[i][k][left] + mn[i+k][l-k][right]);
              mx[i][l][res] = max(mx[i][l][res],
                  mx[i][k][left] + mx[i+k][l-k][right]);
            } else if (right == RMEXPR) {
              mn[i][l][res] = min(mn[i][l][res],
                  mn[i][k][left] - mx[i+k][l-k][right]);
              mx[i][l][res] = max(mx[i][l][res],
                  mx[i][k][left] - mn[i+k][l-k][right]);
            } else if (right == EXPR || right == TERM) {
              mn[i][l][res] = min(mn[i][l][res], mn[i+k][l-k][right]);
              mx[i][l][res] = max(mx[i][l][res], mx[i+k][l-k][right]);
            } else if (left == LEXPR) {
              mn[i][l][res] = min(mn[i][l][res], mn[i][k][left]);
              mx[i][l][res] = max(mx[i][l][res], mx[i][k][left]);
            }
          }
        }
      }
      while (true) {
      bool update = false;
        for (auto p : m) {
          int src, res;
          tie(src, res) = p;
          if (cyk[i][l][src]) {
            if (!cyk[i][l][res]) update = true;
            cyk[i][l][res] = true;
            mn[i][l][res] = min(mn[i][l][res], mn[i][l][src]);
            mx[i][l][res] = max(mx[i][l][res], mx[i][l][src]);
          }
        }
        if (!update) break;
      }
    }
  }
  cout << mx[0][n][EXPR] << endl;
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 11608
final_rank: 7

code_snippet: #include "bits/stdc++.h"
using namespace std;
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2710&lang=jp
// reference : http://suikaba.hatenablog.com/entry/2017/05/24/231631
typedef pair<int, int> pii;
#define INF 1<<25

pii rec(int l, int r, const string& S,vector<vector<pii>>& dp) {
	pii& ret = dp[l][r];
	/* Updated */
	if (ret != pii{ -INF, INF }) return ret;

	if (l == r) {
		return ret = { S[l] - '0',S[l] - '0' };
	}

	if (S[l] == '(' && r - l >= 3) {
		pii x = rec(l + 1, r, S, dp);
		ret.first = max(ret.first, x.first);
		ret.second = min(ret.second, x.second);
	}
	if (S[r] == ')' && r - l >= 3) {
		pii x = rec(l, r - 1, S, dp);
		ret.first = max(ret.first, x.first);
		ret.second = min(ret.second, x.second);
	}
	for (int m = l + 1; m < r; m++) {
		if (S[m] == '+' || S[m] == '-') {
			pii x1 = rec(l, m - 1, S, dp);
			pii x2 = rec(m + 1, r, S, dp);
			if (S[m] == '+') {
				ret.first = max(ret.first, x1.first + x2.first);
				ret.second = min(ret.second, x1.second + x2.second);
			}
			else {
				ret.first = max(ret.first, x1.first - x2.second);
				ret.second = min(ret.second, x1.second - x2.first);
			}
		}
	}
	return ret;
}

int main(void) {
	cin.tie(0); ios::sync_with_stdio(false);
	string S; cin >> S;
	/*  dp[l][r] := { maximum value of the interval(l-r) , minimum value of ~ }*/
	vector<vector<pii>> dp(S.length(), vector<pii>(S.length(), { -INF,INF }));
	cout << rec(0, S.length() - 1, S, dp).first << endl;
}
accuracy: 1
time_ms: 350
memory_kb: 3320
final_rank: 13

code_snippet: #include <iostream>
#include <cmath>
#define REP(i, a, n) for(int i = ((int) a); i < ((int) n); i++)
#define INF 100000000
using namespace std;
typedef long long ll;

string S;
ll dp[300][300][2];

ll number(int l, int r) {
  ll ret = 0;
  REP(i, l, r) {
    if('0' <= S[i] && S[i] <= '9') ret = ret * 10 + (S[i] - '0');
    else return -1;
  }
  return ret;
}

ll dfs(int l, int r, int mode, int depth) {

  if(dp[l][r][mode] > -INF * 2) return dp[l][r][mode];

  ll n = number(l, r);
  if(n >= 0) return n;

  ll ret = mode == 0 ? -INF : INF;
  REP(i, l, r) if(S[i] == '+' || S[i] == '-') {
    int nl = l, nr = r;
    while(1) {
      ll n1 = dfs(nl, i, mode, depth + 1);
      ll n2 = dfs(i + 1, nr, S[i] == '-' ? 1 - mode : mode, depth + 1);
      if(abs(n1) != INF && abs(n2) != INF) {
        if(mode == 0) {
          if(S[i] == '+') ret = max(ret, n1 + n2);
          if(S[i] == '-') ret = max(ret, n1 - n2);
        } else {
          if(S[i] == '+') ret = min(ret, n1 + n2);
          if(S[i] == '-') ret = min(ret, n1 - n2);
        }
      }
      if(S[nl] != '(' && S[nr - 1] != ')') break;
      if(S[nl] == '(') nl++;
      if(S[nr - 1] == ')') nr--;
    }
  }
  return dp[l][r][mode] = ret;
}

int main(void) {
  cin >> S;

  REP(i, 0, 201) REP(j, 0, 201) REP(k, 0, 2) dp[i][j][k] = -INF * 2;
  cout << dfs(0, S.size(), 0, 0) << endl;

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4108
final_rank: 1

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

using P = pair<int, int>;

constexpr int INF = 1e9;

P memo[201][201];

P rec(int l, int r, string const& s) {
    P& res = memo[l][r];
    if(res != P{-INF, INF}) {
        return res;
    }
    if(l == r) {
        int v = s[l] - '0';
        return res = make_pair(v, v);
    }
    if(s[l] == '(' && r-l >= 3) {
        P p = rec(l+1, r, s);
        res.first = max(res.first, p.first);
        res.second = min(res.second, p.second);
    }
    if(s[r] == ')' && r-l >= 3) {
        P p = rec(l, r-1, s);
        res.first = max(res.first, p.first);
        res.second = min(res.second, p.second);
    }
    for(int i=l+1; i<r; ++i) {
        char op = s[i];
        if(op == '+' || op == '-') {
            P rl = rec(l, i-1, s);
            P rr = rec(i+1, r, s);
            if(op == '+') {
                res.first = max(res.first, rl.first + rr.first);
                res.second = min(res.second, rl.second + rr.second);
            } else {
                res.first = max(res.first, rl.first - rr.second);
                res.second = min(res.second, rl.second - rr.first);
            }
        }
    }
    return res;
}

int main() {
    string s;
    cin >> s;
    for(int i=0; i<s.size(); ++i) {
        for(int j=0; j<s.size(); ++j) {
            memo[i][j] = make_pair(-INF, INF);
        }
    }
    cout << rec(0, s.size()-1, s).first << endl;
}
accuracy: 1
time_ms: 370
memory_kb: 3504
final_rank: 14

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

int calc(string &s,int &i){
  int res=0;
  while(1){
    if(isdigit(s[i]))res=s[i++]-'0';
    else if(s[i]=='('){res=calc(s,++i),i++;}
    else if(s[i]=='+')res+=calc(s,++i);
    else if(s[i]=='-')res-=calc(s,++i);
    else break;
  }
  return res;
}

unordered_map<string,int> mem[2];
int dfs(string s,int f){
  if(mem[f].count(s))return mem[f][s];

  int j=0,res=calc(s,j),n=s.size(),d=-1;
  for(int i=0;i<n;i++) d+=(s[i]=='+'||s[i]=='-');
  
  for(int i=0,c=0,cnt=0;i<n;i++,cnt++,d--){
    while(1){
      while(s[i]=='('||s[i]==')')c+=(s[i]=='(')-(s[i]==')'),i++;
      if(i>=n||s[i]=='+'||s[i]=='-') break;
      i++;
    }
    if(i>=n||(cnt&&s[i-2]=='(')||(d&&s[i+2]==')'))continue;
    string a=s.substr(0,i),b=s.substr(i+1,n-i-1);
    int t=c,ch=s[i];
    while(t--)a+=')',b='('+b;
    int mxa=dfs(a,0),mna=dfs(a,1);
    int mxb=dfs(b,0),mnb=dfs(b,1);
    if(f==0&&ch=='+') res=max(res, mxa+mxb);
    if(f==0&&ch=='-') res=max(res, mxa-mnb);
    if(f==1&&ch=='+') res=min(res, mna+mnb);
    if(f==1&&ch=='-') res=min(res, mna-mxb);
  }
  return mem[f][s]=res;
}

int main(){
  string S;
  cin>>S;
  cout <<dfs(S,0)<<endl;
  return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 4444
final_rank: 9