R - Bear and Finding Criminals

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

注意下两种不同的情况就好了,一个是距离只存在一个即只够找到一个一的,和另个距离都存在且都为一的

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
	int a[105],s[105];
	int n,m;
	memset(s,0,sizeof(s));
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	for(int i=0;i<n;i++)
	{
		if(a[i]==1)
		{
			if(i<=m-1)
			{
				s[m-1-i]++;
			}
			else
			{
				s[i-m+1]++;
			}
		}
	}
	int flag=0;
	for(int i=1;i<n;i++)
	{
		if(s[i]==2)
		{
			flag+=2;
		}
		if(s[i]==1&&m+i>n||m-i-1<0&&s[i]==1)
		{
			flag++;
		}
	}
	if(s[0]==1)
	{
		flag++;
	}
	printf("%d",flag);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lemail0406/article/details/84552900
今日推荐