codeforces-scrape

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

703B.md (2896B)


      1 Source: https://codeforces.com/problemset/problem/703/B
      2 
      3 B. Mishka and trip
      4 
      5 time limit per test
      6 
      7 1 second
      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 Little Mishka is a great traveller and she visited many countries. After thinking about where to travel this time, she chose XXX — beautiful, but little-known northern country.
     22 
     23 Here are some interesting facts about XXX:
     24 
     25 1. XXX consists of *n* cities, *k* of whose (just imagine!) are capital cities.
     26 2. All of cities in the country are beautiful, but each is beautiful in its own way. Beauty value of *i*-th city equals to *c**i*.
     27 3. All the cities are consecutively connected by the roads, including 1-st and *n*-th city, forming a cyclic route 1 — 2 — ... — *n* — 1. Formally, for every 1 ≤ *i* < *n* there is a road between *i*-th and *i* + 1-th city, and another one between 1-st and *n*-th city.
     28 4. Each capital city is connected with each other city directly by the roads. Formally, if city *x* is a capital city, then for every 1 ≤ *i* ≤ *n*,  *i* ≠ *x*, there is a road between cities *x* and *i*.
     29 5. There is at most one road between any two cities.
     30 6. Price of passing a road directly depends on beauty values of cities it connects. Thus if there is a road between cities *i* and *j*, price of passing it equals *c**i*·*c**j*.
     31 
     32 Mishka started to gather her things for a trip, but didn't still decide which route to follow and thus she asked you to help her determine summary price of passing each of the roads in XXX. Formally, for every pair of cities *a* and *b* (*a* < *b*), such that there is a road between *a* and *b* you are to find sum of products *c**a*·*c**b*. Will you help her?
     33 
     34 Input
     35 
     36 The first line of the input contains two integers *n* and *k* (3 ≤ *n* ≤ 100 000, 1 ≤ *k* ≤ *n*) — the number of cities in XXX and the number of capital cities among them.
     37 
     38 The second line of the input contains *n* integers *c*1, *c*2, ..., *c**n* (1 ≤ *c**i* ≤ 10 000) — beauty values of the cities.
     39 
     40 The third line of the input contains *k* distinct integers *id*1, *id*2, ..., *id**k* (1 ≤ *id**i* ≤ *n*) — indices of capital cities. Indices are given in ascending order.
     41 
     42 Output
     43 
     44 Print the only integer — summary price of passing each of the roads in XXX.
     45 
     46 Examples
     47 
     48 Input
     49 
     50 ```
     51 4 1  
     52 2 3 1 2  
     53 3
     54 ```
     55 
     56 Output
     57 
     58 ```
     59 17
     60 ```
     61 
     62 Input
     63 
     64 ```
     65 5 2  
     66 3 5 2 2 4  
     67 1 4
     68 ```
     69 
     70 Output
     71 
     72 ```
     73 71
     74 ```
     75 
     76 Note
     77 
     78 This image describes first sample case:
     79 
     80 ![](https://espresso.codeforces.com/381b902daeaf561f856da6af5655b6b8fc013e86.png)
     81 
     82 It is easy to see that summary price is equal to 17.
     83 
     84 This image describes second sample case:
     85 
     86 ![](https://espresso.codeforces.com/b7b0066f0651fd531bb1143eb8d1eb9d5bc600d3.png)
     87 
     88 It is easy to see that summary price is equal to 71.