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: Accepted)

import java.io.*;
import java.nio.CharBuffer;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class Main {

    public static void main(String[] args) {
        SimpleScanner scanner = new SimpleScanner(System.in);
        PrintWriter writer = new PrintWriter(System.out);

        final int MODULO = 998244353;
        int n = scanner.nextInt();
        int[] x = new int[n + 1];
        int[] sum = new int[n + 1];
        x[0] = sum[0] = 0;
        for (int i = 1; i <= n; ++i) {
            x[i] = scanner.nextInt();
            sum[i] = sum[i - 1] + x[i];
        }
        long[][] f = new long[n + 1][sum[n] + 1];
        long[][] g = new long[n + 1][sum[n] + 1];
        Arrays.fill(f[0], 0);
        Arrays.fill(g[0], 0);
        f[0][0] = 1;
        g[0][0] = 1;
        long r = 1;
        for (int i = 1; i <= n; ++i) {
            r = (r * 3) % MODULO;
            Arrays.fill(f[i], 0);
            Arrays.fill(g[i], 0);
            for (int j = 0; j <= sum[i - 1]; ++j) {
                f[i][j] = f[i - 1][j] * 2 % MODULO;
                g[i][j] = g[i - 1][j] % MODULO;
            }
            for (int j = x[i]; j <= sum[i]; ++j) {
                f[i][j] = (f[i][j] + f[i - 1][j - x[i]]) % MODULO;
                g[i][j] = (g[i][j] + g[i - 1][j - x[i]]) % MODULO;
            }
        }
        for (int i = sum[n] % 2 == 0 ? sum[n] / 2 : sum[n] / 2 + 1; i <= sum[n]; ++i) {
            r = (r - f[n][i] * 3 % MODULO) % MODULO;
        }
        if (sum[n] % 2 == 0) {
            r = (r + 3 * g[n][sum[n] / 2] % MODULO) % MODULO;
        }
        writer.println((r + MODULO) % MODULO);

        writer.close();
    }

    private static class SimpleScanner {

        private static final int BUFFER_SIZE = 10240;

        private Readable in;
        private CharBuffer buffer;
        private boolean eof;

        SimpleScanner(InputStream in) {
            this.in = new BufferedReader(new InputStreamReader(in));
            buffer = CharBuffer.allocate(BUFFER_SIZE);
            buffer.limit(0);
            eof = false;
        }


        private char read() {
            if (!buffer.hasRemaining()) {
                buffer.clear();
                int n;
                try {
                    n = in.read(buffer);
                } catch (IOException e) {
                    n = -1;
                }
                if (n <= 0) {
                    eof = true;
                    return '\0';
                }
                buffer.flip();
            }
            return buffer.get();
        }

        void checkEof() {
            if (eof)
                throw new NoSuchElementException();
        }

        char nextChar() {
            checkEof();
            char b = read();
            checkEof();
            return b;
        }

        String next() {
            char b;
            do {
                b = read();
                checkEof();
            } while (Character.isWhitespace(b));
            StringBuilder sb = new StringBuilder();
            do {
                sb.append(b);
                b = read();
            } while (!eof && !Character.isWhitespace(b));
            return sb.toString();
        }

        int nextInt() {
            return Integer.valueOf(next());
        }

        long nextLong() {
            return Long.valueOf(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }
}