很容易想到枚举第一步切掉的边,然后再计算能够产生的最大值。
联想到区间DP,令dp[i][l][r]为第一步切掉第i条边后从第i个顶点起区间[l,r]能够生成的最大值是多少。
但是状态不好转移,因为操作的符号不仅有‘+’,还有‘*’,加法的话,父区间的最大值显然可以从子区间的最大值相加得出。
乘法的话,父区间的最大值除了由子区间的最大值相乘得出,还可以由子区间的最小值相乘得出。
所以,多定义一维状态。 dp[i][l][r][flag]表示第一步切掉第i条边后从第i个顶点起区间[l,r]能够生成的最大值/最小值是多少?
转移的话很简单
dp[i][l][r][0]=min(dp[i][l][k][0]+dp[i][k+1][r][0])(操作符为+),min(dp[i][l][k][0]*dp[i][k+1][r][1], dp[i][l][k][1]*dp[i][k+1][r][0])(操作符为*).
dp[i][l][r][1]=max(dp[i][l][k][1]+dp[i][k+1][r][1])(操作符为+),min(dp[i][l][k][0]*dp[i][k+1][r][0], dp[i][l][k][1]*dp[i][k+1][r][1])(操作符为*).
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int res=, flag=;
char ch;
if((ch=getchar())=='-') flag=;
else if(ch>=''&&ch<='') res=ch-'';
while((ch=getchar())>=''&&ch<='') res=res*+(ch-'');
return flag?-res:res;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... int ans[N], num[N], dp[N][N][N][], n;
char s[N][];
bool vis[N][N][N][]; int dfs(int x, int l, int r, int flag)
{
if (vis[x][l][r][flag]) return dp[x][l][r][flag];
vis[x][l][r][flag]=;
if (l==r) return dp[x][l][r][flag]=num[(x+l-)%n+];
if (flag) {
int ans=-INF;
FO(i,l,r) {
if (s[(x+i-)%n+][]=='t') ans=max(ans,dfs(x,l,i,)+dfs(x,i+,r,));
else ans=max(ans,max(dfs(x,l,i,)*dfs(x,i+,r,),dfs(x,l,i,)*dfs(x,i+,r,)));
}
return dp[x][l][r][flag]=ans;
}
else {
int ans=INF;
FO(i,l,r) {
if (s[(x+i-)%n+][]=='t') ans=min(ans,dfs(x,l,i,)+dfs(x,i+,r,));
else ans=min(ans,min(dfs(x,l,i,)*dfs(x,i+,r,),dfs(x,l,i,)*dfs(x,i+,r,)));
}
return dp[x][l][r][flag]=ans;
}
}
int main ()
{
int ma=-INF, flag=;
scanf("%d",&n);
FOR(i,,n) scanf("%s%d",s[i],num+i);
FOR(i,,n) ma=max(ma,dfs(i,,n,));
printf("%d\n",ma);
FOR(i,,n) if (dp[i][][n][]==ma) printf(flag?"%d":" %d",i), flag=;
putchar('\n');
return ;
}