Time Limits: 1000 ms Memory Limits: 262144 KB Detailed Limits
1 #include <bits/stdc++.h> 2 #define ll long long 3 #define il inline 4 #define rgi register ll 5 6 using namespace std; 7 8 const int oo = 0x3f3f3f3f; 9 const ll N = 100000 + 10; 10 11 ll n, m, step, ans, minn = oo; 12 ll def[N], atk[N], x[N]; 13 ll sum_def[N], sum_atk[N], sum_x[N]; 14 15 il ll read() 16 { 17 rgi x = 0, f = 0, ch; 18 while(!isdigit(ch = getchar())) f |= ch == '-'; 19 while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); 20 return f ? -x : x; 21 } 22 23 int main() 24 { 25 m = read(),n = read(); 26 for(rgi i = 1; i <= m; ++i) 27 { 28 char op[10]; 29 ll val; 30 scanf("%s %lld", op, &val); 31 if(op[0] == 'A') 32 atk[++atk[0]] = val; 33 else if(op[0] == 'D') 34 def[++def[0]] = val; 35 } 36 for(rgi i = 1; i <= n; ++i) 37 x[i] = read(); 38 sort(atk + 1, atk + atk[0] + 1); 39 sort(def + 1, def + def[0] + 1); 40 sort(x + 1, x + n + 1); 41 for(rgi i = 1; i <= atk[0]; ++i) 42 sum_atk[i] = sum_atk[i - 1] + atk[i]; 43 for(rgi i = 1; i <= def[0]; ++i) 44 sum_def[i] = sum_def[i - 1] + def[i]; 45 for(rgi i = 1; i <= n; ++i) 46 sum_x[i] = sum_x[i - 1] + x[i]; 47 atk[atk[0]+1] = oo; 48 for(rgi i = 1; i <= n; ++i) 49 { 50 while(x[i] >= atk[step]) 51 step++; 52 step--; 53 minn = min(minn, step + n - i); 54 } 55 ans = sum_x[n] - sum_x[n - minn] - sum_atk[minn]; 56 if(minn >= atk[0] && n > m) 57 { 58 step = 1; 59 for(rgi i = 1; i <= def[0]; ++i) 60 { 61 while(def[i] >= x[step]) 62 step++; 63 x[step] = 0; 64 } 65 if(step <= n - minn) 66 for(rgi i = 1; i <= n - minn; ++i) 67 ans += x[i]; 68 } 69 for(rgi i = 1; i < minn; ++i) 70 ans = max(ans, sum_x[n] - sum_x[n - i] - sum_atk[i]); 71 printf("%lld", ans); 72 return 0; 73 }