codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

959B.md (2531B)


      1 Source: https://codeforces.com/problemset/problem/959/B
      2 
      3 B. Mahmoud and Ehab and the message
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 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.
     22 
     23 Mahmoud knows that the *i*-th word can be sent with cost *a**i*. 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?
     24 
     25 The cost of sending the message is the sum of the costs of sending every word in it.
     26 
     27 Input
     28 
     29 The first line of input contains integers *n*, *k* 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.
     30 
     31 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.
     32 
     33 The third line contains *n* integers *a*1, *a*2, ..., *a**n* (1 ≤ *a**i* ≤ 109) where *a**i* is the cost of sending the *i*-th word.
     34 
     35 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.
     36 
     37 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.
     38 
     39 Output
     40 
     41 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.
     42 
     43 Examples
     44 
     45 Input
     46 
     47 ```
     48 5 4 4  
     49 i loser am the second  
     50 100 1 1 5 10  
     51 1 1  
     52 1 3  
     53 2 2 5  
     54 1 4  
     55 i am the second
     56 ```
     57 
     58 Output
     59 
     60 ```
     61 107
     62 ```
     63 
     64 Input
     65 
     66 ```
     67 5 4 4  
     68 i loser am the second  
     69 100 20 1 5 10  
     70 1 1  
     71 1 3  
     72 2 2 5  
     73 1 4  
     74 i am the second
     75 ```
     76 
     77 Output
     78 
     79 ```
     80 116
     81 ```
     82 
     83 Note
     84 
     85 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.
     86 
     87 In the second sample, Mahmoud shouldn't do any replacement so the cost will be 100+1+5+10=116.