#P9106. [PA 2020] Programowanie współbieżne

[PA 2020] Programowanie współbieżne

Problem Description

This problem is translated from PA 2020 Round 5 Programowanie współbieżne.

To prepare for algorithmic contests, Bytie decided to learn some knowledge about concurrent programming. After all, even in PA there have been distributed problems before (see PA 2018 Round 4).

Bytie starts by writing nn very simple programs. All programs share a global integer variable xx. In addition, each program has a private counter yy. Each program consists of a sequence of instructions, and each instruction is one of the following four types:

  • W\texttt W: load the value of the global variable xx into the private counter yy.
  • Z\texttt Z: write the value of the private counter yy into the global variable xx.
  • + c\texttt{+ }c: increase yy by a positive constant cc.
  • - c\texttt{- }c: decrease yy by a positive constant cc.

Bytie runs all programs in parallel. The initial values of all counters yy and the variable xx are 00. The instructions of these programs are executed in an interleaved way: all instructions of all programs are executed one by one, and at any moment, for each program, some prefix of its instructions has been executed, and the overall execution order is some interleaving of these prefixes.

This interleaving execution leads to a rather unfortunate result: the final value of xx can be so small that Bytie is very surprised. He even suspects this is impossible, and that his computer is cheating him. Help Bytie verify this by writing a checker: for the given programs, compute the minimum possible final value of xx after all programs run in parallel.

Input Format

The first line contains an integer tt, the number of test cases.

For each test case, the first line contains an integer nn, the number of programs written by Bytie.

The next 2n2n lines describe the programs. Each program is described by two lines: the first line contains an integer ll, the number of instructions in the program. The second line contains the descriptions of these ll instructions. Each instruction is one of the following four types:

  • a character W\texttt W: a load instruction.
  • a character Z\texttt Z: a write instruction.
  • a character +\texttt{+} and a number cc: add cc to the private counter.
  • a character -\texttt{-} and a number cc: subtract cc from the private counter.

Over all test cases, the sum of ll does not exceed 10610^6.

Output Format

Output tt lines. The ii-th line is the answer for the ii-th test case, meaning the minimum possible value of xx after running these programs in parallel.

2
2
12
W + 2 Z W + 2 Z W + 2 Z W + 2 Z
12
W + 3 Z W + 3 Z W + 3 Z W + 3 Z
3
3
W W - 5
5
+ 9 Z + 1 Z W
8
+ 10 Z - 2 Z - 5 W - 1 Z
5
7

Hint

Explanation for Sample 1

For the first test case, the following table shows an execution order of the program instructions that achieves the minimum xx.


Constraints

This problem uses bundled testdata.

For 100%100\% of the data, it is guaranteed that 1t1051\le t\le 10^5, 1n1051\le n\le 10^5, 1l1051\le l\le 10^5, l106\sum{l}\leq 10^6.

Translated by ChatGPT 5