CF BUDDY
← Problems·

1542D · Priority Queue

2200 · combinatorics, dp, implementation

Problem: You are given a sequence AA, where its elements are either in the form + x or -, where xx is an integer.

For such a sequence SS where its elements are either in the form + x or -, define f(S)f(S) as follows:

  • iterate through SS's elements from the first one to the last one, and maintain a multiset TT as you iterate through it.
  • for each element, if it's in the form + x, add xx to TT; otherwise, erase the smallest element from TT (if TT is empty, do nothing).
  • after iterating through all SS's elements, compute the sum of all elements in TT. f(S)f(S) is defined as the sum.

The sequence bb is a subsequence of the sequence aa if bb can be derived from aa by removing zero or more elements without changing the order of the remaining elements. For all AA's subsequences BB, compute the sum of f(B)f(B), modulo 998244353998\,244\,353.

Input Format: The first line contains an integer nn (1n5001\leq n\leq 500) — the length of AA.

Each of the next nn lines begins with an operator + or -. If the operator is +, then it's followed by an integer xx (1x<9982443531\le x<998\,244\,353). The ii-th line of those nn lines describes the ii-th element in AA.

Output Format: Print one integer, which is the answer to the problem, modulo 998244353998\,244\,353.

Note: In the first example, the following are all possible pairs of BB and f(B)f(B):

  • B=B= {}, f(B)=0f(B)=0.
  • B=B= {-}, f(B)=0f(B)=0.
  • B=B= {+ 1, -}, f(B)=0f(B)=0.
  • B=B= {-, + 1, -}, f(B)=0f(B)=0.
  • B=B= {+ 2, -}, f(B)=0f(B)=0.
  • B=B= {-, + 2, -}, f(B)=0f(B)=0.
  • B=B= {-}, f(B)=0f(B)=0.
  • B=B= {-, -}, f(B)=0f(B)=0.
  • B=B= {+ 1, + 2}, f(B)=3f(B)=3.
  • B=B= {+ 1, + 2, -}, f(B)=2f(B)=2.
  • B=B= {-, + 1, + 2}, f(B)=3f(B)=3.
  • B=B= {-, + 1, + 2, -}, f(B)=2f(B)=2.
  • B=B= {-, + 1}, f(B)=1f(B)=1.
  • B=B= {+ 1}, f(B)=1f(B)=1.
  • B=B= {-, + 2}, f(B)=2f(B)=2.
  • B=B= {+ 2}, f(B)=2f(B)=2.

The sum of these values is 1616.

Sample Cases

Case 1

Input

4
-
+ 1
+ 2
-

Output

16

Case 2

Input

15
+ 2432543
-
+ 4567886
+ 65638788
-
+ 578943
-
-
+ 62356680
-
+ 711111
-
+ 998244352
-
-

Output

750759115

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback