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: Wrong Answer)

#include<bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define pb push_back
#define pf pop_front
#define mp make_pair
#define mod 998244353
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mat vector<vector<int>>
#define all(x) x.begin(),x.end()
#define F first
#define S second
#define fo(i,n) for(int i=0;i<n;i++)
#define inp(a) int a;cin>>a;
#define inpa(a,n)vector<int> a(n);fo(i,n)cin>>a[i];
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
using namespace std;
#define int ll


auto max(auto a){auto ans=a[0];for(auto i:a)if(i>ans)ans=i;return ans;}
auto min(auto a){auto ans=a[0];for(auto i:a)if(i<ans)ans=i;return ans;}
void print(auto a){for(auto i:a)cerr<<i<<' ';cerr<<'\n';}

int MAX;
int n;
int a[300];
int dp[300][90001][2];


int md(int x)
{
	if(x>=mod)
		return x-mod;
}

int f(int ind,int diff)
{
	if(diff>MAX)
		return 0;
	int t=((diff>=0)?0:1);
	if(ind>=n)
		return ((diff==0)?1:0);
	if(dp[ind][abs(diff)][t]!=-1)
		return dp[ind][abs(diff)][t];
	dp[ind][abs(diff)][t]=md(md(2*f(ind+1,diff+a[ind]))+f(ind+1,diff-a[ind]));
	return dp[ind][abs(diff)][t];
}

int modpow(int a,int n,int m=mod)
{
	int ans=1;
	while(n>0)
	{
		if(n%2)
			ans=md(ans*a);
		a=md(a*a);
		n/=2;
	}
	return ans;
}

int g(int x,int ind)
{
	if(ind==n)
		if(x==0)
			return 1;
		else
			return 0;
	return md(g(x-a[ind],ind+1)+g(x,ind+1));
}

signed main()
{
	fio
	cin>>n;
	int su=0;
	fo(i,n)cin>>a[i],su+=a[i];
	MAX=su;
	fo(i,n)fo(j,MAX+1)fo(k,2)dp[i][j][k]=-1;
	int ans=0;
	for(int i=0;i<=MAX;i++)
		ans=md(ans+f(0,i));
	if(su%2==0)
		ans=md(ans-g(su/2,0));
	cout<<md(modpow(3,n)-md(3*ans)+mod)<<'\n';
	
	printclock;
}