日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當前位置:首頁 > 芯聞號 > 充電吧
[導讀]【CF簡介】 題目鏈接:CF 703B 題面: A - Mishka and trip Time Limit:1000MS????Memory Limit:262144KB????64bit I

【CF簡介】

題目鏈接:CF 703B

題面:

A - Mishka and trip Time Limit:1000MS????Memory Limit:262144KB????64bit IO Format:%I64d & %I64u SubmitStatusPracticeCodeForces 703B

Description

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 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. 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. 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 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 between a 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.

Sample Input

Input
4 1
2 3 1 2
3
Output
17
Input
5 2
3 5 2 2 4
1 4
Output
71

Hint

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.


題意:

??? 1-n個點,k個點是關鍵點,關鍵點會向所有點連邊,非關鍵點只向兩邊連邊,首尾相接,兩點之間最多只有一條邊,兩點間邊的代價,是兩個點的值的乘積,問所有邊的總代價是多少?


解題:

?? 簡單統(tǒng)計所有邊的代價是很簡單的,但是因為點數(shù)較多,這樣是會超時的??梢灶A先統(tǒng)計出所有點的總代價,所有關鍵點的總代價。計算時,將所有邊統(tǒng)計兩遍,其中非關鍵點較好處理,只要向兩邊連邊,而關鍵點向所有點連兩倍邊,關鍵點之間會有四條邊,要后期減去,而關鍵點和不是相鄰的非關鍵點之間就是兩倍邊,而關鍵點和相鄰的非關鍵點(如果是的話)是三倍邊,要減去一次。最后,統(tǒng)計完之后,將總代價除以2,即為所求。


代碼:

#include 
#include 
#define LL long long
#define sz 100005
using namespace std;
//每個點val
LL val[sz];
//是否是capital
bool vis[sz];
//哪些點是capital
int keyp[sz];
int main()
{
	int n,k,tmp;
	//sum1是所有點的和,sum2是capital的和
	LL sum1=0,ans=0,sum2=0,tmp2=0;
	scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
	{
		scanf("%lldn",&val[i]);
		sum1+=val[i];
	}
	for(int i=1;i<=k;i++)
	{
		scanf("%d",&tmp);
		vis[tmp]=1;
		keyp[i]=tmp;
		sum2+=val[tmp];
    }
	//首尾連接
	val[0]=val[n];
	vis[0]=vis[n];
	val[n+1]=val[1];
	vis[n+1]=vis[1];
	//所有邊計算兩次
    for(int i=1;i<=n;i++)
	{
	  //是capital那么它連出去的邊都計算兩次
	  if(vis[i])
	  {   
		  ans+=2*(sum1-val[i])*val[i];
		  //如果前一個點不是capital,那么要減去一次,因為非capital點也會向兩邊連邊
	      if(!vis[i-1])
			  ans-=val[i]*val[i-1];
		  if(!vis[i+1])
			  ans-=val[i]*val[i+1];
	  }
	  //非capital點向兩邊連邊
	  else
         ans+=val[i]*(val[i+1]+val[i-1]);
	}
	//關鍵點因為向所有點都連了兩次,關鍵點之間連了4次,故需減去2次
    for(int i=1;i<=k;i++)
	{
      tmp2+=(sum2-val[keyp[i]])*val[keyp[i]];
	}
	ans-=tmp2;
	//因為所有邊都統(tǒng)計了兩次
	printf("%lldn",ans/2);
	return 0;
}


本站聲明: 本文章由作者或相關機構授權發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權益,請及時聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關閉