B. Mishka and trip
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

Little Mishka is a great traveller and she visited many countries. After thinking about where to travel this time, she chose XXX — beautiful, but little-known northern country.

Here are some interesting facts about XXX:

  1. XXX consists of n cities, k of whose (just imagine!) are capital cities.
  2. All of cities in the country are beautiful, but each is beautiful in its own way. Beauty value of i-th city equals to ci.
  3. All the cities are consecutively connected by the roads, including 1-st and n-th city, forming a cyclic route 1 — 2 — ... — n — 1. Formally, for every 1 ≤ i < n there is a road between i-th and i + 1-th city, and another one between 1-st and n-th city.
  4. Each capital city is connected with each other city directly by the roads. Formally, if city x is a capital city, then for every 1 ≤ i ≤ n,  i ≠ x, there is a road between cities x and i.
  5. There is at most one road between any two cities.
  6. Price of passing a road directly depends on beauty values of cities it connects. Thus if there is a road between cities i and j, price of passing it equals ci·cj.

Mishka started to gather her things for a trip, but didn't still decide which route to follow and thus she asked you to help her determine summary price of passing each of the roads in XXX. Formally, for every pair of cities a and b (a < b), such that there is a road between a and b you are to find sum of products ca·cb. Will you help her?

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.

Examples
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.

题目连接:http://codeforces.com/contest/703/problem/B


题意:n个城市,每个城市之间最多有一条道路直接相连。 1 — 2 — ... — n — 1之间有一条道路。其中k个城市是首都,首都与其他城市之间都有道路直接相连。输出这个城市所有道路的权值。

思路:n个城市的权值总和为sum,每个首都与其他城市之间的道路权值为c[i]*(sum-c[i-1]-c[i+1]-cou)。其中cou为c[i]之前出现过的首都的权值总和。考虑c[i-1]与c[i+1]在i=1和i=n的情况和cou里面包括了c[i-1]和c[i+1]的情况。

代码:

#include<bits/stdc++.h>
using namespace std;
__int64 c[];
int d[];
int sign[];
int main()
{
int i,n,k;
scanf("%d%d",&n,&k);
__int64 ans=,sum=;
for(i=; i<=n; i++)
{
scanf("%I64d",&c[i]);
if(i>) ans+=(c[i-]*c[i]);
sum+=c[i];
}
ans+=(c[n]*c[]);
__int64 cou=;
memset(sign,,sizeof(sign));
for(i=; i<k; i++)
{
scanf("%d",&d[i]);
__int64 flag=;
if(d[i]>)
{
if(sign[d[i]-]==)
flag+=c[d[i]-];
}
else
{
if(sign[n]==)
flag+=c[n];
}
if(d[i]<n)
{
if(sign[d[i]+]==)
flag+=c[d[i]+];
}
else
{
if(sign[]==)
flag+=c[];
}
//cout<<c[d[i]]<<" "<<flag<<" "<<cou<<endl;
ans+=c[d[i]]*(sum-flag-cou-c[d[i]]);
cou+=c[d[i]];
sign[d[i]]=;
}
printf("%I64d\n",ans);
return ;
}

模拟

Codeforces 703B. Mishka and trip 模拟的更多相关文章

  1. CodeForces 703B Mishka and trip

    简单题. 先把环上的贡献都计算好.然后再计算每一个$capital$ $city$额外做出的贡献值. 假设$A$城市为$capital$ $city$,那么$A$城市做出的额外贡献:$A$城市左边城市 ...

  2. CodeForces 703A Mishka and trip

    Description Little Mishka is a great traveller and she visited many countries. After thinking about ...

  3. codeforces 703B B. Mishka and trip(数学)

    题目链接: B. Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input stan ...

  4. Codeforces Round #365 (Div. 2) Mishka and trip

    Mishka and trip 题意: 有n个城市,第i个城市与第i+1个城市相连,他们边的权值等于i的美丽度*i+1的美丽度,有k个首都城市,一个首都城市与每个城市都相连,求所有边的权值. 题解: ...

  5. cf703B Mishka and trip

    B. Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input  standard ...

  6. 暑假练习赛 003 F Mishka and trip

    F - Mishka and trip Sample Output   Hint In the first sample test: In Peter's first test, there's on ...

  7. codeforces 703E Mishka and Divisors

    codeforces 703E Mishka and Divisors 题面 给出大小为\(1000\)的数组和一个数\(k\),求长度最短的一个子序列使得子序列的元素之积是\(k\)的倍数,如果有多 ...

  8. CodeForces.158A Next Round (水模拟)

    CodeForces.158A Next Round (水模拟) 题意分析 校赛水题的英文版,坑点就是要求为正数. 代码总览 #include <iostream> #include &l ...

  9. Codeforces 703D Mishka and Interesting sum 离线+树状数组

    链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...

随机推荐

  1. JS 更新

    JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScript提交给国际标准化组织ECM ...

  2. C宏定义

    宏定义中宏名一般用大写,用以和一般的变量名区分开来,但是用宏名用小写也没有错; 对程序中用双引号括起来的字符串内的字符,不进行宏替换操作, #include<stdio.h> #defin ...

  3. ASP.NET CMS: Administration Template

    ASP.NET CMS: Administration Template For many creating advanced ASP.NET website or application admin ...

  4. mysql开启查询日志功能

    1.开启查询日志  https://www.cnblogs.com/kerrycode/p/7130403.html MYsql 查询日志配置    mysql> show variables ...

  5. 62. 用流程自带的打印功能,IE浏览器打印出来是空白

    用流程自带的打印功能,IE浏览器打印出来是空白的这个问题确认是由于IE启用了兼容模式导致的了把IE的兼容模式关掉就行了

  6. Star打印机数据解密

    通过串口调试工具 抓取到的16进制文本; 如下 然后打开我们的文档,查看命令数据内容. 详情请密我QQ:1161588342  说明加好友原因

  7. jetty异常

    异常一: java.net.BindException: Address already in use: bind jvm 1 | 2017-10-18 15:08:10,792+0800 WARN ...

  8. 基于OpenGL编写一个简易的2D渲染框架-12 重构渲染器-BlockAllocator

    BlockAllocator 的内存管理情况可以用下图表示 整体思路是,先分配一大块内存 Chunk,然后将 Chunk 分割成小块 Block.由于 Block 是链表的一个结点,所以可以通过链表的 ...

  9. linux c++下载http文件并显示进度<转>

    #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <net ...

  10. 7 python 模块间相互导入

    python在不同层级目录import模块的方法 注意,在python3里,即使目录下没__int__.py文件也能创建成功,猜应该是解释器优化所致,但创建包还是要记得加上这个文件 吧. 1.模块的分 ...