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

#GD0000219. Nhiệm vụ của mèo Kattis (Kattis's Quest)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Nhiệm vụ của mèo Kattis (Kattis's Quest)

Kattis's Quest

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

Kattis maintains a pool of game quests. Each quest costs energy EE and rewards GG gold. There are qq events. An add E G event inserts a new quest, and identical quests remain separate items. A query X event starts a gaming session with energy XX. Kattis repeatedly chooses an unfinished quest having the largest energy cost no greater than the remaining energy; ties in energy are broken by choosing the largest gold reward. The quest is removed, its energy cost is deducted and its gold added to the session total. The session ends when no quest can be completed. Print the gold earned in each query; uncompleted quests persist across sessions.

Input

  • Line 1 contains qq, the number of events.
  • Each of the next qq lines is either add E G (add a quest costing EE energy and rewarding GG gold) or query X (play a session with initial energy XX). Command names are lowercase; numeric arguments are integers.

Output

Print one line for every query command, showing the total gold earned in that session. An add command produces no output.

Subtasks

  • Subtask 1 (20 points): 1≤q≤201\le q\le 20; every event has the form add E G or query X; 1≤E,G,X≤1091\le E,G,X\le 10^9 for the corresponding parameters.
  • Subtask 2 (30 points): 1≤q≤20001\le q\le 2000; every event has the form add E G or query X; 1≤E,G,X≤1091\le E,G,X\le 10^9 for the corresponding parameters.
  • Subtask 3 (50 points): 1≤q≤2000001\le q\le 200000; every event has the form add E G or query X; 1≤E,G,X≤1091\le E,G,X\le 10^9 for the corresponding parameters.

Examples

Input

9
add 8 10
add 3 25
add 5 6
query 7
query 7
add 1 9
add 2 13
query 20
query 1

Output

6
25
32
0

Explanation

Initially quests are (8,10),(3,25),(5,6)(8,10),(3,25),(5,6). Query 77 completes the energy-55 quest for 66 gold, then has insufficient energy for another quest. The next query 77 completes the energy-33 quest for 2525 gold. After adding (1,9)(1,9) and (2,13)(2,13), query 2020 completes all three remaining quests, earning 10+13+9=3210+13+9=32. Query 11 then earns 00 because the pool is empty.