Algorithm


B. Pair of Toys
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Tanechka is shopping in the toy shop. There are exactly n toys in the shop for sale, the cost of the i-th toy is i burles. She wants to choose two toys in such a way that their total cost is k burles. How many ways to do that does she have?

Each toy appears in the shop exactly once. Pairs (a,b)(�,�) and (b,a)(�,�) are considered equal. Pairs (a,b)(�,�), where a=b�=�, are not allowed.

Input

The first line of the input contains two integers nk (1n,k10141≤�,�≤1014) — the number of toys and the expected total cost of the pair of toys.

Output

Print the number of ways to choose the pair of toys satisfying the condition above. Print 0, if Tanechka can choose no pair of toys in such a way that their total cost is k burles.

Examples
input
Copy
8 5
output
Copy
2
input
Copy
8 15
output
Copy
1
input
Copy
7 20
output
Copy
0
input
Copy
1000000000000 1000000000001
output
Copy
500000000000
Note

In the first example Tanechka can choose the pair of toys (1,41,4) or the pair of toys (2,32,3).

In the second example Tanechka can choose only the pair of toys (7,87,8).

In the third example choosing any pair of toys will lead to the total cost less than 2020. So the answer is 0.

In the fourth example she can choose the following pairs: (1,1000000000000)(1,1000000000000)(2,999999999999)(2,999999999999)(3,999999999998)(3,999999999998), ..., (500000000000,500000000001)(500000000000,500000000001). The number of such pairs is exactly 500000000000500000000000.

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

long long n, k;

int main() {
  scanf("%lld %lld", &n, &k);
  
  if(k - 1 > n) {
    long long a = k / 2, b = k / 2 + 1;
    if(a <= n && b <= n && a + b == k)
      printf("%lld\n", min(n - b + 1, a));
    else if(a - 1 <= n && a - 1 >= 1 && b <= n && (a - 1) + b == k) {
      --a;
      printf("%lld\n", min(n - b + 1, a));
    } else
      puts("0");
    return 0;
  }

  long long a = 1, b = k - 1;
  long long res = b / 2;
  printf("%lld\n", res>;

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
8 5

Output

x
+
cmd
2
Advertisements

Demonstration


Codeforces Solution-B. Pair of Toys-Solution in C, C++, Java, Python,Pair of Toys,Codeforces Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+