Account
Draft saved on this device
▤ DescriptionOriginal CppValley problem
Design PatternsEasy

Build A Command Object For Increment Operations

Maintain a signed counter initially zero.

Maintain a signed counter initially zero. INC x adds x and records it as an undoable command. UNDO removes the effect of the latest non-undone INC, if present; otherwise it leaves the counter unchanged. Print the final value. Commands are processed in order.

Input format

First line Q; next Q lines INC x or UNDO.

Output format

Print one signed integer: the final counter.

Examples

Example 1
Input
6
INC 5
INC 3
UNDO
INC 2
UNDO
INC -1
Expected output
4

Constraints

  • 0 ≤ Q ≤ 100000
  • -10^9 ≤ x ≤ 10^9
  • The result fits signed 64-bit.

Related concepts

Command Pattern

This early-access exercise uses standard input/output. More advanced concurrency challenges will use specialized class interfaces, instrumentation, and stress tests once the judging infrastructure is ready.

⌘ CodeC++17
GNU C++17 · main.cpp

Loading Monaco C++ editor…

Checking judge...

Checking whether the external C++ judge is available.

⌁ Test cases & resultsPublic samples: 1
Input
6
INC 5
INC 3
UNDO
INC 2
UNDO
INC -1
Expected output
4
Execution requires a secure external judge.