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. BigDecimal空指针异常——个人应用

    背景: 将数据库统计的数据,封装成了两个BigDecimal,此时要将两个BigDecimal进行运算.其中有一个没有数据的话,会报null(不管null值在前在后) 先上解决: 我把数据库的数据进行 ...

  2. 〖Python〗-- Django的Form组件

    [Django的Form组件] Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 Form类的使 ...

  3. HTML|CSS之前端入门

    知识内容: 1.计算机网络综述 2.web基础 3.HTML与CSS介绍 4.JavaScript与jQuery介绍 一.计算机网络综述 1.什么是计算机网络 计算机网络是指将地理位置不同.具有独立功 ...

  4. nagios - 环境搭建

    ㈠ 公共服务监控 ㈡ 实现资源监控 ㈢ 图形化MRTG =============================== nagios-安装 安装前准备 创建用户和用户组 # groupadd -r n ...

  5. nginx、TP框架实现兼容pathinfo和rewrite两种url访问方式

    环境:centos7,yum安装的nginx1.10.php-fpm,tp3.2 本方法只需要配置nginx.conf的一个文件就可以支持pathinfo和rewrite两种url访问方式 vim / ...

  6. 温故而知新-正则单词和strlen

    1 正则表达式用\b表示单词的开始和结束 \bblog\b 正则查找blog这个单词 2 关于strlen的汉字问题 在utf8格式下  strlen('汉字')=6 在gbk格式下 strlen(' ...

  7. python正则表达式re库(自用)

    经典例子: 1.由26个字母组成的字符串 ^[A-Za-z]+$ 2. 中国境内邮政编码 [1-9]\d{5} 3.IP地址 0-99:[1-9]?\d 100-199:1\d{2} 200-249: ...

  8. leetcode743

    class Solution { public: int networkDelayTime(vector<vector<int>>& times, int N, int ...

  9. spring Ioc和DI

    spring的“控制反转”和“依赖注入”,个人看来是一个意思. 传统java程序中,使用一个对象的时候,都需要先new Object()创建一个新对象,才能使用.对象的控制权,在程序手里. 使用spr ...

  10. SQL 2012 分页取数据

    ,), data int ) select * from t1 row rows only create clustered index t1c on t1(id) declare @i int ) ...