Fox Ciel is playing a card game with her friend Jiro.

Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.

Now is Ciel's battle phase, Ciel can do the following operation many times:

  1. Choose one of her cards X. This card mustn't be chosen before.
  2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then:
    • If Y's position is Attack, then (X's strength)  ≥  (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength).
    • If Y's position is Defense, then (X's strength)  >  (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage.

Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.

 

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of cards Jiro and Ciel have.

Each of the next n lines contains a string position and an integer strength (0 ≤ strength ≤ 8000) — the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense.

Each of the next m lines contains an integer strength (0 ≤ strength ≤ 8000) — the strength of Ciel's current card.

 

Output

Output an integer: the maximal damage Jiro can get.

 

Sample Input

Input
2 3
ATK 2000
DEF 1700
2500
2500
2500
Output
3000
Input
3 4
ATK 10
ATK 100
ATK 1000
1
11
101
1001
Output
992
Input
2 4
DEF 0
ATK 0
0
0
1
1
Output
1

Hint

In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000.

In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.

In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card.

  这道题提示我们可以用INF强化优先级,之后减回来就好。

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=,M=,INF=;
int n,m,cnt,fir[N],nxt[M],to[M],cap[M],val[M];
int vis[N],dis[N],path[N],q[N],front,back;
void addedge(int a,int b,int c,int v){
nxt[++cnt]=fir[a];to[fir[a]=cnt]=b;cap[cnt]=c;val[cnt]=v;
nxt[++cnt]=fir[b];to[fir[b]=cnt]=a;cap[cnt]=;val[cnt]=-v;
//printf("%d %d %d %d\n",a,b,c,v);
} int BFS(int S,int T){
for(int i=S+;i<=T;i++)dis[i]=*INF;
q[front=back=]=S;vis[S]=true;
while(front<=back){
int x=q[front++];vis[x]=false;
for(int i=fir[x];i;i=nxt[i])
if(cap[i]&&dis[to[i]]>dis[x]+val[i]){
dis[to[i]]=dis[x]+val[i];
if(!vis[to[i]])q[++back]=to[i];
vis[to[i]]=true;path[to[i]]=i;
}
}
return dis[T];
} void Aug(int S,int T){
int p=T;
while(p!=S){
cap[path[p]]-=;
cap[path[p]^]+=;
p=to[path[p]^];
}
} int McMf(int S,int T){
int ret=,d,tmp=;
for(int i=;i<=n;i++){
d=BFS(S,T);
if(d>=INF)return -ret;
tmp+=d;ret=min(ret,tmp);Aug(S,T);
}
for(int i=n+;i<=m;i++){
d=BFS(S,T);
if(d==*INF)return -ret;
tmp+=d-*INF;ret=min(ret,tmp);Aug(S,T);
}
return -ret;
} int A[N],D[N],B[N],ca,cd,S,T,x;
void Init(){
memset(dis,,sizeof(dis));
memset(fir,,sizeof(fir));
front=back=cnt=;
}
char op[];
int main(){
Init();
scanf("%d%d",&n,&m);
S=;T=n+m+;
for(int i=;i<=n;i++){
scanf("%s%d",op,&x);
if(op[]=='A')A[++ca]=x;
if(op[]=='D')D[++cd]=x;
}
for(int i=;i<=n;i++)
addedge(i+m,T,,);
for(int i=;i<=m;i++){
scanf("%d",&B[i]);
addedge(S,i,,);
addedge(i,T,,*INF-B[i]);
for(int j=;j<=ca;j++)
if(A[j]<=B[i])addedge(i,m+j,,-B[i]+A[j]);
for(int j=;j<=cd;j++)
if(D[j]<B[i])addedge(i,m+j+ca,,);
}
printf("%d\n",McMf(S,T));
return ;
}

网络流(费用流)CodeForces 321B:Ciel and Duel的更多相关文章

  1. CF 321B Ciel and Duel(费用流)

    题目链接:http://codeforces.com/problemset/problem/321/B 题意:两个人,分别有n.m张牌.每张牌有两个属性类型和能力,类型为攻击或者防守.B的m张牌的属性 ...

  2. BZOJ2055 80人环游世界 网络流 费用流 有源汇有上下界的费用流

    https://darkbzoj.cf/problem/2055 https://blog.csdn.net/Clove_unique/article/details/54864211 ←对有上下界费 ...

  3. 【上下界网络流 费用流】bzoj2055: 80人环游世界

    EK费用流居然写错了…… Description     想必大家都看过成龙大哥的<80天环游世界>,里面的紧张刺激的打斗场面一定给你留下了深刻的印象.现在就有这么     一个80人的团 ...

  4. 图论--网络流--费用流POJ 2195 Going Home

    Description On a grid map there are n little men and n houses. In each unit time, every little man c ...

  5. 图论--网络流--费用流--POJ 2156 Minimum Cost

    Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...

  6. HDU 5352——MZL's City——————【二分图多重匹配、拆点||网络流||费用流】

    MZL's City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. 线性规划||网络流(费用流):COGS 288. [NOI2008] 志愿者招募

    [NOI2008] 志愿者招募 输入文件:employee.in   输出文件:employee.out   简单对比 时间限制:2 s   内存限制:512 MB [问题描述] 申奥成功后,布布经过 ...

  8. POJ训练计划3422_Kaka&#39;s Matrix Travels(网络流/费用流)

    解题报告 题目传送门 题意: 从n×n的矩阵的左上角走到右下角,每次仅仅能向右和向下走,走到一个格子上加上格子的数,能够走k次.问最大的和是多少. 思路: 建图:每一个格子掰成两个点,分别叫" ...

  9. BZOJ 1834: [ZJOI2010]network 网络扩容(网络流+费用流)

    一看就知道是模板题= = ,不说什么了= = PS:回去搞期末了,暑假再来刷题了 CODE: #include<cstdio> #include<iostream> #incl ...

  10. 【bzoj1877】[SDOI2009]晨跑 费用流

    题目描述 Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个十字路口和M条街 ...

随机推荐

  1. jQuery.each的function中有哪些参数(可以大概理解function中的参数问题)

    1.没有参数 $("img").each(function(){ $(this).toggleClass("example"); }); 1 2 3 2.有一个 ...

  2. 利用SpringMVC参数绑定实现动态插入数据

    javabean代码:public class User { private String firstName; private String lastName; public String getF ...

  3. QuickSort 递归 分治

    QuickSort 参考<算法导论>,<C程序设计语言> #include<stdio.h> void swap(int v[], int i, int j); v ...

  4. Best Time to Buy and Sell Stock III 解题思路

    题目要求: 最多交易两次,并且只能买卖完之后再买. 总思路: 在数组中找一个适当的点i,使得i左右两边profit之和最大. 思路: 1.从左往右扫描,left[i]记录包括i元素以内的左部的maxp ...

  5. SQL分页小Demo

    SELECT @TotalCount=count(1) FROM TableA A WITH(NOLOCK) INNER JOIN TableB B WITH(NOLOCK) ON A.Id=B.Id ...

  6. Android Material Design NavigationView 及 Palette 颜色提取器

    DrawerLayout + NavigationView DrawerLayout布局,通常在里面添加两个子控件,程序主界面添加到NavitagionView前面. <android.supp ...

  7. 【转】oracle数据库NUMBER数据类型

    原文:http://www.jb51.net/article/37633.htm NUMBER ( precision, scale)a)  precision表示数字中的有效位;如果没有指定prec ...

  8. Win7下通过easyBCD引导安装Ubuntu14.04

    Ubuntu14.04作为目前最新版本的ubuntu系统,相信很多人都想在自己的电脑上安装一下,然而系统的安装方法各式各样,u盘法.grub引导法等等,这里我将介绍在win7系统下用easyBCD软件 ...

  9. linux平台上面python调用c

    不能免俗,先打印个helloworld出来,c代码的函数 hello.c #include <stdio.h> int helloworld() { printf("hello ...

  10. info.plist 属性讲解

    1 常用项: Application requires iPhone environment:如果应用程序不能在ipodtouch上运行,设置此项为true; Application usesWi-F ...