题干:

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:

  1. 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.
  2. 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.
  3. 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.

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.

In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5.

In the third sample Bob should do nothing.

题目大意:

A和B两个人玩游戏,给n个数,每个数对应一个 ' A ' 或 ' B ' , 现在B玩家可以选一个前缀或者一个后缀,把A变B,B变A,操作结束后,B玩家可以取走所有标号为 ' B ' 的数字,问B玩家可以取到的数字之和最大是多少?

解题报告:

看题目名称,猜是博弈?然而让你失望了,这题和A没有任何卵关系,,,先预处理出前缀A和B,后缀A和B,这四个数组。然后枚举每一个元素,维护翻这一位或者不翻这一位的最大值maxx,并且维护和ans的最大值,输出ans即可。

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 5e5 + 5;
ll val[MAX];
ll a[MAX],b[MAX],A[MAX],B[MAX];
ll ans,maxx;
char s[MAX];
int main()
{int n;cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",val+i);cin>>s+1;//prefix预处理 for(int i = 1; i<=n; i++) {if(s[i] == 'A') {a[i] = a[i-1] + val[i];b[i] = b[i-1];}else {a[i] = a[i-1];b[i] = b[i-1] + val[i];}}//suffix预处理for(int i = n; i>=1; i--) {if(s[i] == 'A') {A[i] = A[i+1] + val[i];B[i] = B[i+1];}else {A[i] = A[i+1];B[i] = B[i+1] + val[i];}} for(int i = 1; i<=n; i++) {maxx = max(b[i]+B[i+1],a[i] + B[i+1]);ans = max(ans,maxx);}for(int i = n; i>=1; i--) {maxx = max(b[i] + B[i+1],b[i] + A[i+1]);ans = max(ans,maxx);}printf("%lld\n",ans);return 0 ;
}

【CodeForces - 632B】Alice, Bob, Two Teams (预处理,思维,前缀和后缀和)相关推荐

  1. Educational Codeforces Round 9 B. Alice, Bob, Two Teams 前缀和

    B. Alice, Bob, Two Teams 题目连接: http://www.codeforces.com/contest/632/problem/B Description Alice and ...

  2. Alice, Bob, Oranges and Apples CodeForces - 586E

    E - Alice, Bob, Oranges and Apples CodeForces - 586E 自己想的时候模拟了一下各个结果 感觉是不是会跟橘子苹果之间的比例有什么关系 搜题解的时候发现了 ...

  3. CodeForces-1016C Vasya And The Mushrooms(模拟+思维+前缀和的前缀和) 解题报告 Apare_xzc

    CodeForces-1016C Vasya And The Mushrooms(模拟+思维+二重前缀和 ) 解题报告 xzc 2019/4/7 这周周赛的C题:wyt学姐的恶意   这道题周赛的时候 ...

  4. codeforces #274 C. Riding in a Lift dp+前缀和优化

    codeforces #274  C. Riding in a Lift   dp+前缀和优化 Imagine that you are in a building that has exactly  ...

  5. [codeforces 1341B] Nastya and Door 神奇的掐头去尾+前缀和

    Codeforces Round #637 (Div. 2) - Thanks, Ivan Belonogov!   比赛人数15390 [codeforces 1341B]    Nastya an ...

  6. CodeForces - 346A Alice and Bob(数论+博弈)

    题目链接:点击查看 题目大意:初始时给出n个数组成的集合,现在要求爱丽丝和鲍勃两人轮流按照规则操作,无法操作的一方即为输,本游戏的规则就是,在集合中任意选择两个数x和y,计算(x-y)的绝对值,若该绝 ...

  7. 【CodeForces - 798D】Mike and distribution (思维构造,贪心,黑科技)

    题干: Mike has always been thinking about the harshness of social inequality. He's so obsessed with it ...

  8. 密码学中常用的人物角色名称--Alice, Bob,etc

    密码学中常用的人物角色名称 转载至 (https://blog.51cto.com/manual/788667) 爱丽丝(Alice)与鲍伯(Bob)是广泛地代入密码学和物理学领域的通用角色.除了爱丽 ...

  9. 枚举 ---- D. Zigzags[ Educational Codeforces Round 94 (Rated for Div. 2)]思维枚举优化4重循环

    D. Zigzags 题目大意:就是给你i<j<k<l并且aj=al&&ai=aki<j<k<l并且a_j=a_l \&\& a_i ...

最新文章

  1. ORB_SLAM2中Tracking线程的三种追踪方式
  2. Oracle宣布提供新的Java支持价格体系
  3. 网站快照更新不及时有什么好的解决办法吗?
  4. Python之web开发(二):python使用django框架搭建网站之新建文件
  5. 【采访】腾讯社交广告高校算法大赛决赛第二周周冠军——nju_newbiew比赛经验及心得分享
  6. Windows 8 DirectX 开发学习笔记(十六)使用Terragen生成自然环境贴图
  7. 从Mysql数据库中导入导出表结构
  8. 在一个centos6上安装多个不同版本python
  9. 【优化求解】基于matalb改进的遗传算法(GA+IGA)求解城市交通信号优化问题【含Matlab源码 213期】
  10. 未来5-10年计算机视觉发展趋势
  11. 直通车的计算机设备跟移动设备的区别,电脑端直通车和手淘直通车的有什么不同?...
  12. 简单功能强大的jQuery在线图片裁剪插件croppic
  13. ogg与wav格式转换
  14. 【无人机 学习笔记 2】无人机导航制导与控制
  15. Rabbitmq交换机详解
  16. Linux之文件/目录搜索
  17. MathJax:微积分常用符号
  18. 18-4-16《Population-based VS. Single-solution Heuristics for the Travelling Thief Problem》2-opt,TTP
  19. 【亲测可用】经验分享,开发在线考试平台(类似问卷星、考试星、阿里巴巴在线考试、易考通、考试云、移动网大、移动网校、中石化网络学院、宝武微学院、iTEST等)如何用程序代码防止学生作弊?
  20. 连锁加盟与直营连锁有什么区别?

热门文章

  1. 深度学习03-CNN 应用
  2. 给定两个整数m和n,求出m~n这段连续的整数中所有偶数的平方和以及所有奇数的立方和。
  3. python聚类分析结果很差_python中的聚类分析:value错误:x和y的大小必须相同
  4. 堆排序python代码实现_python实现堆排序
  5. 用python画六瓣雪花_python-turtle-画雪花-2种方法及效果的详解
  6. mips汇编计算开方_读美国伊利诺伊理工大学计算机科学硕士能学到什么?
  7. 1到n阶乘算法的改进
  8. 画王八java代码参数_java画乌龟源代码-郭遥航.doc
  9. export function函数传参_从底层看前端(七)—— JavaScript到底有多少种函数?
  10. c语言凸包算法,基于C语言的凸包算法实现