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个城市,有
思路:我们从头开始向后扫,遇到首都进行相乘,注意:城市总和根据前面的首都数进行改变,而且,注意出现全部都是首都情况。
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;
}
本篇介绍了一个旅行者Mishka面对一个由多个首都组成的国家时,如何通过算法计算所有可能路线的成本总和。该算法考虑了城市间的连接方式及城市的美观价值。

509

被折叠的 条评论
为什么被折叠?



