PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000215. Tết âm lịch và gọi món (Lunar New Year and Food Ordering)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tết âm lịch và gọi món (Lunar New Year and Food Ordering)

Lunar New Year and Food Ordering

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

The restaurant offers nn kinds of dishes. Initially kind ii has aia_i portions available at price cic_i each. There are mm customers, served in order; customer jj orders djd_j portions of kind tjt_j. Portions are served one at a time. If the requested kind remains in stock, use it; otherwise use the cheapest remaining kind, breaking equal-price ties by smaller kind index. Stock is reduced immediately. If the restaurant runs out before completing an order, the customer pays 00, even for portions already served; those served portions are still removed from stock. Otherwise the customer pays the total price of all portions served. Determine the price paid by each customer.

Input

  • Line 1 contains n,mn,m: the number of dish kinds and customers.
  • Line 2 contains exactly nn integers aia_i, the initial portions of each kind in index order.
  • Line 3 contains exactly nn integers cic_i, the respective prices per portion.
  • The next mm lines each contain tj,djt_j,d_j: the one-based requested kind index and the number of portions in order jj.

Output

Print mm lines; line jj contains the total charge of customer jj, or 00 if the order cannot be fully served.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le 20; 1≤m≤201\le m\le 20; 1≤ai≤1071\le a_i\le 10^7; 1≤ci≤1061\le c_i\le 10^6 for all ii from 11 to nn; 1≤tj≤n1\le t_j\le n and 1≤dj≤1071\le d_j\le10^7 for all jj from 11 to mm.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤m≤20001\le m\le 2000; 1≤ai≤1071\le a_i\le 10^7; 1≤ci≤1061\le c_i\le 10^6 for all ii from 11 to nn; 1≤tj≤n1\le t_j\le n and 1≤dj≤1071\le d_j\le10^7 for all jj from 11 to mm.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤m≤1000001\le m\le 100000; 1≤ai≤1071\le a_i\le 10^7; 1≤ci≤1061\le c_i\le 10^6 for all ii from 11 to nn; 1≤tj≤n1\le t_j\le n and 1≤dj≤1071\le d_j\le10^7 for all jj from 11 to mm.

Examples

Input

3 4
2 1 3
5 2 4
1 2
2 2
1 1
3 2

Output

10
6
4
0

Explanation

Customer 1 receives two portions of kind 11, paying 1010. Customer 2 gets one portion of kind 22 for 22 and one of kind 33 for 44, paying 66. Kind 11 is sold out, so customer 3 gets a portion of kind 33 for 44. Only one portion remains when customer 4 requests two, so this incomplete order costs 00.