/
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 900 点
問題文
赤い皿と青い皿が 1 枚ずつあります。また、1 から N までの番号が付いた N 個の石があり、石 i の重さは W_i です。
各石をどちらか一方の皿に乗せた状態を初期状態とします。空の皿があっても構いません。
あなたは、赤い皿に乗っている石の重さの和と、青い皿に乗っている石の重さの和が等しくなるまで、以下の操作を繰り返します。
- 重さの和が大きい方の皿から、最も軽い石を 1 つ選び、もう一方の皿に移す。最も軽い石が複数ある場合は、どれを選んでも構わない。
この操作が有限回(0 回を含む)で終了する初期状態の個数を 998244353 で割った余りを求めてください。 ただし、2 つの初期状態は、乗っている皿が異なる石が存在するときに区別します。番号が異なる石は区別します。
制約
- 2 \le N \le 500
- 1 \le W_i \le 500
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられます。
N W_1 W_2 \ldots W_N
出力
答えを出力してください。
入力例 1
5 4 5 6 7 8
出力例 1
16
- 初期状態で赤い皿に石 1,2 のみが乗っている場合、青い皿から石 3 を移すと両方の重さの和が 15 となり、操作が終了します。
- 初期状態で赤い皿に石 2,4 のみが乗っている場合、石 1 が両方の皿を往復し続けるため、操作は終了しません。
条件を満たす初期状態は 16 通りです。
入力例 2
4 2 2 2 2
出力例 2
16
どの初期状態からも、両方の皿に石が 2 個ずつ乗った状態になり、操作が終了します。重さが等しくても番号の異なる石は区別するため、答えは 2^4=16 です。
入力例 3
2 1 3
出力例 3
0
入力例 4
20 17 4 23 8 15 4 29 11 6 19 8 25 3 14 21 6 27 12 9 31
出力例 4
557536
Score : 900 points
Problem Statement
There is one red plate and one blue plate. There are also N stones numbered from 1 to N, and stone i has weight W_i.
An initial state is a state in which each stone is placed on one of the two plates. A plate may be empty.
You repeat the following operation until the total weight of the stones on the red plate equals the total weight of the stones on the blue plate.
- From the plate with the larger total weight, choose one of the lightest stones and move it to the other plate. If there are multiple lightest stones, you may choose any of them.
Find the number, modulo 998244353, of initial states for which this process terminates after a finite number of operations (possibly zero). Here, two initial states are distinguished when there is a stone that is on different plates in them. Stones with different numbers are distinguished.
Constraints
- 2 \le N \le 500
- 1 \le W_i \le 500
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N W_1 W_2 \ldots W_N
Output
Output the answer.
Sample Input 1
5 4 5 6 7 8
Sample Output 1
16
- If only stones 1,2 are on the red plate in the initial state, moving stone 3 from the blue plate makes both total weights 15, and the process terminates.
- If only stones 2,4 are on the red plate in the initial state, stone 1 keeps going back and forth between the two plates, so the process does not terminate.
There are 16 initial states that satisfy the condition.
Sample Input 2
4 2 2 2 2
Sample Output 2
16
From any initial state, the process reaches a state with two stones on each plate and terminates. Since stones with different numbers are distinguished even if they have the same weight, the answer is 2^4=16.
Sample Input 3
2 1 3
Sample Output 3
0
Sample Input 4
20 17 4 23 8 15 4 29 11 6 19 8 25 3 14 21 6 27 12 9 31
Sample Output 4
557536