AmazonOA #79
Count Bought Items (Recurring Shop Purchase)
Estimated HardJudge-ready
Topics (AI-labelled)Bits & MathBinary SearchSimulation
Buy the n items cyclically in order. The t-th purchase of item i costs t×cost[i]. The problem text does not say what happens when an item is unaffordable; the Python, Java, and C++ solutions of the same fixed OAMaster version all stop immediately at the first unaffordable item, so this site specifies that purchasing stops rather than skipping and continuing.
Input
The first line contains n m; the second line contains n integers cost[i]. 1≤n≤100000, 1≤m≤10^15, 1≤cost[i]≤200000.
Output
Output the total number of items bought before stopping. Stop immediately when the current item is unaffordable; do not skip it.
Example 1
Input:
2 2
5 1
Output:
0
Example 2
Input:
2 5
2 1
Output:
2
Example 3
Input:
2 12
1 2
Output:
5
Example notes
First binary search the number of complete rounds that can be bought, then process the next round in order; stop at the first unaffordable item.
Editorial
Loading solution…