Alice, Bob, Two Teams CodeForces - 632B

Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi.

The way to split up game pieces is split into several steps:

First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B.
Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once.
Alice will get all the pieces marked A and Bob will get all the pieces marked B.
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
The first line contains integer n (1 ≤ n ≤ 5·105) — the number of game pieces.

The second line contains n integers pi (1 ≤ pi ≤ 109) — the strength of the i-th piece.

The third line contains n characters A or B — the assignment of teams after the first step (after Alice’s step).

Output
Print the only integer a — the maximum strength Bob can achieve.

Example
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

题解:
字符串内的A,B可以从前或从后开始改变任意长度的前缀或后缀(A->B,B->A).使最终的B的和最大。
只需要对不同的情况暴力找出最优解就行了。

代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 500005
using namespace std;
typedef long long ll;  //刚开始用的是int,结果错了,一想原来超限了。
ll a[maxn];    //存输入的每个对应的值。
char s[maxn];    //存A,B字符串。
ll sumA[maxn]={0};  //存一段字符串中A对应的值之和。
ll sumB[maxn]={0};   //存一段字符串中B对应值之和。

int main()
{
    ll n;
    while(~scanf("%lld",&n)){
        for(ll i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        scanf("%s",s+1);
        for(ll i=1;i<=n;i++){   //统计每一段字符串中A,B对应的值。
            sumA[i]=sumA[i-1];
            sumB[i]=sumB[i-1];
            if(s[i]=='A'){
                sumA[i]=sumA[i]+a[i];
            }else{
                sumB[i]=sumB[i]+a[i];
            }
        }
        ll ans=max(sumA[n],sumB[n]);  //记录最大值。
        for(ll i=1;i<=n;i++){  
            ans=max(ans,sumB[n]-sumB[i]+sumA[i]);
            ans=max(ans,sumA[n]-sumA[i]+sumB[i]);

        }
        printf("%lld\n",ans);


    }

    return 0;
}

其中ans=max(ans,sumB[n]-sumB[i]+sumA[i]);统计前缀变时的值,不断更新最大值。
其中ans=max(ans,sumA[n]-sumA[i]+sumB[i]);统计后缀变时的值,不断更新最大值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值