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
  1. 4 1
    2 3 1 2
    3
output
  1. 17
input
  1. 5 2
    3 5 2 2 4
    1 4
output
  1. 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的情况

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n,m;
  4. long long q[100005];
  5. long long p[100005];
  6. long long ans1[100005],ans2[100005];
  7. map<int,int>c;
  8. set<long long>e;
  9. long long sum=0,ans=0;
  10. long long cot;
  11. int main()
  12. {
  13. cin>>n>>m;
  14. int a,b;
  15.  
  16. for(int i=1; i<=n; i++)
  17. {
  18. cin>>q[i];
  19. sum+=q[i];
  20. }
  21. for(int i=1; i<=m; i++)
  22. {
  23. cin>>p[i];
  24. c[p[i]]=1;
  25. }
  26. if(c[1]==0&&c[n]==0)
  27. {
  28. ans+=(q[1]*q[n]);
  29. }
  30. for(int i=1;i<=m;i++)
  31. {
  32. sum-=q[p[i]];
  33. ans+=(q[p[i]]*sum);
  34. }
  35. for(int i=1;i<n;i++)
  36. {
  37. if(c[i]==0&&c[i+1]==0)
  38. {
  39. ans+=(q[i]*q[i+1]);
  40. }
  41. }
  42.  
  43. cout<<ans<<endl;
  44. return 0;
  45. }

  

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. Mishka and Divisors[CodeForces Round #365 Div.2]

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

  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) A 水

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

  5. 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 ...

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

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

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

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

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

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

  9. Codeforces Round #365 (Div. 2) B - Mishka and trip

    http://codeforces.com/contest/703/problem/B 题意: 每个点都有一个值,每条边的权值为这两个点相乘.1~n成环.现在有k个城市,城市与其他所有点都相连,计算出 ...

  10. Codeforces Round #365 (Div. 2) A

    Description Mishka is a little polar bear. As known, little bears loves spending their free time pla ...

随机推荐

  1. C++ STL, sort用法。

    在algorithm头文件中的sort可以给任意对象排序,包括内置类型和自定义类型,前提是定义了“<“运算符. sort(begin,end),表示一个范围,例如: #include" ...

  2. loadrunner手动生成脚本函数

    1.点击insert

  3. 测试-Swagger:目录

    ylbtech-测试-Swagger:目录 1.返回顶部 1. https://swagger.io/ 2.Swagger Editor http://swagger.io/swagger-edito ...

  4. rails Ajax--利用Jquery

    view function init_tree(product_name) { var htmlobj=$.ajax({url: "get_all_file?param=" + p ...

  5. python 使用sqlite3

    Sqlite是一个轻量级的数据库,类似于Access. 一. 安装 Python 2.5开始提供了对sqlite的支持,带有sqlite3库. 没有sqlite的版本需要去PySqlite主页上下载安 ...

  6. C语言连接mysql -insert-update

    C语言连接mysql数据库实现insert语句:数据库:test表:systeminfo CREATE TABLE `systeminfo` (  `id` int(11) NOT NULL AUTO ...

  7. Flask11 Session、CSRF、注销session、利用端点自动跳转

    1 怎么对存储的cookie数据进行加密 利用response对象去设置cookie时,存储到浏览器中的cookie数据都是明文的,容易被一些计算机爱好者利用:利用session存的cookie数据可 ...

  8. 《精通Spring4.X企业应用开发实战》读后感第六章(国际化)

  9. hdu1047

    #include<stdio.h>#include<string>#include<iostream>using namespace std; //高精度加法//只 ...

  10. 如何保持blog的高质量(相对于自己的进步而言的)

    多写! 多改!! 多删!!!