"Play CF, learn algorithm--Two star" Codeforces 703B Mishka and Trip (statistics)

Source: Internet
Author: User

"About CF"

Title Link: CF 703B

Surface:

A-Mishka and TripTime Limit:MS Memory Limit:262144KB 64bit IO Format:%i64d & Amp %i64u SubmitStatusPracticecodeforces 703B

Description

Little Mishka is a great traveller and she visited many countries. After thinking on where to travel this time, she chose Xxx-beautiful, but Little-known Northern country.

Here is some interesting facts about XXX:

  1. XXX consists of n cities, K of whose (just imagine!) is capital cities.
  2. All of the cities in the country were beautiful, but they were beautiful in their own. Beauty value ofi-th city equals to CI.
  3. All the cities is 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.
  4. Each of the capital city are connected with the directly by the roads. Formally, if Cityx was a capital city and then for every 1?≤? I? ≤? N,?? i? ≠? x, there is a road between citiesx and I.
  5. There is at the most one road between any and cities.
  6. Price of passing a road directly depends on beauty values of cities it connects. Thus If there is a road between citiesI and J, Price of passing it equalsci c J .

Mishka started to gather hers things for a trips, but didn ' t still decide which route to follow and thus she asked the He LP her determine summary price of passing each of theroads in XXX. Formally, for every pair of citiesa and b (a? <? b), such that there was a road betweena and b you were to find the sum of productsca· C b. Would you help her?

Input

The first line of the input contains the integers n andK (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 integersc1,? C2,?...,? Cn (1?≤? ) Ci≤?10?000)-beauty values of the cities.

The third line of the input contains k distinct integersID1,? ID2,?...,? IDk (1?≤? ) IDi? ≤? n)-indices of capital cities. Indices is given in ascending order.

Output

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

Sample Input

Input
4 12 3) 1 23
Output
17
Input
5 23 5 2 2 41 4
Output
71

Hint

This image describes first sample case:

It's easy-to-see-summary price was equal to.

This image describes second the sample case:

It's easy-to-see-summary price was equal to.


Test instructions

1-n points, K points is the key point, the key point will be to all points of the edge, non-critical point only to the side of the side, the end of the end, two points between the most only one edge, two points between the cost of the edge, is the value of the two points of the product, ask all the total cost of the side?


Solving:

The cost of simply counting all the edges is simple, but because of the number of points, this can be timed out. The total cost of all points can be pre-calculated, and the total cost of all the key points. When calculating, all edges are counted two times, the non-critical point is better processed, as long as the two sides of the edge, and the key point to all points to connect twice times, the key points will have four edges, to be subtracted later, and the key point and not adjacent to the non-critical point is twice times the edge, and the key points and adjacent non-critical points (if it is) three times to subtract once. Finally, after the statistics, the total cost is divided by 2, which is the request.


Code:

#include <iostream> #include <cstdio> #define LL long long#define sz 100005using namespace std;//each point valll val[ sz];//is Capitalbool vis[sz];//which point is Capitalint keyp[sz];int main () {int n,k,tmp;//sum1 is all points of and, Sum2 is capital and LL Sum1=0,    ANS=0,SUM2=0,TMP2=0;SCANF ("%d%d", &n,&k); for (int i=1;i<=n;i++) {scanf ("%lld\n", &val[i]); sum1+=val[i];}    for (int i=1;i<=k;i++) {scanf ("%d", &tmp); vis[tmp]=1;keyp[i]=tmp;sum2+=val[tmp]; }//end-to-end connection val[0]=val[n];vis[0]=vis[n];val[n+1]=val[1];vis[n+1]=vis[1];//All sides calculated two times for (int i=1;i<=n;i++) {//  It's capital, so it calculates two times on the side of the exit if (Vis[i]) {ans+=2* (Sum1-val[i]) *val[i];  If the previous point is not capital, subtract it once, because the non-point will also be connected to either side if (!vis[i-1]) ans-=val[i]*val[i-1];  if (!vis[i+1]) ans-=val[i]*val[i+1]; }//non-capital point to both sides of the edge else ans+=val[i]* (Val[i+1]+val[i-1]);} The key points are connected two times to all points and 4 times between the keys, so subtract 2 times for (int i=1;i<=k;i++) {tmp2+= (Sum2-val[keyp[i]) *val[keyp[i]];} ans-=tmp2;//because all sides were counted two times printf ("%lld\n", ANS/2); return 0;}


"Play CF, learn algorithm--Two star" Codeforces 703B Mishka and Trip (statistics)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.