#C3914. Final Sequence Sum
Final Sequence Sum
Final Sequence Sum
You are given a series of operations on an initially empty sequence. Each operation is either an ADD x
command or a REMOVE x
command, where x
is an integer. The command ADD x
appends the integer x
to the sequence, while REMOVE x
removes the last occurrence of x
from the sequence if it exists. Your task is to compute the sum of all the numbers remaining in the sequence after processing all operations.
Note:
- If a
REMOVE
operation targets a numberx
that does not exist in the sequence, simply ignore that operation. - The removal should always remove the most recent occurrence of
x
(i.e. the one closest to the end of the sequence).
The sum of the sequence is defined as \(S = \sum_{i=1}^{k} a_i\), where \(a_i\) are the elements of the sequence after all operations.
inputFormat
The first line of the input contains an integer (n) representing the number of operations. Each of the next (n) lines contains an operation in the format ADD x
or REMOVE x
.
outputFormat
Output a single integer — the sum of the sequence after performing all operations.## sample
5
ADD 3
ADD 5
REMOVE 3
ADD 7
REMOVE 5
7
</p>