Under construction

Still being built. Things may change

Shopping

Time limit5sMemory limit512 MB

Summary
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 aia_i dollars per unit, and every product has unlimited stock.

q customers walk in one after another. Customer i carries viv_i dollars, starts at product lil_i, and moves one step to the right at a time until product rir_i 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. (1≤n,q≤2000001 \le n, q \le 200000)

The second line contains the prices a1,a2,…,ana_1, a_2, \dots, a_n, separated by spaces. (1≤ai≤10181 \le a_i \le 10^{18})

Each of the next q lines describes one customer with three integers viv_i, lil_i, rir_i, separated by spaces. (1≤vi≤10181 \le v_i \le 10^{18}, 1≤li≤ri≤n1 \le l_i \le r_i \le n)

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.

Examples2

  1. Example 1

    Input
    5 3
    5 3 2 4 6
    8 5 5
    107 1 4
    7 3 5
    
    Expected output
    2
    0
    1
    
  2. Example 2

    Input
    3 2
    10 20 30
    5 1 3
    9 2 3
    
    Expected output
    5
    9