CodeForce 680B - Bear and Finding Criminals

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 31 1 1 0 1 0

Output

3

Input

5 20 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.

题意:

给出小偷的位置p, 然后开始偷跑,

分2种情况。

一.与位置p相距i单位的左边,右边城市都有罪犯,那么随便跑哪个城市都能抓到罪犯,且两个都能抓到。二.与位置p相距i单位的左边(右边)没有城市,但是对称的右边(左边)有城市,且城市里面有罪犯,也就是有罪犯的城市唯一,那么这个罪犯就一定能被抓到。AC代码:

# include <iostream>
using namespace std;
int a[105];
int main()
{
    int n, p, ans;
    while(cin >> n >> p)
	{
        for(int i = 0; i < n; i++)
		{
            cin >> a[i];
        }
        ans = 0, p--;
        if(a[p])
			ans++;
        for(int i = 1 ;i < n ; i++)
		{
            if(p + i >= n && p - i < 0)
				break;
            if(p + i >= n && a[p-i])
				ans++;
            else if(p - i < 0 && a[p+i])
				ans++;
            else if(p + i < n && p - i >= 0 && a[p-i] && a[p+i])
				ans += 2;
        }
        cout << ans << endl;
    }
    return 0;
}
时间: 2024-11-03 21:03:36

CodeForce 680B - Bear and Finding Criminals的相关文章

codeforces 680B B. Bear and Finding Criminals(水题)

题目链接: B. Bear and Finding Criminals //#include <bits/stdc++.h> #include <vector> #include <iostream> #include <queue> #include <cmath> #include <map> #include <cstring> #include <algorithm> #include <cstd

D - 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 b

Codeforce 573A. Bear and Poker

Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are nplayers (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can

Codeforces Round #356 (Div. 2) [Codeforces680]

此处有目录↑ Codeforces Round #356(Div. 2):http://codeforces.com/contest/680 A. Bear and Five Cards (贪心) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A little bear Limak plays a game. He has

codeforce好地方啊 Bear and Elections *

Codeforces Round #318 居然可以看测试数据,哪里没过一目了然,哈哈哈 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 #define M

codeforces 653B B. Bear and Compressing(dfs)

题目链接: B. Bear and Compressing time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Limak is a little polar bear. Polar bears hate long strings and thus they like to compress them. You should al

codeforces 653A A. Bear and Three Balls(水题)

题目链接: A. Bear and Three Balls time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Limak is a little polar bear. He has n balls, the i-th ball has size ti. Limak wants to give one ball to each

POJ 2049 Finding Nemo 优先队列 STL

题目链接:http://poj.org/problem?id=2049 题目利用了<海底总动员>的情节,小丑鱼尼莫迷路了,他老爸去营救他便是题意. 题目给出了这样的地图,说是假设地图由墙和门组成,忽略墙的厚度,地图上有门,没有墙的地方是可以自由行动的问可以经过最少多少道门便可以营救到尼莫. 这个题给的数据是墙的交点为整数点,但鱼爸爸实在非墙的地方自由移动. 因此,这个题有两个难点: 1.如果建图保存地图 2.如何在地图上遍历 由于题目是给出一个点(x,y),来表示一段墙 我便用一对X,Y来表示

CodeForce 448C 木片填涂问题

题目大意:有多片木片需要填涂,可以每次横着涂一行,也可以一次涂一列,当然你涂一行时遇到中间长度不够高的木片,填涂到此中断 这题目运用dfs能更容易的解出,虽然还是十分不容易理解 1 #include <iostream> 2 3 using namespace std; 4 5 #define N 5010 6 int a[N],n; 7 8 int Min(int c,int d) 9 { 10 return c<d?c:d; 11 } 12 int dfs(int c,int d,i