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.

  • Cost=ClCl+1+Cl+2Cl+3+.....+Cr1Cr if length of segment is even.
  • Cost=ClCl+1+Cl+2Cl+3+.....+Cr11 otherwise.

where C1,C2,C3,......,Cn 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

  • 1n2105
  • 0k1018
  • 0Ai106
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Segments which Alice can Choose :

  1. C1C2 with cost 5*0 = 0.
  2. C1C4 with cost 5*0 + 5*0 = 0.
  3. C2C2 with cost 0*1 = 0.
  4. C2C3 with cost 0*5 = 0.
  5. C2C4 with cost 0*5 + 0*1 = 0.
  6. C3C4 with cost 5*0 = 0.
  7. C4C4 with cost 0*1 = 0.
Editor Image

?