日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 632B】Alice, Bob, Two Teams (预处理,思维,前缀和后缀和) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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.

    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 (预处理,思维,前缀和后缀和)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。