inflation — EGOI contest
Task type: batch | Time limit: 3 s | Memory limit: 1024 MB | Difficulty: 10
Algorithms: data structures, prefix sum, hash map

# Inflation
![](_page_0_Figure_0.jpeg)

**EGOI Day 1 Tasks**
**English (ISC)**

| Problem Name | Inflation   |
|--------------|-------------|
| Time Limit   | 3 seconds   |
| Memory Limit | 1 gigabyte  |

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

A falafel place has *N* different dishes on their menu. The *i*-th dish has price *p<sub>i</sub>*.

Every day, one of the following events happen:

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

Your task is to process *Q* days, and after each day print the sum of all prices *p<sub>i</sub>*.

## Input

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

The second line contains *N* integers *p<sub>1</sub>*, *p<sub>2</sub>*, ..., *p<sub>N</sub>*.

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

The following *Q* lines each contain a string *s* followed by either one or two integers.

If *s* is `INFLATION`, then one integer *x* follows. This means that *x* is added to all prices on this day.

If *s* is `SET`, then two integers *x* and *y* follow. This means that all dishes with price *x* get their price set to *y* on this day.

## Output

Print *Q* lines, the sum of all prices *p<sub>i</sub>* after each day.

## Constraints and Scoring

*   1 ≤ *N* ≤ 3 ⋅ 10<sup>5</sup>.
*   1 ≤ *p<sub>i</sub>* ≤ 10<sup>6</sup> (for each *i* such that 1 ≤ *i* ≤ *N*).
*   1 ≤ *Q* ≤ 10<sup>5</sup>.
*   1 ≤ *x*, *y* ≤ 10<sup>6</sup> for all days.

**Note:** The answer may not fit in a 32-bit integer, so be aware of overflows if you are using C++.

Your solution will be tested on a set of test groups, each worth a number of points. Each test group contains a set of test cases. To get the points for a test group you need to solve all test cases in the test group.

| Group | Score | Limits                          |
|-------|-------|---------------------------------|
| 1     | 14    | *N* = 1                         |
| 2     | 28    | *N*, *Q*, *p<sub>i</sub>*, *x*, *y* ≤ 100       |
| 3     | 19    | There are only `INFLATION` events |
| 4     | 23    | There are only `SET` events       |
| 5     | 16    | No additional constraints       |

## Example

This figure corresponds to the first two days of sample 1. Note that the sum of prices after the first day is 16, so the first integer in the output is 16.

![](_page_1_Figure_10.jpeg)

| Input         | Output |
|---------------|--------|
| 5             | 16     |
| 2 1 1 2 5     | 14     |
| 6             | 14     |
| INFLATION 1   | 34     |
| SET 3 2       | 14     |
| SET 5 2       | 5      |
| INFLATION 4   |        |
| SET 6 1       |        |
| SET 10 1      |        |

| Input         | Output |
|---------------|--------|
| 3             | 6      |
| 1 4 1         | 6      |
| 5             | 12     |
| SET 1 1       | 8      |
| SET 3 4       | 6      |
| INFLATION 2   |        |
| SET 3 1       |        |
| SET 6 4       |        |

Starter code: