Source Code
/// Frail the skin is dry and pale
#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;

#include "bits/stdc++.h"
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace __gnu_pbds;
using namespace __gnu_cxx;

using ll = long long;
using ii = pair<int, int>;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define pb push_back
#define F first
#define S second
#define f(i, a, b)  for(int i = a; i < b; i++)
#define all(a)  a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define mp make_pair

const int N = 1e6 + 5, LG = 18, MOD = 31607;
const double PI = acos(-1);
int get(char c) {
    if(c == 's')return 0;
    if(c == 'c')return 1;
    return 2;
}
vector<int> go[3];
vector<int> psum[3];
ll sum[4 * 100005];
int cnt[4 * 100005];
void upd(int node, int s, int e, int idx, int val) {
    if(s==e) {
        sum[node] += s * val;
        cnt[node] += val;
        return;
    }
    int md = (s + e) >> 1;
    if(idx <= md)
        upd(node<<1,s,md,idx,val);
    else
        upd(node<<1|1,md+1,e,idx,val);
    sum[node] = sum[node<<1] + sum[node<<1|1];
    cnt[node] = cnt[node<<1] + cnt[node<<1|1];
}
ll calc(int node, int s, int e, int c) {
    if(s==e) {
        return 1ll * c * s;
    }
    int md = (s + e) >> 1;
    if(cnt[node<<1|1] <= c)
        return sum[node<<1|1] + calc(node<<1,s,md,c-cnt[node<<1|1]);
    return calc(node<<1|1,md+1,e,c);
}
void doWork() {

    int n, a, b;
    cin >> n >> a >> b;

    f(i,0,n) {
        char c;
        cin >> c;
        int x;
        cin >> x;
        go[get(c)].pb(x);
    }

    int tot = 0;
    f(i,0,3) {
        sort(all(go[i]));
        reverse(all(go[i]));    ///el kbeer el awal
        psum[i].pb(0);
        for(auto x : go[i]) {
            psum[i].pb(x + psum[i].back());
            if(i!=0)
                upd(1,1,100000, x, 1),tot+=1;
        }
    }

    ll ans = 0;
    for(int take = 0; take <= go[1].size(); take++) {
        if(take) {
            upd(1, 1, 100000, go[1][take-1], -1);
        }
        if((go[0].size() + take) / a <= (go[1].size() - take + go[2].size()) / b) {
            int turns = (go[0].size() + take) / a;
            if(take < a * turns) {
                ll curAns = psum[1][take]+psum[0].back() + calc(1, 1, 100000, (go[1].size() - take + go[2].size() - turns * b));
                ans = max(ans, curAns);
            }
        }
    }

    cout << ans << '\n';






}
int32_t main() {
#ifdef ONLINE_JUDGE
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif // ONLINE_JUDGE
    int t = 1;
//    cin >> t;
    while (t--) {
        doWork();
    }


    return 0;
}
Copy
ClearTheBit DeadlyPillow
GNU G++17
1 ms
360 KB
Wrong Answer