Bear and Finding Criminals (CodeForces - 680B)(距a地0、1、2. . .有几个小偷)

                                              Bear and Finding Criminals

                                                                        time limit per test 2 seconds

                                                                  memory limit per test 256 megabytes

                                                                               input standard input

                                                                             output standard output

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.

题解

题的大意是:小明藏了一些糖果让小红找,小明会在小红面前放n个盒子,当然小明知道哪个盒子是有糖果的,每个盒子最多有一个糖果(从最左边的盒子开始,用1和0表示糖果数),小红指定从哪个盒子开始,小明会依次告诉他与小红指定的盒子a距离为0、1、2......的糖果数,让小红猜哪个盒子有糖果,猜到的小红就可以拿走。问小红可以拿走多少糖果,是哪几个盒子的

这个题,分三种:

①距离i=0时a+i就是位置a的糖果数

②从i=1开始遍历,判断a-i和a+i的糖果数,如果是2,那两边距离i的盒子都有糖果。如果是1,则无法得知位置

③a-i或a+i不存在时,判断a-i或a+i的糖果数,如果是1,则该位置有糖果

# include <cstdio>
# include <algorithm>
# include <cstring>

using namespace std;
const int maxn = 1e5 + 10;

int main()
{
	int a[105];
	int n, m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	
	int mark = 0;
	if(a[m]) mark++;
	for(int i=1;;i++)
	{
		if(m-i>0 && m+i<=n)
		{
			if(a[m-i] && a[m+i]) 
				mark += 2;
		}
		else if(m-i<=0 && m+i<=n) 
		{
			if(a[m+i]) 
				mark++;
		}		
		else if(m-i>0 && m+i>n) 
		{
			if(a[m-i]) mark++;
		}
		
		else break;
	}
	printf("%d\n",mark);
	return 0;
 } 

猜你喜欢

转载自blog.csdn.net/qq_39060776/article/details/81163960