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 Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.util.HashMap;
import java.util.Scanner;

public class Main {
	Scanner sc = new Scanner(System.in);
	
	public static void main(String[] args) {
		Main m = new Main();
		m.run();
	}

	long mod = 998244353;

	void run() {
		
		int n = sc.nextInt();
		int a[] = new int[n];
		int s = 0;
		for(int i = 0; i < n; i ++) {
			a[i] = sc.nextInt();
			s += a[i];
		}
		
		boolean[] contains = new boolean[s+1];

		contains[0] = true;
		for(int i =0 ; i < n ; i ++) {
			for(int j = 0 ; j <= s - a[i]; j++) {
				if(contains[j]) {
					contains[j+a[i]] = true;
				}
			}
		}
		
		
		long ans = 0;
		for(int r = 1; r <= s/3; r++) {
			if(!contains[r]) {
				continue;
			}
			for(int g = Math.max(r, s/2 - r + 1) ; g <= s - g - r  ; g++  ) {
				if(!contains[g]) {
					continue;
				}				
				int b = s - g - r;

				if(!contains[b]) {
					continue;
				}

				long c = calc(r,g,b,0,a,s) ;
				
				if(r == b) {
					
				}else if(r == g || b==g) {
					c*=3;
				}else {
					c*=6;
				}
				ans += c;
				ans = ans % mod;
			}
		}
		
		System.out.println(ans);
		
		
	}
	HashMap<String,Long> map = new HashMap<String,Long>();

	long calc(int rr, int gg,int bb,int i, int[] a, int s){
		int r = Math.min(bb,Math.min(rr, gg));
		int b = Math.max(bb,Math.max(rr, gg));
		int g = s - r-b;

		String key = r+"_"+g+"_"+b+"_"+i;
		if(map.containsKey(key)) {
			return map.get(key);
		}
		if(i == a.length) {
			if(r==0 && g==0 && b==0) {
				return 1;
			}else {
				return 0;
			}
		}
		
		long count = 0;

		if(r >= a[i]) {
			count += calc(r-a[i], g, b,i+1,a,s-a[i]);
		}
		if(g >= a[i]) {
			count += calc(r, g-a[i], b,i+1,a,s-a[i]);
		}
		if(b >= a[i]) {
			count += calc(r, g, b-a[i],i+1,a,s-a[i]);
		}
		count %= mod;

		map.put(key,count);
		return count;
	}
}