Buy 'Em All

4

2 votes
Advanced Data Structures, Data Structures, Divide-and-conquer algorithm, Fenwick Tree, Fenwick tree, Medium, Two pointer
Problem

Alice went shopping with her Dad to a magical shop.

She wants to buy some golden snitches from the shop. All the snitches in the shop are kept on a rack in straight line, one kept aside the other.

Shop being magical, doesn't let anyone pick snitch from any arbitrary position. If someone want to buy more than one snitch one can only pick contiguous segment of the items. And the cost of the segment is determined by the cost of each item in it. Alternatively Cost of segment from position l to r(inclusive) is.

  • if length of segment is even.
  • otherwise.

where are the cost of each item from 1 to n.

Alice's Dad has exactly k Rupees(Currency in India) and he wants to spend it all on Alice. Help Alice find the number of segments which she can afford to buy with k Rupees.

Note : Word "item" and "snitch" are used interchangeably in the above statements.

Input format

  • First line contains n and k denoting number of snitches in shop and the amount of money Alice's Dad has.
  • Second line contains n space separated integers C1, C2, ..... Cn denoting cost of each snitch present in the shop.

Output format

  • Output a single number as asked in the question.

Constraints

Sample Input
4 0
5 0 5 0
Sample Output
7
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Segments which Alice can Choose :

  1. with cost 5*0 = 0.
  2. with cost 5*0 + 5*0 = 0.
  3. with cost 0*1 = 0.
  4. with cost 0*5 = 0.
  5. with cost 0*5 + 0*1 = 0.
  6. with cost 5*0 = 0.
  7. with cost 0*1 = 0.
Editor Image

?