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

#FT0000057. Cập nhật điểm và hạn mức thẻ (Balance Update Query)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Cập nhật điểm và hạn mức thẻ (Balance Update Query)

Balance Update Query

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

There are NN kinds of cards. Kind ii has score aia_i per card and a selection quota of at most bib_i cards. Multiple cards of the same kind may be chosen, within its quota.

Process QQ queries in order: 1 x y assigns score yy to kind xx; 2 x y assigns quota yy to kind xx; 3 x asks for the maximum possible total score when choosing exactly xx cards. If the sum of all quotas is smaller than xx, output -1. Assignments affect all subsequent queries.

Input

  • Line 1: integer NN, number of card kinds.
  • Next NN lines: each line contains score aia_i and quota bib_i.
  • Next line: integer QQ.
  • Next QQ lines: operations in the format 1 x y, 2 x y, or 3 x.

Output

For each type 3 query, print one integer: the maximum score or -1 if the requested number of cards cannot be selected.

Subtasks

Subtask 1 (20 points):

  • 1≤N,Q≤201\le N,Q\le 20; 0≤ai≤1090\le a_i\le10^9, 0≤bi≤1040\le b_i\le10^4 for each kind; type 1: 1≤x≤N,0≤y≤1091\le x\le N,0\le y\le10^9; type 2: 1≤x≤N,0≤y≤1041\le x\le N,0\le y\le10^4; type 3: 1≤x≤1091\le x\le10^9; at least one type 3 query occurs.

Subtask 2 (30 points):

  • 1≤N,Q≤20001\le N,Q\le 2000; 0≤ai≤1090\le a_i\le10^9, 0≤bi≤1040\le b_i\le10^4 for each kind; type 1: 1≤x≤N,0≤y≤1091\le x\le N,0\le y\le10^9; type 2: 1≤x≤N,0≤y≤1041\le x\le N,0\le y\le10^4; type 3: 1≤x≤1091\le x\le10^9; at least one type 3 query occurs.

Subtask 3 (50 points):

  • 1≤N,Q≤2000001\le N,Q\le 200000; 0≤ai≤1090\le a_i\le10^9, 0≤bi≤1040\le b_i\le10^4 for each kind; type 1: 1≤x≤N,0≤y≤1091\le x\le N,0\le y\le10^9; type 2: 1≤x≤N,0≤y≤1041\le x\le N,0\le y\le10^4; type 3: 1≤x≤1091\le x\le10^9; at least one type 3 query occurs.

Examples

Examples 1

Input

3
1 1
2 2
3 3
7
3 4
1 1 10
3 4
2 1 0
2 3 0
3 4
3 2

Output

11
19
-1
4

Explanation

Initially, the best four cards score 3+3+3+2=113+3+3+2=11. After raising kind 1 to score 10, the best four score 10+3+3+3=1910+3+3+3=19. Setting quotas of kinds 1 and 3 to zero leaves only two cards of kind 2, so choosing four is impossible. Choosing two cards scores 2+2=42+2=4.

Examples 2

Input

2
5 1
2 2
5
3 2
2 1 0
3 2
1 2 7
3 2

Output

7
4
14

Explanation

Initially, choose scores 5 and 2 to obtain 7. Setting kind 1 quota to zero leaves two cards of score 2, totaling 4. Updating kind 2 score to 7 makes the best pair total 14.

For the first 3 2 query, the total quota is 1+2=31+2=3, so selecting two cards is possible; the best scores are 55 and 22. After 2 1 0, kind 1 cannot be selected and the total quota is just 22. Operation 1 2 7 changes a score, not a quota, so both final selected cards score 77.