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. .CBB 文件 如何打开

    CCB Visual Basic动态按钮配置文件 用SQL server,导入时选择 access,打开后即可.

  2. Executor框架(五)Executors工厂类

    Executors 简介 Executors 是一个工厂类,其提供的是Executor.ExecutorService.ScheduledExecutorService.ThreadFactory 和 ...

  3. Python自定义状态码枚举类

    在Java里很容易做到自定义有状态码和状态说明的枚举类例如: public enum MyStatus { NOT_FOUND(404, "Required resource is not ...

  4. python urlretrieve 下载图片

    python 3中urlretrieve方法直接将远程数据下载到本地.为什么不行? 55 import re import urllib.request def getHtml(url): page ...

  5. java 调用短信 api 接口发送短信

    参考:   https://blog.csdn.net/u014793522/article/details/59062014 参考 :https://blog.csdn.net/Lu_shilusi ...

  6. Spring Boot @Trasactionl 失效, JDK,CGLIB动态代理

    来自: https://www.cnblogs.com/sweetchildomine/p/6978037.html?utm_source=itdadao&utm_medium=referra ...

  7. C# 事件 event 【转】

    C#事件(event)解析   事件(event),这个词儿对于初学者来说,往往总是显得有些神秘,不易弄懂.而这些东西却往往又是编程中常用且非常重要的东西.大家都知道windows消息处理机制的重要, ...

  8. c++ vector, 迭代器

    现代c++尽量使用vector(容器)和迭代器(相当于指针),少使用数组和指针,除非对程序执行效率有很高的要求. 容器优点,易于扩展,可通过push_back方法动态添加元素,数组不能动态添加元素. ...

  9. 没有装delphi软件则须修改程序的Uses单元,去掉QDialogs qtintf.dll

    转: 花了几天的功夫,终于完成了一个delphi调用webservice(C#)的任务,发现了好多问题,不过还是解决了,和大家分享一下.首先,就是调用时一个问题,如果你的webservice没有数据库 ...

  10. redis启动.停止.重启

    Linux下安装 ]# wget http://download.redis.io/releases/redis-2.8.17.tar.gz ]# tar xzf redis-2.8.17.tar.g ...