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. dos批处理文件中的变量小结

    很多情况下我们需要用到一些批处理文件中的变量,方便我们执行一些操作,这里简单整理下,方便需要的朋友   批处理中的变量,我把他分为两类,分别为"系统变量"和"自定义变量& ...

  2. 一,Android Studio笔记

    转自:https://developer.android.com/studio/intro/index.html 一.界面 Android Studio 主窗口由图 3 标注的几个逻辑区域组成. 工具 ...

  3. C# 程序A发送Log记录给程序B,程序B处理和分析Log记录

    C# 程序A发送Log记录给程序B,程序B处理和分析Log记录 关键字:C# ;Log记录 ;在线Log记录;Socket:httplistener 一.常用场景 1. APP开发,在真机或者虚拟机上 ...

  4. uva-301-枚举-组合

    题意:从A市到B市有n个站点,限制火车上搭乘的乘客数目,每个站点都从有一些乘车的订单,订单信息 从x到y,乘客m人,求解最大的收入是多少 最多7个站,22个订单 选取订单的时候没有顺序问题,所以不是全 ...

  5. 2k8 32bit下载

    Windows Server 2008 32-bit Standard(标准版) Windows Server 2008 32-bit Enterprise(企业版) Windows Server 2 ...

  6. python入门-类(一)

    1 最简单的一个类 class Dog(): """一次模拟小狗的简单尝试""" def __init__(self,name,age): ...

  7. python入门-WHILE循环

    1 使用while循环 current_number= : print(current_number) current_number += 2 让用户选择退出 prompt = "tell ...

  8. easyui datagrid列使用按钮的一些心得(转)

    http://blog.csdn.net/sskicgah/article/details/16939959 以前,用easyui的datagrid,有时候会用到一些操作选项,比如代码如下: $('# ...

  9. ASP.Net在web.config中设置上传文件的大小方法

    修改Webcong文件:<system.web><httpRuntime maxRequestLength="40960"   //即40MB,1KB=1024u ...

  10. 使用seaborn制图(箱型图)

    import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # 设置风格, ...