Under construction

Still being built. Things may change

Inflation

Interview

Time limit3sMemory limit1024 MB

Summary
Given N dish prices, apply Q events (add x to every price, or set each price equal to x to y) and print the price sum after each event.
Level

Medium5 of 10

Topics
Hash map, Implementation
Solved
No attempts yet

Problem

People in southern Sweden are known to eat falafel a lot. The price of falafel changes often, and the best way to analyze the state of the economy is to visit the same falafel place every day and add up all the prices on its menu.

A falafel place has NN different dishes on its menu. The iith dish has price pip_i.

Every day, one of the following events happens:

  • INFLATION x: The integer xx is added to all prices.
  • SET x y: Every dish with price xx gets its price set to yy.

Your task is to process QQ days, and after each day print the sum of all prices pip_i.

Input

The first line contains one integer NN, the number of dishes.

The second line contains NN integers p1,p2,…,pNp_1, p_2, \ldots, p_N.

The third line contains one integer QQ, the number of days.

The following QQ lines each contain a string ss followed by either one or two integers.

If ss is INFLATION, then one integer xx follows. This means that xx is added to all prices on this day.

If ss is SET, then two integers xx and yy follow. This means that all dishes with price xx get their price set to yy on this day.

Output

Print QQ lines. Line kk contains the sum of all prices pip_i after day kk.

Constraints

  • 1≤N≤3⋅1051 \leq N \leq 3 \cdot 10^5.
  • 1≤pi≤1061 \leq p_i \leq 10^6 for each ii such that 1≤i≤N1 \leq i \leq N.
  • 1≤Q≤1051 \leq Q \leq 10^5.
  • 1≤x,y≤1061 \leq x, y \leq 10^6 for all days.

Note: The answer may not fit in a 3232-bit integer, so be careful about overflow if you use C++.

Hint

This figure corresponds to the first two days of sample 11. The sum of prices after the first day is 1616, so the first integer in the output is 1616.

Examples2

  1. Example 1

    Input
    5
    2 1 1 2 5
    6
    INFLATION 1
    SET 3 2
    SET 5 2
    INFLATION 4
    SET 6 1
    SET 10 1
    
    Expected output
    16
    14
    14
    34
    14
    5
    
  2. Example 2

    Input
    3
    1 4 1
    5
    SET 1 1
    SET 3 4
    INFLATION 2
    SET 3 1
    SET 6 4
    
    Expected output
    6
    6
    12
    8
    6