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 route1 — 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 every1 ≤ 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 andj, 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.

题意:

n个城市 其中k个中心城市 每个城市都有一个权值c   k个中心城市编号按照升序给出

首先以1 — 2 — ... — n — 1路径串成一个环,其次中心城市到任意城市都有一条路.任意两个城市间最多有一条路

每条边的权值为相邻两个城市的权值的乘积。求所有边的权值的和。

题解:

1.处理成串的边的权值和

2.处理与中心城市相连的城市的边的权值和

3.去重(某条边的两段都为中心城市)

 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<queue>
#include<cmath>
#define ll __int64
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
int n,k;
ll a[];
int b[];
int used[];
ll zha[];
int main()
{
scanf("%d %d",&n,&k);
ll sum=;
ll ans=;
memset(used,,sizeof(used));
for(int i=;i<=n;i++)
{
scanf("%I64d",&a[i]);
sum+=a[i];
}
zha[]=;
for(int i=;i<=k;i++)
{
scanf("%d",&b[i]);
zha[i]=zha[i-]+a[b[i]];
used[b[i]]=;
}
for(int i=;i<n;i++)
ans=ans+a[i]*a[i+];
ans=ans+a[n]*a[];
for(int i=;i<=k;i++)
{
if(b[i]==)
{
ans=ans+a[]*(sum-a[]-a[]-a[n]);
continue;
}
if(b[i]==n)
{
ans=ans+a[n]*(sum-a[]-a[n]-a[n-]);
continue;
}
ans=ans+a[b[i]]*(sum-a[b[i]]-a[b[i]-]-a[b[i]+]);
}
for(int i=;i<=k;i++)
{
ll exm=zha[k]-zha[i];
if(b[i]==n)
break;
if(b[i]==)
{
if(used[])
exm=exm-a[];
if(used[n])
exm=exm-a[n];
ans=ans-exm*a[b[i]];
}
else
{
if(used[b[i]+])
exm=exm-a[b[i]+];
ans=ans-exm*a[b[i]];
}
}
printf("%I64d\n",ans);
}

Codeforces Round #365 (Div. 2) B 前缀和的更多相关文章

  1. Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点

    // Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...

  2. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)

    题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...

  3. Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)

    题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...

  4. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树

    题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...

  5. Codeforces Round #365 (Div. 2) E - Mishka and Divisors(转化成01-背包)

    http://codeforces.com/contest/703/problem/E 题意: 给出n个数和一个k,计算出至少要多少个数相乘才是k的倍数. 思路:这道题目参考了杭电大神的代码http: ...

  6. Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)

    http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...

  7. Mishka and Divisors[CodeForces Round #365 Div.2]

    http://codeforces.com/contest/703/problem/E 题意:给定一个最多个数的序列,从中选出最少个数的数字,使得他们的乘积是k的倍数,若有多种选择方式,输出选出数字和 ...

  8. Codeforces Round #365 (Div. 2) D 树状数组+离线处理

    D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...

  9. Codeforces Round #365 (Div. 2) A 水

    A. Mishka and Game time limit per test 1 second memory limit per test 256 megabytes input standard i ...

随机推荐

  1. Android 解压html压缩数据

    public static String unzipHTML(String s){ int endPos = s.indexOf("\r\n\r\n"); if(endPos< ...

  2. libpng交叉编译安装

    tar xzf libpng-1.5.22.tar.gz cd libpng-1.5.22 mkdir tmp 打开Makefile文件并修改CC=arm-linux-gcc ./configure ...

  3. 文件浏览器及数码相框 -2.3.1freetype_pc

    例子 #include <stdio.h> #include <string.h> #include <math.h> #include <ft2build. ...

  4. WP8.1 Study2:MapControl控件的应用

    总的界面布局如下:

  5. mybatis 的 resulttype 和resultMap

    resultType适合返回值比较简单的,比如一个数据类型,或者一个对象.比如对象的情况,是将表的列名和对象的属性一一对应的. 但是resultType无法处理返回值比较复杂的,特别是连接查询,需要用 ...

  6. 命令行BASH

    shell 壳,把用户的指令翻译给内核kernel,真正工作的是内核 shell分为cli(command line interface)和gui(graphical user interface) ...

  7. c#图像处理入门(-bitmap类和图像像素值获取方法) 转

    一.Bitmap类 Bitmap对象封装了GDI+中的一个位图,此位图由图形图像及其属性的像素数据组成.因此Bitmap是用于处理由像素数据定义的图像的对象.该类的主要方法和属性如下: 1. GetP ...

  8. ACM -二分图题目小结

    暂时只包括与最大匹配相关的问题. 求最大独立集,最小路径覆盖等等大多数题目都可以转化为求最大匹配用匈牙利算法解决. 1.最大匹配(边集) 此类问题最直接,直接用匈牙利算法即可. HDU 2063  过 ...

  9. PAT 06-2 字符串字母大小写转换

    没什么好说的,记得使用ctype.h就好了,谭浩强那本书就介绍了,再不使用就太对不起他老人家了:有一点小小的地方需要注意一下,&&的优先级比=号高,所以getchar()两边没有括号的 ...

  10. 2016年3月AV评测