Bear and Finding Criminals

Description
There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|.

Limak is a police officer. He lives in a city a. His job is to catch criminals. It’s hard because he doesn’t know in which cities criminals are. Though, he knows that there is at most one criminal in each city.

Limak is going to use a BCD (Bear Criminal Detector). The BCD will tell Limak how many criminals there are for every distance from a city a. After that, Limak can catch a criminal in each city for which he is sure that there must be a criminal.

You know in which cities criminals are. Count the number of criminals Limak will catch, after he uses the BCD.

Input
The first line of the input contains two integers n and a (1 ≤ a ≤ n ≤ 100) — the number of cities and the index of city where Limak lives.

The second line contains n integers t1, t2, …, tn (0 ≤ ti ≤ 1). There are ti criminals in the i-th city.

Output
Print the number of criminals Limak will catch.

Examples
Input
6 3
1 1 1 0 1 0
Output
3
Input
5 2
0 0 0 1 0
Output
1
Note
In the first sample, there are six cities and Limak lives in the third one (blue arrow below). Criminals are in cities marked red.

Using the BCD gives Limak the following information:

There is one criminal at distance 0 from the third city — Limak is sure that this criminal is exactly in the third city.
There is one criminal at distance 1 from the third city — Limak doesn’t know if a criminal is in the second or fourth city.
There are two criminals at distance 2 from the third city — Limak is sure that there is one criminal in the first city and one in the fifth city.
There are zero criminals for every greater distance.
So, Limak will catch criminals in cities 1, 3 and 5, that is 3 criminals in total.

In the second sample (drawing below), the BCD gives Limak the information that there is one criminal at distance 2 from Limak’s city. There is only one city at distance 2 so Limak is sure where a criminal is.

C语言版本一

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int n,a;
	int z[110];
	int i,j;
	scanf("%d%d",&n,&a);
	for(i=0;i<n;i++){
		scanf("%d",&z[i]);
	}
	a-=1;
	int count=0;
	//if (z[a]==1)count+=1;
	
	for(i=0;i<=n/2;i++){
		if(z[a-i]==z[a+i]&&z[a-i]==1){
			if(i==0)count+=1;
			else count+=2;
		}
	
		if(a-i<=0||a+i>=n-1)break;
	}
	
	if(a-i<=0&&a+i<n-1){
		for(j=a+i+1;j<n;j++){
			if(z[j]==1)count+=1;
		}
	}else if(a-i>0&&a+i>=n-1){
		for(j=0;j<a-i;j++){
			if(z[j]==1)count+=1;
		}
	}
	printf("%d\n",count);
	
	return 0;
}

C++版本一

#include<cstdio>
#include<cstring>
const int MYDD=1103;
 
int main() {
	int n,a;
	while(scanf("%d%d",&n,&a)!=EOF) {
		int c[MYDD];//city
		memset(c,0,sizeof(c));
		for(int j=1; j<=n; j++)
			scanf("%d",&c[j]);
 
		int ans=0;
		for(int j=1; j<=n; j++) {
			if(c[j]) {//当前城市有小偷
				int other=a-(j-a);
				if(other<1||other>n||c[other])
					ans++;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务