#include <bits/stdc++.h>
#define ll long long
#define SWAP(i, j, t) t = i, i = j, j = t
using namespace std;
const ll MAX = 1e6;
ll scores[MAX];
int main() {
ll t;
cin >> t;
while(t--) {
ll n, m;
cin >> n >> m;
ll u[n], v[m];
string d1, d2;
for(int i = 0; i < n; ++i) cin >> u[i];
cin >> d1;
for(int i = 0; i < m; ++i) cin >> v[i];
cin >> d2;
priority_queue<ll, vector<ll>, less<ll>> defense1, defense2;
priority_queue<ll, vector<ll>, greater<ll>> attack1, attack2;
scores[0] = 0;
for(int i = 0; i < n; i++) {
scores[0] += u[i];
if(d1[i] == 'A') attack1.push(u[i]);
else defense1.push(u[i]);
}
for(int i = 0; i < m; i++) {
scores[0] -= v[i];
if(d2[i] == 'A') attack2.push(v[i]);
else defense2.push(v[i]);
}
int i = 0;
int cnt = 1;
while(1) {
if(i%2 == 0) {
if(attack1.empty() || defense2.empty()) break;
ll t1 = attack1.top(); attack1.pop();
ll t2 = defense2.top(); defense2.pop();
//cout << t1 << " " << t2 << endl;
scores[cnt] = scores[cnt-1] + t2;
defense1.push(t1);
cnt++;
}
else {
if(attack2.empty() || defense1.empty()) break;
ll t1 = attack2.top(); attack2.pop();
ll t2 = defense1.top(); defense1.pop();
//cout << t1 << " " << t2 << endl;
scores[cnt] = scores[cnt-1] - t2;
defense2.push(t2);
cnt++;
}
i++;
}
--cnt;
ll scr = scores[cnt];
cnt--;
while(cnt >= 0) {
if(cnt%2 == 0) {
if(scores[cnt] > scr) scr = scores[cnt];
}
else {
if(scores[cnt] < scr) scr = scores[cnt];
}
cnt--;
}
cout << scr << '\n';
}
}
Why this code is giving wrong answer for monster problem of Feb Lunchtime.