#P8848. [JRKSJ R5] 1-1 B

    ID: 9412 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划 DP2022洛谷原创洛谷月赛

[JRKSJ R5] 1-1 B

Background

This problem is a harder version of 1-1. The easier version is 1-1 A.

Problem Description

You are given a sequence aa. For all i[1,n]i \in [1,n], ai{1,1}a_i \in \{1,-1\}.

You need to count how many sequences obtained by rearranging aa make the maximum subarray sum as small as possible.

Two sequences are considered different if and only if there exists at least one position where the numbers are different.

Input Format

The first line contains an integer nn.

The second line contains nn integers representing aa.

Output Format

Output one integer representing the answer. The answer should be taken modulo 998244353998244353.

4
1 -1 1 -1
3
5
1 1 1 -1 1
3
10
1 1 1 1 1 1 1 -1 -1 -1
40

Hint

Definition of the maximum subarray sum: the maximum value of the sum over any interval in the sequence, i.e., max1lrni=lrai\max_{1\le l\le r\le n} \sum_{i=l}^r a_i.

Constraints

This problem uses bundled testdata.

Subtask\text{Subtask} nn\le Score\text{Score}
11 1010 2020
22 100100
33 500500
44 10410^4 4040

For 100%100\% of the testdata, 1n1041\le n\le 10^4, ai{1,1}a_i\in \{1,-1\}.

Input Format

Output Format

Translated by ChatGPT 5