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. 环境部署(三):Linux下安装Git

    Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理,是目前使用范围最广的版本管理工具. 这篇博客,介绍下Linux下安装Git的步骤,仅供参考,当然,还是yum安装 ...

  2. windows使用.NET CORE下创建MVC,发布到linux运行

    1.在有dotnet core 的环境下,打开控制台.创建文件夹demo1 2.创建MVC程序 3.创建完成 4.使用记事本修改一下HomeController 修改端口 5.发布 6.压缩发布的文件 ...

  3. handsontable-chosen-editor

    https://github.com/mydea/handsontable-chosen-editor handsontable-chosen-editor是handsontable column的扩 ...

  4. Unity编辑器:自定义编辑器样式——GUIStyle

    通过GUIStyle,可以自定义Unity编辑器的样式. GUIStyle可以new一个全新的实例,这样,需要自己处理所有自己需要的效果. GUIStyle还可以基于已经存在的实例new一个新的实例, ...

  5. 实战Asp.Net Core:DI生命周期

    title: 实战Asp.Net Core:DI生命周期 date: 2018-11-30 21:54:52 --- 1.前言 Asp.Net Core 默认支持 DI(依赖注入) 软件设计模式,那使 ...

  6. GC调优入门笔记

    想给项目代码做做调优但有许多疑惑,比如有哪些参数要调.怎么调.使用什么工具.调优的效果如何定量测量等.发现Oracle的这份资料不错,简洁直接,回答了我的许多问题,给了许多很实用的大方向上的指导.将其 ...

  7. js中布尔值为false的六种情况

    下面6种值转化为布尔值时为false,其他转化都为true 1.undefined(未定义,找不到值时出现) 2.null(代表空值) 3.false(布尔值的false,字符串"false ...

  8. 009-定时关闭弹出广告窗口 By BoAi 20190414

    ;~ 定时关闭弹出广告窗口 By BoAi 20190414 ; ### 参数设置段 ######################################SingleInstance,forc ...

  9. codeforces#1090 D. New Year and the Permutation Concatenation(打表找规律)

    题意:给出一个n,生成n的所有全排列,将他们按顺序前后拼接在一起组成一个新的序列,问有多少个长度为n的连续的子序列和为(n+1)*n/2 题解:由于只有一个输入,第一感觉就是打表找规律,虽然表打出来了 ...

  10. 最短路问题 Floyd+Dijkstra+SPFA

    参考博客:https://blog.csdn.net/qq_35644234/article/details/60875818 题目来源:http://acm.hdu.edu.cn/showprobl ...