Codeforces Round #365 (Div. 2) B

Description

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 aand 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 12 3 1 23

output

17

input

5 23 5 2 2 41 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.

题意:给你一些城市,有些城市是省城,与其他城市都会相连。一般的城市就是 1 — 2 — ... — n — 1这种连接方式,求连接城市相乘积的和。

解法:为了防止重复计算,首先考虑的就是省城的连接,我们先保存所有连接点的和,然后就是 a(省城)*(sum-a(省城)),每计算一次,sum就减一次省城的权值

接下来再考虑一般城市连接,已经计算过的不去考虑,注意一下1和n的情况

#include <bits/stdc++.h>
using namespace std;
int n,m;
long long q[100005];
long long p[100005];
long long ans1[100005],ans2[100005];
map<int,int>c;
set<long long>e;
long long sum=0,ans=0;
long long cot;
int main()
{
    cin>>n>>m;
    int a,b;

    for(int i=1; i<=n; i++)
    {
        cin>>q[i];
        sum+=q[i];
    }
    for(int i=1; i<=m; i++)
    {
        cin>>p[i];
        c[p[i]]=1;
    }
    if(c[1]==0&&c[n]==0)
    {
        ans+=(q[1]*q[n]);
    }
    for(int i=1;i<=m;i++)
    {
         sum-=q[p[i]];
         ans+=(q[p[i]]*sum);
    }
    for(int i=1;i<n;i++)
    {
        if(c[i]==0&&c[i+1]==0)
        {
            ans+=(q[i]*q[i+1]);
        }
    }

    cout<<ans<<endl;
    return 0;
}

  

时间: 2024-11-16 13:24:09

Codeforces Round #365 (Div. 2) B的相关文章

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

http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的性质,在一个区间中,我们如果把所有数字都异或的话,可以发现最后偶数次的数字异或后都变成了0,只剩下了奇数次的数字异或. 举个例子,{1,2,3,2,3,5} 异或和是1^2^3^2^3^5=1^5 因为最后要计算偶数次数字的异或和,那么最后我们只需要再异或上该区间内所有不同数字即可. 那么我们可以先

Codeforces Round #365 (Div. 2) C

链接:http://codeforces.com/problemset/problem/703/C 分析:  这题其实是个大水题,只要短短的几行就可以搞定的, 首先需要直觉判断一点,就是只要判断最左边的点和最下面的点 以及和他们相邻边的右边的点 第二部我们就需要判断人与这两个边有没有交点,如果人与上面的有交点,那么人肯定就需要在下面的那条边等待  等待的时间是 max(x - y*1.0*v/u) 那么我们所需要做的还有一步就是判断就是需不需要等待 ,当min(x - y*1.0*v/u) >=

Codeforces Round #365 (Div. 2) A

Description Mishka is a little polar bear. As known, little bears loves spending their free time playing dice for chocolates. Once in a wonderful sunny morning, walking around blocks of ice, Mishka met her friend Chris, and they started playing the g

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

题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和Xor[i],表示1~i的xor和.因为num^num=0,所以Xor[r] ^ Xor[l - 1]求的是l~r之间出现奇数次的数字xor和. 那怎么求偶数次的呢,那我们可以先求l到r之间不重复出现数字的xor(比如1 1 2 求的是1 ^ 2),然后再xor以上求出的Xor[r] ^ Xor[l

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

D. Mishka and Interesting sum 链接: http://codeforces.com/problemset/problem/703/D 题意: 给一个序列 每次询问一个区间 求区间中出现次数为偶数次的数的异或和 代码: 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<map> 5 using namespace std; 6 7 str

Codeforces Round #365 (Div. 2)

5/5 这场CF有毒啊!!好在不计rating(/▽╲) 题A Mishka and Game 题意:略 题解:略 题B Mishka and trip 题意:给你一个图,告诉你一开始围成一个环,然后每个点都有自己的权值,然后两个点的路径权值为这两个点的乘积,然后告诉你某些点是中心点,然后问你权值之和. 题解:这题有点意思,第一题太水了,反而觉得不太好. (1)先算出对于那些中心点的所有权值,这样会重复,画图发现重了的地方就是多算了一次这些中心点形成的完全图,所以只要(2)减去这些中心点形成的完

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

D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes input standard input output standard output Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her wit

Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum

题目链接:传送门 题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和 题目思路:前缀和+离线处理+树状数组 首先可以知道, l,r 内出现奇数次的数的和,就是把 l,r内所有数异或起来就是答案,那么出现偶数次的数就可以 先求出区间 l,r 内有多少不同的数,将这些数异或起来,再异或上区间内出现奇数次的数的异或和就是答案.(出现偶数次的数异或后为0,奇数次的数异或后是本身   然后离线处理询问,对询问按右端点 sort,因为树状数组保存的是数出现的最后位置.离线处理询问后便

Codeforces Round #365 (Div. 2) ABCD

A. Mishka and Game 题解: 水题..比较大小即可 代码: #include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define se second #define fs first #define ll long long #define CLR(x) memset(x,0,sizeof x) #define SZ(x) ((int)(x).size())