Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- ll ans, n, m;
- vector <ll> att, def, my;
- ll with_def(){
- multiset <ll> S;
- for (auto e : my)
- S.insert(e);
- my.clear();
- for (auto e : def){
- auto it = S.upper_bound(e);
- if (it == S.end()) return 0LL;
- S.erase(it);
- }
- for (auto e : S)
- my.push_back(e);
- sort(my.begin(), my.end());
- ll res = 0;
- int poc = 0;
- for (auto e : my){
- if (poc == att.size()){
- res += e;
- continue;
- }
- if (e < att[poc])
- return 0LL;
- res += e - att[poc];
- poc ++;
- }
- return res;
- }
- ll try_attack(int cnt){
- ll res = 0;
- if (cnt > my.size()) return 0LL;
- vector <ll> my_cards;
- vector <ll> to_attack;
- for (int i = 0; i < cnt; i ++)
- my_cards.push_back(my[i]);
- for (int i = att.size() - 1; to_attack.size() < cnt; i --)
- to_attack.push_back(att[i]);
- sort(to_attack.begin(), to_attack.end());
- sort(my_cards.begin(), my_cards.end());
- for (int i = 0; i < my_cards.size(); i ++){
- if (my_cards[i] < to_attack[i]) return 0LL;
- res += my_cards[i] - to_attack[i];
- }
- return res;
- }
- int main(){
- cin >> n >> m;
- for (int i = 0; i < n; i ++){
- string s;
- int x;
- cin >> s >> x;
- if (s[0] == 'A')
- att.push_back(x);
- else
- def.push_back(x);
- }
- for (int i = 0; i < m; i ++){
- int x; cin >> x;
- my.push_back(x);
- }
- sort(my.begin(), my.end());
- sort(att.begin(), att.end());
- sort(def.begin(), def.end());
- reverse(my.begin(), my.end());
- reverse(att.begin(), att.end());
- reverse(def.begin(), def.end());
- for (int i = 1; i < att.size(); i ++)
- ans = max(ans, try_attack(i));
- cout << max(ans, with_def());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement