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. 给ubuntu换内核

    本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 下载内核源码 有两种方式,一种方式是直接从官网:https://www.kernel.org/直接下载,另一种方 ...

  2. keystone系列四:keystone部署及操作

    一 前言 任何软件的部署都是没有技术含量的,任何就部署讲部署的人都是江湖骗子. 部署的本质就是拷贝,粘贴,回车.我们家养了条狗,它可以胜任这件事情. 我们搞技术的,一定不能迂腐:轻信或者一概不信. 轻 ...

  3. DIV CSS float浮动

    一.浮动? #CSS样式的float浮动属性,用于设置标签对象(如:<div>标签盒子.<span>.<a>.等html标签)的浮动布局. #通过定义浮动(floa ...

  4. [Spark][kafka]kafka 生产者,消费者 互动例子

    [Spark][kafka]kafka 生产者,消费者 互动例子 # pwd/usr/local/kafka_2.11-0.10.0.1/bin 创建topic:# ./kafka-topics.sh ...

  5. TMS-规划图

    规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 规划图 ...

  6. JSP报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    今天使用Eclipse+Maven建立了一个Javaweb工程,并在eclipse中配置了Web容器Jboss eap 6.2.新建jsp页面,添加一个简单 的Java类.可是,JSP页面顶端出现“红 ...

  7. mybatis百科-结果集映射类ResultMap

    目录 1 成员变量 2 构造函数 3 其他函数 3.1 setter 和 getter 函数 4 静态内部类 4.1 成员变量 4.2 构造函数 4.3 建造者相关的函数 4.4 获取配置的构造方法参 ...

  8. VUE工程上线首页加载慢问题优化

    使用webpack-bundle-analyzer工具 下面介绍几种压缩文件的方式. 1.vue-router懒加载 2.工程文件打包的时候不生成.map文件 3.gzip压缩 4.CDN 5.VUE ...

  9. react虚拟dom diff算法

    react虚拟dom:依据diff算法 前端:更新状态.更新视图:所以前端页面的性能问题主要是由Dom操作引起的,解放Dom操作复杂性 刻不容缓 因为:Dom渲染慢,而JS解析编译相对非常非常非常快! ...

  10. Thrift序列化与反序列化

    Thrift序列化与反序列化的实现机制分析 Thrift是如何实现序死化与反序列化的,在IDL文件中,更改IDL文件中的变量序号或者[使用默认序号的情况下,新增变量时,将新增的变量不放在IDL文件的结 ...