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. Android测试(三):本地单元测试

    原文:https://developer.android.com/training/testing/unit-testing/local-unit-tests.html 如果你的单元测试没有依赖或者只 ...

  2. Generative Adversarial Nets[Introduction]

    0. 背景 通过阅读书籍<Pro Deep Learning with TensorFlow: A Mathematical Approach to Advanced Artificial In ...

  3. ORACLE存储过程中%TYPE和%ROWTYPE的区别

    ORACLE存储过程中%TYPE和%ROWTYPE的区别 在存储过程中%TYPE和%ROWTYPE常用来在PL/SQL中定义变量 因为 t_emp emp%rowtype ;这个语句的意思是 定义一个 ...

  4. .Net高级进阶,教你如何构建企业模型数据拦截层,动态控制字段验证

    现在,你有一个MVC架构的web项目,你要完成一个注册功能. 前台传了3个值到你的控制器,分别是账号.密码.邮箱. 如图:现在你要在控制器里面判断,账号名称.密码.邮箱不能为空,并且名称和密码不超过1 ...

  5. 移动H5页面微信支付踩坑之旅(微信支付、单页面路由模拟、按钮加锁、轮询等常见功能)

    开发背景: .net混合开发的vue模板语法的单页面应用,所以不存在脚手架以及没有路由可以跳转. 项目描述: 需要写两个页面,在订单详情页需要点击“请输入手机号”进入手机号绑定页面,手机号绑定成功后自 ...

  6. Wannafly summer camp Day6 - D 区间权值

    这道题实在是不该,我在化式子的时候,多此一举,把式子进行累加,导致自己当时化的式子是错的,这样导致自己卡了很久,也没想到好的思路,赛后重新分析一波,感觉巨™简单...难受的一逼. 这道题的关键在于,W ...

  7. Five Dimensional Points CodeForces - 851C (计算几何+暴力)

      C. Five Dimensional Points time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. Redis服务端的搭建(初级)

    前方低能,仅适合入门级菜鸟阅读,大神大牛通通闪开! 前言:redis经常被用来做缓存(原因自行科普),基于学习的需要自己搭建了一个redis服务器,考虑到项目的分布式部署,所以前期开始的时候,redi ...

  9. HDU 2001 计算两点间的距离

    http://acm.hdu.edu.cn/showproblem.php?pid=2001 Problem Description 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离 ...

  10. PHP的优化建议(仅借鉴)

    转载: https://www.awaimai.com/1050.html 1 字符串 1.1 少用正则表达式 能用PHP内部字符串操作函数的情况下,尽量用他们,不要用正则表达式, 因为其效率高于正则 ...