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:
- XXX consists of n cities, k of whose (just imagine!) are capital cities.
- 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.
- All the cities are consecutively connected by the roads, including 1-st and n-th city, forming a cyclic route1 — 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.
- Each capital city is connected with each other city directly by the roads. Formally, if city x is a capital city, then for every1 ≤ i ≤ n, i ≠ x, there is a road between cities x and i.
- There is at most one road between any two cities.
- Price of passing a road directly depends on beauty values of cities it connects. Thus if there is a road between cities i andj, 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 between a and b you are to find sum of products ca·cb. Will you help her?
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.
Print the only integer — summary price of passing each of the roads in XXX.
4 1
2 3 1 2
3
17
5 2
3 5 2 2 4
1 4
71
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个中心城市 每个城市都有一个权值c k个中心城市编号按照升序给出
首先以1 — 2 — ... — n — 1路径串成一个环,其次中心城市到任意城市都有一条路.任意两个城市间最多有一条路
每条边的权值为相邻两个城市的权值的乘积。求所有边的权值的和。
题解:
1.处理成串的边的权值和
2.处理与中心城市相连的城市的边的权值和
3.去重(某条边的两段都为中心城市)
1 /****************************** 2 code by drizzle 3 blog: www.cnblogs.com/hsd-/ 4 ^ ^ ^ ^ 5 O O 6 ******************************/ 7 //#include<bits/stdc++.h> 8 #include<iostream> 9 #include<cstring> 10 #include<cstdio> 11 #include<map> 12 #include<algorithm> 13 #include<queue> 14 #include<cmath> 15 #define ll __int64 16 #define PI acos(-1.0) 17 #define mod 1000000007 18 using namespace std; 19 int n,k; 20 ll a[100005]; 21 int b[100005]; 22 int used[100005]; 23 ll zha[100005]; 24 int main() 25 { 26 scanf("%d %d",&n,&k); 27 ll sum=0; 28 ll ans=0; 29 memset(used,0,sizeof(used)); 30 for(int i=1;i<=n;i++) 31 { 32 scanf("%I64d",&a[i]); 33 sum+=a[i]; 34 } 35 zha[0]=0; 36 for(int i=1;i<=k;i++) 37 { 38 scanf("%d",&b[i]); 39 zha[i]=zha[i-1]+a[b[i]]; 40 used[b[i]]=1; 41 } 42 for(int i=1;i<n;i++) 43 ans=ans+a[i]*a[i+1]; 44 ans=ans+a[n]*a[1]; 45 for(int i=1;i<=k;i++) 46 { 47 if(b[i]==1) 48 { 49 ans=ans+a[1]*(sum-a[1]-a[2]-a[n]); 50 continue; 51 } 52 if(b[i]==n) 53 { 54 ans=ans+a[n]*(sum-a[1]-a[n]-a[n-1]); 55 continue; 56 } 57 ans=ans+a[b[i]]*(sum-a[b[i]]-a[b[i]-1]-a[b[i]+1]); 58 } 59 for(int i=1;i<=k;i++) 60 { 61 ll exm=zha[k]-zha[i]; 62 if(b[i]==n) 63 break; 64 if(b[i]==1) 65 { 66 if(used[2]) 67 exm=exm-a[2]; 68 if(used[n]) 69 exm=exm-a[n]; 70 ans=ans-exm*a[b[i]]; 71 } 72 else 73 { 74 if(used[b[i]+1]) 75 exm=exm-a[b[i]+1]; 76 ans=ans-exm*a[b[i]]; 77 } 78 } 79 printf("%I64d ",ans); 80 }