题目链接:点击打开链接
 
B. Alice, Bob, Two Teams
 
 
time limit per test
 
 
memory limit per test
 
 
input
 
 
output
 
n pieces, and the i-th piece has a strength pi.
The way to split up game pieces is split into several steps:
- AandB. This can be seen as writing the assignment of teams of a piece in anncharacter string, where each character isAorB.
- AtoBandBtoA). He can do this step at most once.
- Aand Bob will get all the pieces markedB.
The strength of a player is then the sum of strengths of the pieces in the group.
Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve.
 
Input
 
 
n (1 ≤ n ≤ 5·105) — the number of game pieces.
n integers pi (1 ≤ pi ≤ 109) — the strength of the i-th piece.
n characters A or B
 
Output
 
 
a
 
Examples
 
 
input
 
   
5 1 2 3 4 5 ABABA
 
  
output
 
   
11
 
  
input
 
   
5 1 2 3 4 5 AAAAA
 
  
output
 
   
15
 
  
input
 
   
1 1 B
 
  
output
 
   
1
 
Note
 
 
In the first sample Bob should flip the suffix of length one.
5.
In the third sample Bob should do nothing.
 
 
题意:给定n个数和一个字符串,每个数对应一个字符,A表示属于Alice,B表示属于Bob。Bob可以选出字符串的前缀 或者 一个后缀来翻转,A -> B, B -> A,只能使用一次。问Bob可以获得的最大值。(所有属于Bob的数之和)。
思路:维护每一位的前缀和 和 后缀和,最后再是不翻转的情况。
#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
const int MAXN=5e5+10;
int n;
LL p[MAXN];
char str[MAXN];
LL suma[MAXN],sumb[MAXN];
int main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
scanf("%I64d",&p[i]);
scanf("%s",str+1);
memset(suma,0,sizeof(suma));
memset(sumb,0,sizeof(sumb));
for(int i=1;i<=n;i++)
{
suma[i]=suma[i-1];
sumb[i]=sumb[i-1];
if(str[i]=='A')
suma[i]+=p[i];
else
sumb[i]+=p[i];
}
LL ans=max(suma[n],sumb[n]);
for(int i=1;i<=n;i++)
{
ans=max(ans,suma[n]-suma[i]+sumb[i]);
ans=max(ans,sumb[n]-sumb[i]+suma[i]);
}
printf("%I64d\n",ans);
}
return 0;
}










