Buy 'Em All
Practice
4 (2 votes)
Advanced data structures
Data structures
Divide And Conquer algorithm
Fenwick tree
Fenwick tree
Medium
Two pointer
Problem
44% Success 834 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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 = C_l * C_{l+1} + C_{l+2} * C_{l+3} + ..... + C_{r-1} * C_r \) if length of segment is even.
  • \( Cost = C_l * C_{l+1} + C_{l+2} * C_{l+3} + ..... + C_{r-1} * 1 \) otherwise.

where \(C_1, C_2, C_3, ......, C_n\) 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

  • \(1 \le n \le 2 * 10^{5}\)
  • \(0 \le k \le 10^{18}\)
  • \(0 \le Ai \le 10^{6}\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
19 votes
Tags:
ApprovedDepth First SearchGraphsMediumReadySegment TreesTrees
Points:30
29 votes
Tags:
AlgorithmsApprovedImplementationMediumOpenTrees
Points:30
7 votes
Tags:
Advanced Data StructuresData StructuresFenwick (Binary Indexed) TreesC++