区间DP
拆链成环,然后区间DP即可,注意初始化,以及每个地方都要遍历到
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define inf 0x3f3f
using namespace std;
int num[205], dp[205][205][2], n;
char opt[205];
int main() {
cin >> n;
for(int i = 1; i <= 2 * n; i++) {
for(int j = 1; j <= 2 * n; j++)
dp[i][j][1] = -inf, dp[i][j][0] = inf;
}
for(int i = 1; i <= n; i++) {
scanf(" %c ", &opt[i]);
opt[i + n] = opt[i];
cin >> num[i];
num[i + n] = num[i];
dp[i][i][0] = dp[i][i][1] = num[i];
dp[i + n][i + n][0] = dp[i + n][i + n][1] = num[i];
}
for(int k = 2; k <= n; k++) {
for(int i = 1; i + k - 1 <= 2 * n; i++) {
int j = i + k - 1;
for(int t = i; t < j; t++) {
if(opt[t + 1] == 'x') {
dp[i][j][1] = max(dp[i][j][1], dp[i][t][1] * dp[t + 1][j][1]);
dp[i][j][1] = max(dp[i][j][1], dp[i][t][0] * dp[t + 1][j][0]);
dp[i][j][0] = min(dp[i][j][0], dp[i][t][0] * dp[t + 1][j][0]);
dp[i][j][0] = min(dp[i][j][0], dp[i][t][1] * dp[t + 1][j][0]);
dp[i][j][0] = min(dp[i][j][0], dp[i][t][0] * dp[t + 1][j][1]);
}else {
dp[i][j][1] = max(dp[i][j][1], dp[i][t][1] + dp[t + 1][j][1]);
dp[i][j][0] = min(dp[i][j][0], dp[i][t][0] + dp[t + 1][j][0]);
}
}
}
}
int ans = 0;
/*for(int k = 1; k <= n; k++) {
for(int i = 1; i + k - 1 <= 2 * n; i++) {
int j = i + k - 1;
printf("%d ", dp[i][j][1]);
}
printf("\n");
}
printf("\n");*/
for(int i = 1; i <= n; i++) {
ans = max(ans, dp[i][i + n - 1][1]);
}
cout << ans << endl;
for(int i = 1; i <= n; i++) {
if(ans == dp[i][i + n - 1][1]) cout << i << " ";
}
cout << endl;
return 0;
}