Mishka and trip

CodeForces - 703B

小米什卡是一个伟大的旅行者,她访问了许多国家。在这次考虑去哪里旅行之后,她选择了XXX--这个美丽,但鲜为人知的北方国家。

以下是关于XXX的一些有趣事实:

  • XXX由n个城市组成,其中k个(只是想象!)是省会城市。

  • 这个国家的所有城市都很漂亮,但每个城市都很独特。第i个城市的美丽值等于ci。

  • 所有城市通过道路连续连接,包括第1和第n个城市,形成循环路线1 - 2 - … - n - 1

  • 每个省会城市都直接与其他所有城市相连。

  • 任何两个城市之间最多只有一条公路。

  • 通过道路的价格直接取决于它所连接的城市的美丽值。因此,如果城市i和j之间存在道路,则通过它的价格等于ci·cj。

    米什卡开始收集她的东西去旅行,但是还没有决定走哪条路线,因此她请求你帮助她确定通过XXX中每条道路的总价格。你会帮她吗?

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.

Example

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:

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.

sol:比较坑,先O(n)扫一遍求出无省会城市的边权和,然后计算每个省会城市的所有边权和,但这样两个省会城市之间的边权会被统计两次,要把他们去掉
Ps:要小心,把所有相邻的都减掉
/*
一组hack数据
input
3 3
1 1 1
1 2 3
output
3
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,K,Shengh[N];
ll Cost[N];
int main()
{
int i;
ll Sum=,SS=,ans=;
R(n); R(K);
for(i=;i<=n;i++)
{
Sum+=(Cost[i]=read());
if(i>=) ans+=Cost[i]*Cost[i-];
}
ans+=Cost[]*Cost[n];
for(i=;i<=K;i++)
{
SS+=Cost[Shengh[i]=read()];
ans+=Cost[Shengh[i]]*(Sum-Cost[(Shengh[i]-+n)%n+]-Cost[Shengh[i]%n+]-Cost[Shengh[i]]);
}
sort(Shengh+,Shengh+K+);
for(i=;i<K;i++)
{
ll oo=(SS-=Cost[Shengh[i]]);
if(Shengh[i+]==Shengh[i]+) oo-=Cost[Shengh[i+]];
if(Shengh[K]==(Shengh[i]-+n)%n+) oo-=Cost[Shengh[K]];
ans-=Cost[Shengh[i]]*oo;
}
Wl(ans);
return ;
}
/*
input
4 1
2 3 1 2
3
output
17 input
5 2
3 5 2 2 4
1 4
output
71 input
3 3
1 1 1
1 2 3
output
3
*/
 

codeforces703B的更多相关文章

  1. Codeforces703B Mishka and trip

    题意: 就是有n个点,本来相邻点之间就有一条边,1和n之间也有一条,然后给你几个特殊点,说这些特殊点和其他所有点都连起来了,然后算一个所有边的权值和,每条边的权值等于两个点的c相乘. 思路: 水题啊- ...

随机推荐

  1. B+Tree原理及mysql的索引分析

    一.索引的本质 MySQL官方对索引的定义为:索引(Index)是帮助MySQL高效获取数据的数据结构.提取句子主干,就可以得到索引的本质:索引是数据结构. 我们知道,数据库查询是数据库的最主要功能之 ...

  2. redis学习(六)——Sorted Set数据类型

    一.概述: Sorted Set(有序集合)和Set类型极为相似,它们都是字符串的集合,都不允许重复的成员出现在一个Set中.它们之间的主要差别是Sorted Set中的每一个成员都会有一个分数(sc ...

  3. 【php增删改查实例】第二十六节 - 个人详情页制作

    在一般的系统中,当用户点击头像的时候,就会跳转到对应的个人详情页,在这个页面,他可以查看和修改自己的个人信息,或者更换头像. 本案例中,个人详情页使用bootstrap框架. 首先,我们新建一个htm ...

  4. xxtea---单片机数据加密算法

    转:https://www.cnblogs.com/LittleTiger/p/4384741.html 各位大侠在做数据传输时,有没有考虑过把数据加密起来进行传输,若在串口或者无线中把所要传的数据加 ...

  5. 初窥RabbitMQ消息中间及SpringBoot整合

    一:RabbitMQ简介 RabbitMQ介绍 RabbitMQ 即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用. 消息中间件最主要的作用是解耦,中间件最标准 ...

  6. ReactJs移动端兼容问题汇总

    汽车H5使用ReactJs问题汇总 Q:安卓4.4webview显示空白? A:初步怀疑是css属性没有加前缀引发的兼容问题,但添加后发现也不行,通过webview调试后控制台输出Set is und ...

  7. 使用 OpenSSL 创建私有 CA:3 用户证书

    OpenSSL 创建私有 CA 三部曲:使用 OpenSSL 创建私有 CA:1 根证书使用 OpenSSL 创建私有 CA:2 中间证书使用 OpenSSL 创建私有 CA:3 用户证书 在前文&l ...

  8. Centos7 ssh配置RSA证书登录

    修改sshd配置文件 vim /etc/ssh/sshd_config #增加以下三项 RSAAuthentication yes PubkeyAuthentication yes Authorize ...

  9. 20分钟 看图手写的table

    <html><body><table width="100%" border="1" cellspacing="0&qu ...

  10. HDFS的命令

    .....Hdfs dfs -cat path hadoop fs - 等同 1 -ls 查看当前目录的文件和文件夹 2 -lsr 递归查看 3 -du 查看文件的大小 4-dus 查看文件夹中所有的 ...