Mahmoud wants to send a message to his friend Ehab. Their language consists of n words numbered from 1 to n. Some words have the same meaning so there are k groups of words such that all the words in some group have the same meaning.

Mahmoud knows that the i-th word can be sent with cost ai. For each word in his message, Mahmoud can either replace it with another word of the same meaning or leave it as it is. Can you help Mahmoud determine the minimum cost of sending the message?

The cost of sending the message is the sum of the costs of sending every word in it.

Input

The first line of input contains integers nk and m (1 ≤ k ≤ n ≤ 105, 1 ≤ m ≤ 105) — the number of words in their language, the number of groups of words, and the number of words in Mahmoud's message respectively.

The second line contains n strings consisting of lowercase English letters of length not exceeding 20 which represent the words. It's guaranteed that the words are distinct.

The third line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) where ai is the cost of sending the i-th word.

The next k lines describe the groups of words of same meaning. The next k lines each start with an integer x (1 ≤ x ≤ n) which means that there are x words in this group, followed by x integers which represent the indices of words in this group. It's guaranteed that each word appears in exactly one group.

The next line contains m space-separated words which represent Mahmoud's message. Each of these words appears in the list of language's words.

Output

The only line should contain the minimum cost to send the message after replacing some words (maybe none) with some words of the same meaning.

Examples
input

Copy
5 4 4
i loser am the second
100 1 1 5 10
1 1
1 3
2 2 5
1 4
i am the second
output

Copy
107
input

Copy
5 4 4
i loser am the second
100 20 1 5 10
1 1
1 3
2 2 5
1 4
i am the second
output

Copy
116
Note

In the first sample, Mahmoud should replace the word "second" with the word "loser" because it has less cost so the cost will be 100+1+5+1=107.

In the second sample, Mahmoud shouldn't do any replacement so the cost will be 100+1+5+10=116.


阅读理解题

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
//#include <xfunctional>
#define ll long long
#define mod 998244353
using namespace std;
int dir[][] = { {,},{,-},{-,},{,} };
const int maxn = ;
const long long inf = 0x7f7f7f7f7f7f7f7f;
const int INT = 0x3f3f3f3f;
int main()
{
int n, k, m;
cin >> n >> k >> m;
vector<string> s(n + );
map<string, int> mp;
map<int, int> mpn;
for (int i = ; i <= n; i++)
{
cin >> s[i];
mp[s[i]] = i;
}
for (int i = ; i <= n; i++)
{
int t;
cin >> t;
mpn[i] = t;
}
for (int i = ; i < k; i++)
{
int all,minn=INT;
cin >> all;
vector<int> v(all);
for(int i=;i<all;i++)
{
cin >> v[i];
minn = min(minn, mpn[v[i]]);
}
for (int i = ; i < all; i++)
{
mpn[v[i]] = minn;
}
}
ll res=;
while (m--)
{
string ss;
cin >> ss;
res += mpn[mp[ss]];
}
cout << res;
return ;
}

Mahmoud and Ehab and the message的更多相关文章

  1. [CF959B]Mahmoud and Ehab and the message题解

    超级大模拟 直接用map吧string对应到编号上来,然后在开个数组把每个编号对应到每个可以互相转化区块上来,预处理出区块的最小值,使用时直接取最小是即可 代码 #include <cstdio ...

  2. Codeforces 862A Mahmoud and Ehab and the MEX

    传送门:CF-862A A. Mahmoud and Ehab and the MEX time limit per test 2 seconds memory limit per test 256 ...

  3. Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)

    Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...

  4. E - Mahmoud and Ehab and the bipartiteness CodeForces - 862B (dfs黑白染色)

    Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipa ...

  5. Codeforces 862B - Mahmoud and Ehab and the bipartiteness

    862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...

  6. Codeforces 862C - Mahmoud and Ehab and the xor

    862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了 ...

  7. Codeforces 862D. Mahmoud and Ehab and the binary string (二分)

    题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...

  8. CF 959 E. Mahmoud and Ehab and the xor-MST

    E. Mahmoud and Ehab and the xor-MST https://codeforces.com/contest/959/problem/E 分析: 每个点x应该和x ^ lowb ...

  9. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

随机推荐

  1. ipad的12系统里提示更新到ipdos13,原来安装在12上的app是不是没有了,要重新下载

    ipad的12系统里提示更新到ipdos13,原来安装在12上的app是不是没有了,要重新下载 更新后原来的WiFi密码都还在不用重新连接WiFi,桌面壁纸也是原来12系统上的 在Ipad的设置里将1 ...

  2. alibaba工程师,如何解决乐观锁冲突问题?

    很多做过电商系统的人应该知道,我们在设计电商系统中关于商品库存扣减时,在大部分情况下(并发量不高时),商品库存都可以直接在关系型数据库中进行扣减,那么在限时抢购活动正式开始后,那些单价比平时更给力.更 ...

  3. Windows下Anaconda安装、换源与更新

    Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项.当你尝试pip install xxx时出现各种意外和依赖问题,那么conda就是一 ...

  4. C#排序算法的实现---快速排序

    快速排序(Quicksort)是对冒泡排序的一种改进.由C. A. R. Hoare在1962年提出.它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的 ...

  5. 剑指offer-面试题14-剪绳子-贪婪算法

    /* 题目: 给定一个长度为n的绳子,把绳子剪为m段,(n>1,m>1) 求各段绳子乘积的最大值. */ /* 思路: 贪婪算法. 当绳子的长度大于5时,尽可能多的剪长度为3的绳子:当剩下 ...

  6. oracle卸载步骤

    一.删除产品 1. 停服务,进入下图,一个个删除Oracle产品,再执行卸载 二.清注册表,开始-搜索- 输入regedit,进入注册表 1.选择HKEY_LOCAL_MACHINE\SOFTWARE ...

  7. 查看whl包名是否满足系统的条件的命令,以此解决whl包出现“is not a supported wheel on this platform”错误提示的问题

    在Ubuntu系统中,使用pip安装whl包时,常常会报如下错误: tensorflow_gpu-1.11.0-cp35-cp35m-manylinux1_x86_64.whl is not a su ...

  8. LED Candle Light Factory-LED Candle Light: Full Of Romance

    LED candle lights are beautiful and can add elegance and romance to any space. These candles make th ...

  9. 接口测试(http 和 rpc)

    接口测试主要分HTTP和RPC两类,RPC类型里面以Dubbo较为知名.互联网微服务架构,两种接口都需要做接口测试的,不管是业务测试还是回归测试: Dubbo:Java栈的互联网公司比如阿里.美团.5 ...

  10. VSCode常用插件之open in browser使用

    更多VSCode插件使用请访问:VSCode常用插件汇总 open in browser安装完这个插件就可以在编辑器菜单右键html,在默认浏览器打开了,高级使用暂未了解,请自行其它文章学习