Shopping
Time limit5sMemory limit512 MB
Given an array of prices and a sequence of queries (money, l, r), simulate a shopper who spends as much as possible at each product from l to r and report the leftover money.
- Level
Hard8 of 10
- Topics
- Array, Segment tree, Binary search, Greedy
- Solved
- No attempts yet
Problem
A discount store keeps n products in a row on one shelf. The ith product from the left costs dollars per unit, and every product has unlimited stock.
q customers walk in one after another. Customer i carries dollars, starts at product , and moves one step to the right at a time until product has been looked at.
Every time a customer looks at a product, that customer buys as many units of it as the remaining money allows. A product whose unit price is higher than the remaining money is skipped.
For each customer, find how much money is left after the last product has been looked at.
Input
The first line contains the number of products n and the number of customers q, separated by a space. ()
The second line contains the prices , separated by spaces. ()
Each of the next q lines describes one customer with three integers , , , separated by spaces. (, )
Output
Print q lines. On the ith line, print the money customer i has left after the last product has been looked at, in the order the customers are given in the input.