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 12 3 1 23

Output
17

Input
5 23 5 2 2 41 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=0;bool f=0;char ch=' ';while(!isdigit(ch)){f|=(ch=='-'); ch=getchar();}while(isdigit(ch)){s=(s<<3)+(s<<1)+(ch^48); ch=getchar();}return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{if(x<0){putchar('-'); x=-x;}if(x<10){putchar(x+'0'); return;}write(x/10);putchar((x%10)+'0');return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=100005;
int n,K,Shengh[N];
ll Cost[N];
int main()
{int i;ll Sum=0,SS=0,ans=0;R(n); R(K);for(i=1;i<=n;i++){Sum+=(Cost[i]=read());if(i>=2) ans+=Cost[i]*Cost[i-1];}ans+=Cost[1]*Cost[n];for(i=1;i<=K;i++){SS+=Cost[Shengh[i]=read()];ans+=Cost[Shengh[i]]*(Sum-Cost[(Shengh[i]-2+n)%n+1]-Cost[Shengh[i]%n+1]-Cost[Shengh[i]]);}sort(Shengh+1,Shengh+K+1);for(i=1;i<K;i++){ll oo=(SS-=Cost[Shengh[i]]);if(Shengh[i+1]==Shengh[i]+1) oo-=Cost[Shengh[i+1]];if(Shengh[K]==(Shengh[i]-2+n)%n+1) oo-=Cost[Shengh[K]];ans-=Cost[Shengh[i]]*oo;}Wl(ans);return 0;
}
/*
input
4 1
2 3 1 2
3
output
17input
5 2
3 5 2 2 4
1 4
output
71input
3 3
1 1 1
1 2 3
output
3
*/

View Code

 

转载于:https://www.cnblogs.com/gaojunonly1/p/10632767.html

codeforces703B相关推荐

  1. 安卓仿苹果音量调节_android自定义view仿照MIUI中音量控制效果

    先看效果图: 这就是miui中的音量效果图,实现思路是自定义视图,绘制圆环,然后设置进度显示. 核心代码在onDraw中实现如下: @Override protected void onDraw(Ca ...

最新文章

  1. linux挂载media装服务,CentOS8服务器入门系列教程(五):Linux挂载光盘、yum安装软件包...
  2. linux 中文输入法 xshall,Centos6.7下安装ibus中文输入法
  3. 全球最大AI巨量模型,参数2457亿炼丹16天最懂中文
  4. 大数据的起步:初学者
  5. sublime text 2中Emmet8个常用的技巧
  6. Delphi数据类型
  7. 并行程序设计模式--Master-Worker模式
  8. 广州运营开放式数据交易平台发力大数据业务
  9. Dentry negativity/negative dentry
  10. 【Foreign】字串变化 [DP]
  11. centos7安装源疯了_Jenkins 在 Centos7 上安装(使用国内源)
  12. word文档计算机基础考试,计算机基础考试题库(含答案)48924
  13. mysql数据库置疑,sqlserver2000数据库置疑的解决方法
  14. Kali Linux系统apt源配置
  15. Intellij IDEA 强大的搜索能力
  16. python读Excel数据成numpy数组
  17. 关于安卓视频加水印与剪辑功能实现
  18. oracle序列号、
  19. 杰理之统一音量控制【篇】
  20. 【对讲机的那点事】“驴途”不是“囧途”,安全出游请务必配备上对讲机!

热门文章

  1. CVPR 2022|U2PL:使用不可靠伪标签的半监督语义分割
  2. 薅羊毛 | Python 自动化带你轻松赚钱(完结版)
  3. java合成海报的工具类(二维码、头像、海报)
  4. Layui 弹出层选择数据实现
  5. Oracle EBS Interface/API(25) - Lookups API
  6. 大数据:Shell的操作(2)
  7. cursor常用的属性值
  8. HTML5 代码规范
  9. macbook如何使用visual studio code进行c语言编程
  10. [环保] 为了反击无耻的两桶油雇来的砖家,三伏天,我一个月开车不开空调测油耗