Coderforces 703B Mishka and trip

本文介绍了一个关于计算旅行者Mishka在一个由多个城市组成的国家中旅行时所有路线的价格的问题。该国包含多个首都城市,每个城市都有各自的美丽值,通过特定公式计算各路线的费用。
Mishka and trip
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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

Sample Input

Input
4 1
2 3 1 2
3
Output
17
Input
5 2
3 5 2 2 4
1 4
Output
71

Hint

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.


思路:

首都城市与各个城市之间都有路,花费为 a[i]  *( sum -  a[i]  ),sum为各个城市beauty values值之和,但是这样计算的话,首都城市与首都城市之间的路计算了两次,每次都要减去,

#include<stdio.h> 
#include<string.h>
int c[100010];
int id[100010];
bool vis[100010];
int main()
{
	int n,k;
	while(scanf("%d%d",&n,&k)!=EOF)
	{
		long long sum=0;
		long long ans=0;
		long long cnt=0;//用int型会出错; 
		//int sum=0,ans=0,cnt=0;
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&c[i]);
			sum+=c[i];
		}
		for(int i=1;i<=k;i++)
		{
			scanf("%d",&id[i]); 
			ans+=c[id[i]];
		}
		for(int i=1;i<=k;i++)
		{
			vis[id[i]]=1; //标记首都城市; 
			cnt+=(sum-c[id[i]])*c[id[i]];
			cnt-=(ans-c[id[i]])*c[id[i]];
			ans-=c[id[i]];//每次减去计算过的首都城市; 
		} 
		for(int i=1;i<n;i++)
		{
			if(!vis[i]&&!vis[i+1])
			{
				cnt+=c[i]*c[i+1];
			}
		}
		if(!vis[1]&&!vis[n])//1和n的时候要特判; 
		{
			cnt+=c[1]*c[n];
		}
		printf("%lld\n",cnt);
	}
	return 0;
}

[root@master01 minikube]# kubectl describe pod etcd-master01 -n kube-system Name: etcd-master01 Namespace: kube-system Priority: 2000001000 Priority Class Name: system-node-critical Node: master01/192.168.40.10 Start Time: Mon, 25 Aug 2025 10:11:08 +0800 Labels: component=etcd tier=control-plane Annotations: kubeadm.kubernetes.io/etcd.advertise-client-urls: https://192.168.40.10:2379 kubernetes.io/config.hash: df74c2895462c99ef9834ba1fb0bc727 kubernetes.io/config.mirror: df74c2895462c99ef9834ba1fb0bc727 kubernetes.io/config.seen: 2025-08-25T10:11:27.722351014+08:00 kubernetes.io/config.source: file seccomp.security.alpha.kubernetes.io/pod: runtime/default Status: Running IP: 192.168.40.10 IPs: IP: 192.168.40.10 Controlled By: Node/master01 Containers: etcd: Container ID: docker://07ffe31db312b665797217c5d58a4f5a79914382f560b65a6945646688b6e012 Image: k8s.gcr.io/etcd:3.5.0-0 Image ID: docker://sha256:2252d5eb703b0519569abad386ce39bcccc05b24a6b1619c3a646fe0746f9258 Port: <none> Host Port: <none> Command: etcd --advertise-client-urls=https://192.168.40.10:2379 --cert-file=/var/lib/minikube/certs/etcd/server.crt --client-cert-auth=true --data-dir=/var/lib/minikube/etcd --initial-advertise-peer-urls=https://192.168.40.10:2380 --initial-cluster=master01=https://192.168.40.10:2380 --key-file=/var/lib/minikube/certs/etcd/server.key --listen-client-urls=https://127.0.0.1:2379,https://192.168.40.10:2379 --listen-metrics-urls=http://127.0.0.1:2381 --listen-peer-urls=https://192.168.40.10:2380 --name=master01 --peer-cert-file=/var/lib/minikube/certs/etcd/peer.crt --peer-client-cert-auth=true --peer-key-file=/var/lib/minikube/certs/etcd/peer.key --peer-trusted-ca-file=/var/lib/minikube/certs/etcd/ca.crt --proxy-refresh-interval=70000 --snapshot-count=10000 --trusted-ca-file=/var/lib/minikube/certs/etcd/ca.crt State: Waiting Reason: CreateContainerError Last State: Terminated Reason: Completed Exit Code: 0 Started: Sat, 30 Aug 2025 19:43:11 +0800 Finished: Tue, 19 Aug 2025 06:15:40 +0800 Ready: False Restart Count: 59 Requests: cpu: 100m memory: 100Mi Liveness: http-get http://127.0.0.1:2381/health delay=10s timeout=15s period=10s #success=1 #failure=8 Startup: http-get http://127.0.0.1:2381/health delay=10s timeout=15s period=10s #success=1 #failure=24 Environment: <none> Mounts: /var/lib/minikube/certs/etcd from etcd-certs (rw) /var/lib/minikube/etcd from etcd-data (rw) Conditions: Type Status Initialized True Ready False ContainersReady False PodScheduled True Volumes: etcd-certs: Type: HostPath (bare host directory volume) Path: /var/lib/minikube/certs/etcd HostPathType: DirectoryOrCreate etcd-data: Type: HostPath (bare host directory volume) Path: /var/lib/minikube/etcd HostPathType: DirectoryOrCreate QoS Class: Burstable Node-Selectors: <none> Tolerations: :NoExecute op=Exists Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Created 31m (x16428 over 2d1h) kubelet Created container etcd Normal Pulled 16m (x33585 over 2d1h) kubelet Container image "k8s.gcr.io/etcd:3.5.0-0" already present on machine Normal SandboxChanged 14m kubelet Pod sandbox changed, it will be killed and re-created. Normal Created 14m kubelet Created container etcd Normal Started 14m kubelet Started container etcd Warning Failed 13m (x10 over 14m) kubelet Error: Error response from daemon: Conflict. The container name "/k8s_etcd_etcd-master01_kube-system_df74c2895462c99ef9834ba1fb0bc727_60" is already in use by container "eca541229b7677eb269641f11f52f709b472136135d1a6c27a84b9a36c0293f3". You have to remove (or rename) that container to be able to reuse that name. Normal Pulled 4m28s (x51 over 14m) kubelet Container image "k8s.gcr.io/etcd:3.5.0-0" already present on machine
08-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值