[CF] B. Alice, Bob, Two Teams - Educational Codeforces Round 9
Solutions Codeforces 前缀和 1400 Easy
Lastmod: 2025-02-03 周一 23:45:10

https://codeforces.com/contest/632/problem/B

题目大意

给出 $n \ (\le 5 \times 10^5)$ 长的权重数组 $p$ 其中 $1 \le p_i \le 10^9$,以及 $n$ 长的只包含 AB 的字符串 $S$。可以最多一次,选择 $S$ 的前缀或后缀(题意这里不是特别清楚),将其中的 AB 互换。最大化 B 对应的权重的和。问最大权重是多少。

简要题解

前缀和处理一下就行了。(这题凭啥 $1400$ 啊)

复杂度

$T$:$O(n)$

$S$:$O(n)$

代码实现

#include <bits/stdc++.h>
using namespace std;

int io_=[](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();

using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using PII = pair<int, int>;
using VI = vector<int>;
using MII = map<int, int>;

template<typename T> void cmin(T &x,const T &y) { if(y<x) x=y; }
template<typename T> void cmax(T &x,const T &y) { if(x<y) x=y; }
template<typename T> bool ckmin(T &x,const T &y) { 
    return y<x ? (x=y, true) : false; }
template<typename T> bool ckmax(T &x,const T &y) { 
    return x<y ? (x=y, true) : false; }
template<typename T> void cmin(T &x,T &y,const T &z) {// x<=y<=z 
    if(z<x) { y=x; x=z; } else if(z<y) y=z; }
template<typename T> void cmax(T &x,T &y,const T &z) {// x>=y>=z
    if(x<z) { y=x; x=z; } else if(y<z) y=z; }

// mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
// mt19937_64 rnd_64(chrono::system_clock::now().time_since_epoch().count());

/*
---------1---------2---------3---------4---------5---------6---------7---------
1234567890123456789012345678901234567890123456789012345678901234567890123456789
*/

void solve() {
  int n; cin >> n;
  vector<int> p(n);
  for (int& i : p) cin >> i;
  string s; cin >> s;

  LL ans = 0;

  for (int ii = 0; ii < 2; ii++) {
    vector<LL> sum(n + 1);
    for (int i = 0; i < n; i++) {
      sum[i + 1] = sum[i] + (s[i] == 'B' ? p[i] : 0);
    }
    cmax(ans, sum[n]);
    LL cur = 0, curb = 0;
    for (int i = n - 1; i >= 0; i--) {
      cur += p[i];
      if (s[i] == 'B') curb += p[i];

      cmax(ans, cur - curb + sum[i]);
    }
    reverse(p.begin(), p.end());
    reverse(s.begin(), s.end());
  }

  cout << ans << '\n';
}

int main() {
  int t = 1; 
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Prev: [CF] B. Cat Cycle - Educational Codeforces Round 104 (Rated for Div. 2)
Next: [CF] B. The Modcrab - Educational Codeforces Round 34 (Rated for Div. 2)