Problem:
Score : 600 points

Problem Statement

You are given N integers. The i-th integer is a_i.
Find the number, modulo 998244353, of ways to paint each of the integers red, green or blue so that the following condition is satisfied:

Let R, G and B be the sums of the integers painted red, green and blue, respectively. There exists a triangle with positive area whose sides have lengths R, G and B.

Constraints

3 \leq N \leq 300

1 \leq a_i \leq 300(1\leq i\leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1
:
a_N

Output

Print the number, modulo 998244353, of ways to paint each of the integers red, green or blue so that the condition is satisfied.

Sample Input 1

4
1
1
1
2

Sample Output 1

18

We can only paint the integers so that the lengths of the sides of the triangle will be 1, 2 and 2, and there are 18 such ways.

Sample Input 2

6
1
3
2
3
5
2

Sample Output 2

150

Sample Input 3

20
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9
3
2
3
8
4

Sample Output 3

563038556

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include<bits/stdc++.h>
using namespace std;
const int mod = 998244353;

int main(){
	int N;
	cin >> N;
	int a[N];
	int sum = 0;
	for(int i=0; i<N; i++){
		cin >> a[i];
		sum += a[i];
	}
	long long th[N+1];
	long long tw[N+1];
	tw[0] = 1; th[0] = 1;
	for(int i=1; i<=N; i++){
		th[i] = (th[i-1] * 3) % mod;
		tw[i] = (tw[i-1] * 2) % mod;
	}
	long long dp[N][90001], dp2[N][90001];
	dp[0][0] = 2;
	for(int i=0; i<=90000; i++){
		if(i == a[0]) dp[0][i] = 1;
		else dp[0][i] = 0;
	}
	for(int i=0; i<N; i++){
		dp[i][0] = tw[i+1];
	}
	for(int i=1; i<N; i++){
		for(int j=1; j<=90000; j++){
			if(j >= a[i]){
				dp[i][j] = (dp[i-1][j-a[i]] + 2*dp[i-1][j]) % mod;
			}else{
				dp[i][j] = 2*dp[i-1][j];
			}
		}
	}
	dp2[0][0] = 1;
	for(int i=1; i<=45000; i++){
		if(i == a[0]) dp2[0][i] = 1;
		else dp2[0][i] = 0;
	}
	for(int i=0; i<N; i++){
		dp2[i][0] = 1;
	}
	for(int i=1; i<N; i++){
		for(int j=1; j<=45000; j++){
			if(j >= a[i]){
				dp2[i][j] = (dp2[i-1][j-a[i]] + dp2[i-1][j]) % mod;
			}else{
				dp2[i][j] = dp2[i-1][j];
			}
		}
	}
	long long ans = 0;
	int mid = (sum+1)/2;
	long long d = 0;
	for(int i=mid; i<=sum; i++){
		d = (d + dp[N-1][i]) % mod;
	}
	if(sum % 2 == 0){
		ans = ((th[N] + dp2[N-1][mid] * 3) % mod - 3*d%mod + mod) % mod;
	}else{
		ans = (th[N] - 3*d%mod + mod) % mod;
	}
	cout << ans << endl;
	return 0;
}