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];elsesum2[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;
}

转载于:https://www.cnblogs.com/qscqesze/p/5236749.html

Educational Codeforces Round 9 B. Alice, Bob, Two Teams 前缀和相关推荐

  1. Educational Codeforces Round #136 (Rated for Div. 2) A~C

    A. Immobile Knight 题意: 给定一个 n×m 的棋盘,棋盘上有一个 "马",可以在一个方向上移动两个单元格,在垂直方向上移动一个单元格. 问:将其放在哪个位置,才 ...

  2. Educational Codeforces Round 138 (Rated for Div. 2) A~D

    比赛链接:Dashboard - Educational Codeforces Round 138 (Rated for Div. 2) - Codeforces 目录 A. Cowardly Roo ...

  3. Educational Codeforces Round 112(Div.2) ABC题解

    D题好像可以做一做,挖个坑以后做好了来填(doge Educational Codeforces Round 112(Div.2) 题目列表 1.A 2.B 3.C 1.A 原题链接 题目大意 有三种 ...

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

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

  5. Educational Codeforces Round 114 (Rated for Div. 2) (A ~ F)全题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Educational Codeforces Round 114 (Rated for Div. 2) ...

  6. Educational Codeforces Round 106 (Rated for Div. 2)(A ~ E)题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 Educational Codeforces Round 106 (Rated for Div. ...

  7. Educational Codeforces Round 32

    http://codeforces.com/contest/888 A Local Extrema[水] [题意]:计算极值点个数 [分析]:除了第一个最后一个外,遇到极值点ans++,包括极大和极小 ...

  8. Educational Codeforces Round 37 (Rated for Div. 2) 1

    Educational Codeforces Round 37 (Rated for Div. 2) A.Water The Garden 题意:Max想给花园浇水.花园可被视为长度为n的花园床,花园 ...

  9. Educational Codeforces Round 90 (Rated for Div. 2)(A, B, C, D, E)

    Educational Codeforces Round 90 (Rated for Div. 2) Donut Shops 思路 分三种情况: a==c/ba == c / ba==c/b这个时候两 ...

最新文章

  1. 先进一站式IP及定制
  2. AI一分钟 |“最抢手”毕业生排名出炉:清华没进前三?支付宝将支持iPhone X刷脸
  3. 6425C-Lab11 配置DNS
  4. cordova最基本的热更新
  5. C++中的转换构造函数和类型转换函数
  6. java代码执行mysql语句_三种执行SQL语句的的JAVA代码
  7. [转贴]ASP优化之显示数据查询内容
  8. Unity中使用Attribute
  9. RabbitMQ 延迟队列,消息延迟推送
  10. 用 Go 编写一个简单的 WebSocket 推送服务
  11. Opencv3 形态学操作
  12. 强制浏览器重定向到另一页
  13. Linux workqueue疑问【转】
  14. 计算机中的进制位运算
  15. 阿里Java开发手册思考(二)
  16. Unity Shader (二)
  17. 关于ActionForm的一些问题
  18. Supercell:靠两款手游如何做到30亿美金市值?
  19. Nginx+obs直播服务器,推流连接失败
  20. python plt pyplot matplotlib绘图时形状异常

热门文章

  1. python求字符组合_python – 生成一个潜在的8个字符串的所有可能的2个字符组合?...
  2. datasource dbcp 数据源_数据源--.dbcp.BasicDataSource 使用
  3. jira怎么提交bug_请停止编写糟糕的提交消息!
  4. c ++ 打印二进制_C / C ++中的二进制搜索树
  5. coalesce函数_什么是SQL Server COALESCE()函数?
  6. 具有IDE或IDE插件的Spring Boot Initilizr
  7. 在ubuntu中搭建guacamole
  8. mybatis基础,mybatis核心配置文件properties元素
  9. win7 nodejs找不到express命令
  10. 供应商与客户 连接平台 的谋合