Codeforces Round #365 (Div. 2) B 前缀和

时间:2021-07-06 04:39:11
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 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 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.
  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 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?

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 1
2 3 1 2
3
output
17
input
5 2
3 5 2 2 4
1 4
output
71
Note

This image describes first sample case:

Codeforces Round #365 (Div. 2) B 前缀和

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

This image describes second sample case:

Codeforces Round #365 (Div. 2) B 前缀和

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

题意:

n个城市 其中k个中心城市 每个城市都有一个权值c   k个中心城市编号按照升序给出

首先以1 — 2 — ... — n — 1路径串成一个环,其次中心城市到任意城市都有一条路.任意两个城市间最多有一条路

每条边的权值为相邻两个城市的权值的乘积。求所有边的权值的和。

题解:

1.处理成串的边的权值和

2.处理与中心城市相连的城市的边的权值和

3.去重(某条边的两段都为中心城市)

 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<queue>
#include<cmath>
#define ll __int64
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
int n,k;
ll a[];
int b[];
int used[];
ll zha[];
int main()
{
scanf("%d %d",&n,&k);
ll sum=;
ll ans=;
memset(used,,sizeof(used));
for(int i=;i<=n;i++)
{
scanf("%I64d",&a[i]);
sum+=a[i];
}
zha[]=;
for(int i=;i<=k;i++)
{
scanf("%d",&b[i]);
zha[i]=zha[i-]+a[b[i]];
used[b[i]]=;
}
for(int i=;i<n;i++)
ans=ans+a[i]*a[i+];
ans=ans+a[n]*a[];
for(int i=;i<=k;i++)
{
if(b[i]==)
{
ans=ans+a[]*(sum-a[]-a[]-a[n]);
continue;
}
if(b[i]==n)
{
ans=ans+a[n]*(sum-a[]-a[n]-a[n-]);
continue;
}
ans=ans+a[b[i]]*(sum-a[b[i]]-a[b[i]-]-a[b[i]+]);
}
for(int i=;i<=k;i++)
{
ll exm=zha[k]-zha[i];
if(b[i]==n)
break;
if(b[i]==)
{
if(used[])
exm=exm-a[];
if(used[n])
exm=exm-a[n];
ans=ans-exm*a[b[i]];
}
else
{
if(used[b[i]+])
exm=exm-a[b[i]+];
ans=ans-exm*a[b[i]];
}
}
printf("%I64d\n",ans);
}