## Polygon

Polygon

### 解

$if(+),f[0][l][r]=\min_{k=l}^{r-1}(f[0][l][k]+f[0][k+1][r])$
$if(\times),f[0][l][r]=\min_{k=l}^{r-1}\min_{p=q=0}^1(f[p][l][k]\times f[q][k+1][r])$

$if(+),f[1][l][r]=\max_{k=l}^{r-1}(f[1][l][k]+f[1][k+1][r])$
$if(\times),f[1][l][r]=\max_{k=l}^{r-1}\max_{p=q=0}^1(f[p][l][k]\times f[q][k+1][r])$

#include <iostream>
#include <cstdio>
#include <cstring>
#define il inline
#define ri register
#define ll long long
#define llmax 1e16
using namespace std;
char c[101];
ll dp[2][101][101];
int stack[101],st;
il void get(char&);
template<class free>il free Max(free,free);
template<class free>il free Min(free,free);
int main(){
int n,n2;scanf("%d",&n),n2=n<<1;
memset(dp[0],66,sizeof(dp[0]));
memset(dp[1],-66,sizeof(dp[1]));
for(int i(1);i<=n;++i)
get(c[i]),scanf("%lld",&dp[0][i][i]),
c[i+n]=c[i],dp[1][i][i]=dp[1][i+n][i+n]
=dp[0][i+n][i+n]=dp[0][i][i];
for(int i,j(1),k,p,q;j<=n2;++j)
for(i=j-1;i;--i)
for(k=i;k<j;++k)
if(c[k+1]=='t')
dp[0][i][j]=Min(dp[0][i][j],dp[0][i][k]+dp[0][k+1][j]),
dp[1][i][j]=Max(dp[1][i][j],dp[1][i][k]+dp[1][k+1][j]);
else for(p=0;p<2;++p)
for(q=0;q<2;++q)
dp[0][i][j]=Min(dp[0][i][j],dp[p][i][k]*dp[q][k+1][j]),
dp[1][i][j]=Max(dp[1][i][j],dp[p][i][k]*dp[q][k+1][j]);
ll ans(-llmax);
for(ri int i(1);i<=n;++i)
if(dp[1][i][i+n-1]>ans)
ans=dp[1][i][i+n-1],stack[st=1]=i;
else if(dp[1][i][i+n-1]==ans)
stack[++st]=i;
printf("%lld\n",ans);
for(int i(1);i<=st;++i)
printf("%d ",stack[i]);
return 0;
}
template<class free>
il free Max(free a,free b){
return a>b?a:b;
}
template<class free>
il free Min(free a,free b){
return a<b?a:b;
}
il void get(char&c){
while(c=getchar(),c==' '||c=='\r'||c=='\n');
}

dfs实现

#include <iostream>
#include <cstdio>
#define il inline
#define ri register
#define ll long long
#define llmax 1e16
#define lsy akioi tql
using namespace std;
char c[101];
int stack[101],st;
ll dp[2][101][101];
bool deal[2][101][101];
il void get(char&);
il ll dfs(int,int,int);
template<class free>il free comp(int,free,free);
int main(){
int n,i;scanf("%d",&n);
for(i=1;i<=n;++i)
get(c[i]),c[i+n]=c[i],
scanf("%lld",&dp[0][i][i]),
dp[1][i][i]=dp[0][i+n][i+n]
=dp[1][i+n][i+n]=dp[0][i][i],
deal[0][i][i]=deal[1][i][i]=
deal[0][i+n][i+n]=deal[1][i+n][i+n]=true;
ll ans(-llmax);
for(int i(1);i<=n;++i)
if(ans<dfs(1,i,i+n-1))
ans=dfs(1,i,i+n-1),stack[st=1]=i;
else if(ans==dfs(1,i,i+n-1))stack[++st]=i;
printf("%lld\n",ans);for(int i(1);i<=st;++i)printf("%d ",stack[i]);
return 0;
}
template<class free>
il free comp(int p,free a,free b){
return (p^(a<b))?a:b;
}
il ll dfs(int p,int l,int r){
if(deal[p][l][r])return dp[p][l][r];
dp[p][l][r]=p?-llmax:llmax,deal[p][l][r]|=true;
for(int k(l),a,b;k<r;++k)
if(c[k+1]=='t')
dp[p][l][r]=comp(p,dp[p][l][r],dfs(p,l,k)+dfs(p,k+1,r));
else for(a=0;a<2;++a)
for(b=0;b<2;++b)
dp[p][l][r]=comp(p,dp[p][l][r],dfs(a,l,k)*dfs(b,k+1,r));
return dp[p][l][r];
}
il void get(char &c){
while(c=getchar(),c==' '||c=='\n'||c=='\r');
}