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. K-means算法(理论+opencv实现)

    写在前面:之前想分类图像的时候有看过k-means算法,当时一知半解的去使用,不懂原理不懂使用规则...显然最后失败了,然后看了<机器学习>这本书对k-means算法有了理论的认识,现在通 ...

  2. uva-10344

    题意: 枚举23点,注意,数字也是可以枚举的,wa了一次 #include<stdio.h> #include<iostream> #include<sstream> ...

  3. CUDA C Programming Guide 在线教程学习笔记 Part 11

    ▶ 数学函数 ● 舍入函数,考虑被舍入参数有双精度浮点和单精度浮点,舍入方式有区别,舍入结果有整形.长整形和长长整形,所以共有以下舍入函数. // math_functions.h extern __ ...

  4. Latex 箭头、下标、符号上下写文字、正方形和三角形

    0. hat   $abc\hat def$  $ab\widehat{cde}f$ 1. 箭头上的文字    $\underrightarrow{\text{你的文字}}$  ($A \xlefta ...

  5. mysql常见问题解决方法.

    1. 问题:mysql启动报错(linux) [root@localhost ~]# service mysqld restart Another MySQL daemon already runni ...

  6. restful 注解 总结 (比较完整的):http://www.xuetimes.com/archives/388 , https://www.cnblogs.com/chen-lhx/p/5599806.html

    参考1:  http://www.xuetimes.com/archives/388 参考2:   https://www.cnblogs.com/chen-lhx/p/5599806.html 参考 ...

  7. 我对if(!this.IsPostBack)的理解

    if(!this.IsPostBack) { } 通常用在page_load中,获取一个值,该值指示该页是否正为响应客户端回发而加载,或者它是否正被首次加载和访问,如果是为响应客户端回发而加载该页,则 ...

  8. mysql 修改用户密码

    修改mysql用户密码   目录 mysqladmin命令 UPDATE user 语句 SET PASSWORD 语句 root密码丢失的情况(待验证) mysqladmin命令(回目录) 格式如下 ...

  9. ubuntu16.04安装tensorflow-gpu和cuda8.0加速训练

    转载请注明出处:http://www.cnblogs.com/buxizhizhoum/p/8086230.html 环境: 系统:ubuntu 16.04 cpu:i5 gpu:gt920m mem ...

  10. JSTL标签库学习记录1-c

    JSTL全称为JSP Standard Tag Library,即JSP标准标签库. 导入JSTL相关的JAR包,jstl.jar standard.jar 导入jstl标签库: <%@tagl ...