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

本篇介绍了一个旅行者Mishka面对一个由多个首都组成的国家时,如何通过算法计算所有可能路线的成本总和。该算法考虑了城市间的连接方式及城市的美观价值。

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:

XXX consists of n cities, k of whose (just imagine!) are capital cities.
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.
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.
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.
There is at most one road between any two cities.
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

题意:n个城市,有m个首都,然后城市i和城市i+1之间必定有一条路,而且首都和每个城市之间都有路,两个城市之间不可能有重复的路,最后问路径的权值和(路径的权值=两边的城市的权值之积)

思路:我们从头开始向后扫,遇到首都进行相乘,注意:城市总和根据前面的首都数进行改变,而且,注意出现全部都是首都情况。

ac代码:

/* ***********************************************
Author       : AnICoo1
Created Time : 2016-08-06-11.14 Saturday
File Name    : D:\MyCode\2016-8月\2016-8-6.cpp
LANGUAGE     : C++
Copyright  2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
int a[MAXN],v[MAXN];
int main()
{
    int n,m,k;cin>>n>>m;ll sum=0;mem(v);
    for(int i=1;i<=n;i++)
        cin>>a[i],sum+=a[i];
    for(int i=1;i<=m;i++) cin>>k,v[k]=1;
    a[n+1]=a[1];if(v[1]) v[n+1]=1;
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        if(!v[i]&&v[i+1]) continue;
        if(v[i])
        {
            ans+=(sum-a[i])*a[i];
            sum-=a[i];
        }
        else
            ans+=(a[i]*a[i+1]);
    }
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值