codeforceB. Mishka and trip

来源:互联网 发布:孙侨潞淘宝店叫什么 编辑:程序博客网 时间:2024/06/09 14:56

B. Mishka and trip
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Here are some interesting facts about XXX:

  1. XXX consists of n cities, k of whose (just imagine!) are capital cities.
  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 ci.
  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.
  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.
  5. There is at most one road between any two cities.
  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 ci·cj.

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 betweena and b you are to find sum of products ca·cb. Will you help her?

Input

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.

The second line of the input contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 10 000) — beauty values of the cities.

The third line of the input contains k distinct integers id1, id2, ..., idk (1 ≤ idi ≤ n) — indices of capital cities. Indices are given in ascending order.

Output

Print the only integer — summary price of passing each of the roads in XXX.

Examples
input
4 12 3 1 23
output
17
input
5 23 5 2 2 41 4
output
71
Note

This image describes first sample case:

It is easy to see that summary price is equal to 17.

This image describes second sample case:

It is easy to see that summary price is equal to 71.


题目大意:

有n个城市, 其中k个城市是首都,首都与其他每个城市都有一条路。城市编号为1-n,每个编号相邻城市之间都有一条路。第一个城市与最后一个城市也有一条路。路的长度计算方法为路连接的两个城市的beauty值乘积。

分析:第十个测试点的数据是100000 100000 1 1 1 ....,就是每个城市都是首都,每个beauty都是1,如果用最直白的方法,即每个首都都左右延伸,这样计算的话,计算的次数超过49亿次,Intel E3主机的cpu每秒钟只是一个for循环的话能跑6亿次多,但是这么直白的方法再加上其他零碎的计算后,时间就很长了,所以一定得用好点方法。


解决办法:

先把所有城市的beauty加起来记录tol,把首都的beauty也加起来cap,计算首都到其他城市的额外道路时候,只要计算每个首都需要额外添加的道路值,这个值可以通过(tol-cap)*该店beatuy值获得,大意就是这样。具体实现请看代码。

#include<bits/stdc++.h>using namespace std;const int maxn = 100005;int a[maxn], b[maxn];bool book[maxn];int main(){    int n, k, cnt;    long long tol, sum, cap;    while(cin >> n >> k)    {        memset(book, false, sizeof(book));        cnt = sum = tol = cap = 0;        for(int i = 1; i <= n; i++)        {            scanf("%d" ,&a[i]);            if(i >= 2)                sum += a[i] * a[i-1];            tol += a[i];        }        sum += a[1] * a[n];        for(int i = 0; i < k; i++)        {            scanf("%d", &b[cnt]);            cap += a[b[cnt]];            book[b[cnt++]] = true;        }        for(int i = 0; i < cnt && n > 3; i++)        {            int tem = b[i];            long long tmp = 0;            if(tem == 1)            {                if(book[n] == false)                    tmp += a[n];                if(book[2] == true)                    tmp = tmp + cap;                else                    tmp = tmp + cap + a[2];                sum += (tol - tmp) * a[1];                cap -= a[1];                book[1] = false;            }            else if(tem == n)            {                tmp = tol - a[n-1] - a[1] - a[n];       //记得要把a[n]也减去                sum += tmp * a[tem];            }            else            {                tmp = cap + a[tem - 1];                if(!book[tem+1])                    tmp += a[tem+1];                sum += (tol - tmp) * a[tem];                cap -= a[tem];                book[tem] = false;      //取消该点是首都的标记,一直因为book[tmp]而wa了好久....            }        }        cout << sum << endl;    }    return 0;}



0 0
原创粉丝点击