B. Alice, Bob, Two Teams

题目连接:

http://www.codeforces.com/contest/632/problem/B

Description

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.

Sample Input

5

1 2 3 4 5

ABABA

Sample Output

11

Hint

题意

有n个物品,每个物品有一个分值

然后每个物品有一个标牌,如果标牌上是A,那么就属于A,是B,那么就属于B

现在你可以选择一个前缀或者后缀,然后将上面的A改成B,将B改成A

然后问你最多B能够得到多少

题解:

暴力维护前缀和就好了

维护A的前缀和,和B的前缀和

翻转其实就是把A的变成B,B的变成A而已

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5+7;
int n;
long long p[maxn];
char s[maxn];
long long sum1[maxn];
long long sum2[maxn];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lld",&p[i]);
scanf("%s",s+1);
for(int i=1;i<=n;i++)
{
sum1[i]=sum1[i-1];
sum2[i]=sum2[i-1];
if(s[i]=='A')
sum1[i]+=p[i];
else
sum2[i]+=p[i];
}
long long ans = max(sum2[n],sum1[n]);
for(int i=1;i<=n;i++)
{
ans = max(ans,sum2[n]-sum2[i]+sum1[i]);
ans = max(ans,sum2[i]+sum1[n]-sum1[i]);
}
cout<<ans<<endl;
}

Educational Codeforces Round 9 B. Alice, Bob, Two Teams 前缀和的更多相关文章

  1. Educational Codeforces Round 129 (Rated for Div. 2) A-D

    Educational Codeforces Round 129 (Rated for Div. 2) A-D A 题目 https://codeforces.com/contest/1681/pro ...

  2. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  3. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  4. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  5. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  6. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  7. Educational Codeforces Round 6 C. Pearls in a Row

    Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...

  8. Educational Codeforces Round 9

    Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...

  9. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

随机推荐

  1. qgis 插件开发

    qgis 插件开发 http://blog.csdn.net/v6543210/article/details/40480341

  2. 【数位dp入门】【HDU4734】F(x)

    记录减的状态,表示还要凑多少才能达到当前值. 然后进行枚举即可.注意状态数不能重复. #include<bits/stdc++.h> #define N 10010 using names ...

  3. VPS性能测试(2):内存大小、交换空间、高速缓存、实际使用内存

    1.要想查看购买的VPS主机的内存信息,执行:cat /proc/meminfo,主要是看内存大小.交换空间.高速缓存 2.VPS主机实际使用内存大小.Linux管理内存的机制是这样的:无论物理内存有 ...

  4. 【java报错】Unknown character set index for field '224' received from server.

    在捣腾免费数据库时,使用的一个数据库提供商的服务器使用utf8mb4编码,而我的jar包还是八百年前的.然后...然后就报错了... (1) MYSQL 5.5 之前, UTF8 编码只支持1-3个字 ...

  5. 如何用jQuery获得radio的值

    如何获得radio的值,在网上查了一下,下面总结几种解决方法,. 1.获取选中值: $('input:radio:checked').val(): $("input[type='radio' ...

  6. Centos7 配置网络

    /* Centos7 的网络 不可以用ifconfig获取,需要安装包 所以 .*/ //查看ip [root@master ~]# ip a /* Centos7 的网卡名字与 Centos6有区别 ...

  7. python中global变量释疑

    疑问 为什么main中不能写global x语句? 在函数中如何修改global变量? 在main中修改了global变量后,在子进程中为什么没有效果? 如何利用进程池的initializer参数(函 ...

  8. Go语言标准包之json编码

    标准的就简单通用. package main import ( "encoding/json" "fmt" "log" ) func mai ...

  9. Pycharm5注册方式 @LYRE}}(T1[DD[@81IZDU$A

    0x1 ,安装 0x2 , 调整时间到2038年. 0x3 ,申请30天试用 0x4, 退出pycharm 0x5, 时间调整回来. 注册方法2:    在 注册时选择 License server ...

  10. php详解和优化

    nginx结合php使用FastCGI方式 apache结合php,php是作为一个模块加载到apache中 (1)FastCGI工作原理 1.用户发送http请求报文给nginx服务器 2.ngin ...