Under construction

Still being built. Things may change

Score Betting

Time limit1sMemory limit1024 MB

Summary
Given prefix and suffix lists of scored strings with updates, find the max and min total score of any nonempty string over letters and digits.
Level

Hard9 of 10

Topics
Trie, String matching, Tree, Segment tree
Solved
No attempts yet

Problem

Amel and Nina bet on scores using strings made of lowercase English letters and digits 0\mathbf{0}-9\mathbf{9}. Amel builds a string that earns the highest possible score, and Nina builds a string that earns the lowest possible score. Each string they build must have length at least 1.

The score of a string AA is the sum of two parts added together:

  • the total score of the strings in the prefix list that are prefixes of AA
  • the total score of the strings in the suffix list that are suffixes of AA

If no string in a list satisfies the condition, that list contributes 00. Both lists start empty.

The prefix list and the suffix list keep changing during the bet, so Amel and Nina have trouble deciding at each moment which string would be most favorable to build.

Help Amel and Nina process the following queries.

  • < str X: Adds the string str with score XX to the prefix list. If str is already in the list, its score is changed to XX.
  • > str X: Adds the string str with score XX to the suffix list. If str is already in the list, its score is changed to XX.
  • +: Prints the maximum score Amel can earn.
  • -: Prints the minimum score Nina can earn.

Input

The first line contains the number of queries QQ (1≤Q≤300 0001\le Q \le 300\,000).

Each of the next QQ lines contains one query.

  • −109≤X≤109-10^9 \le X \le 10^9, and XX is an integer.
  • Every string in the input has length at least 1 and consists only of lowercase English letters. Digits 0\mathbf{0} to 9\mathbf{9} do not appear in the input strings.
  • The total length of all strings in the input does not exceed 1 000 0001\,000\,000.
  • There is at least one + or - query.

Output

For each + and - query, print the result on its own line.

Hint

In the sample output, the strings that earn each score are, in order, abc, 111, abcd, ab123, and abc.

Examples1

  1. Example 1

    Input
    8
    < abc 10
    +
    -
    < ab -6
    +
    -
    > c 7
    +
    
    Expected output
    10
    0
    4
    -6
    11