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. 2.精通前端系列技术之JavaScript模块化开发 seajs(一)

    在使用seajs模块化开发之前,直接在页面引用js会容易出现冲突及依赖相关的问题,具体问题如下 问题1:多人开发脚本的时候容易产生冲突(比如全局参数冲突,方法名冲突),可以使用命名空间降低冲突,不能完 ...

  2. Echarts 地图控件tooltip多行显示

    直接上代码 var o = { "tooltip": { trigger: 'item', "formatter": function (params) { v ...

  3. Android调用Sqlite数据库时自动生成db-journal文件的原因

    数据库为了更好实现数据的安全性,一半都会有一个Log文件方便数据库出现意外时进行恢复操作等.Sqlite虽然是一个单文件数据库,但麻雀虽小五脏俱全,它也会有相应的安全机制存在 这个journal文件便 ...

  4. hdu 1035 (usage of sentinel, proper utilization of switch and goto to make code neat) 分类: hdoj 2015-06-16 12:33 28人阅读 评论(0) 收藏

    as Scott Meyers said in his book Effective STL, "My advice on choosing among the sorting algori ...

  5. 【转发】linux文件系统变为只读的修复

    详细解决方法:http://smartmontools.sourceforge.net/badblockhowto.html 相关问题,更换硬盘:http://blog.chinaunix.net/u ...

  6. config、make、make install

    .config/    .configure  (查看该目录下是否有这个文件,如果有makefile,可直接make)  配置 config是一个shell脚本,根据平台的特性生成Makefile文件 ...

  7. Java基础毕向东day02

    1. 常量 null 等特殊 2.标识符 数字-字母-下划线,数字不能开头 3.二进制 1> 二进制计算方法. 2>常用二进制. 1        1      0       0   1 ...

  8. 2016年3月AV评测

  9. ubuntu下openoffice开发环境配置

    安装openoffice或者liboffice 路径:/usr/lib/openoffice/program ,soffice 开启服务: 安装JDK 其默认路径:jdk7 版本号:1.7...,jd ...

  10. 2016 - 1- 22 HTTP(三)

    一: iOS中发送HTTP请求的方案 三: HTTP通信过程  1.请求 1.1 HTTP协议规定,一个完整的有客户端发给服务器的请求应包含以下内容: 1.1.1 请求头: 包含了对客户端环境的描述与 ...