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 Rust (1.15.1) (judge verdict: Accepted)

//! ----------------------------------------------
//! Framework <https://github.com/vain0x/procon>
//!
//! See the bottom of file for solution.
//! ----------------------------------------------

#![allow(unused_imports)]
#![allow(non_snake_case)]

use std::cell::RefCell;
use std::cmp::{max, min, Ordering};
use std::collections::*;
use std::fmt::{Debug, Display, Formatter, Write as FmtWrite};
use std::io::{stderr, stdin, BufRead, Write};
use std::mem::{replace, swap};
use std::ops::*;
use std::rc::Rc;

/// Print values to standard error if debug mode.
#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), stderr());
            writeln!(err, "\x1B[33m{}\x1B[0m = {:?}", e, $e).unwrap()
        })*
    };
}

/// Read from standard input and parse each word.
/// - `read!(T, U, ..)` parses a line as a tuple of words.
/// - `read![[T]]` parses a line as an array of words.
/// - `read![..; N]` parses `N` lines, using `read!(..)` repeatedly.
#[allow(unused_macros)]
macro_rules! read {
    ([$t:ty] ; $n:expr) =>
        ((0..$n).map(|_| read!([$t])).collect::<Vec<_>>());
    ($($t:ty),+ ; $n:expr) =>
        ((0..$n).map(|_| read!($($t),+)).collect::<Vec<_>>());
    ([$t:ty]) =>
        (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::<Vec<$t>>());
    ($($t:ty),*) => {{
        let buf = rl();
        let mut w = buf.split_whitespace();
        ($(w.next().unwrap().parse::<$t>().unwrap()),*)
    }};
}

/// Read a line from standard input.
#[allow(dead_code)]
fn rl() -> String {
    let mut buf = String::new();
    stdin().read_line(&mut buf).unwrap();

    #[allow(deprecated)]
    buf.trim_right().to_owned()
}

// -----------------------------------------------
// Solution
// -----------------------------------------------

const P: i64 = 998244353;

fn main() {
    let N = read!(usize);
    let A = read![usize; N];
    let S = A.iter().sum::<usize>();
    let M = A.iter().cloned().max().unwrap();

    // dp1[k][r] = x : 前から k 個の整数を塗り分けて、赤の総和が r になる塗りかたの個数が x
    let mut dp1 = vec![vec![0; M * (N + 1) + 1]; N + 1];
    // dp2[k][r] = x : ある1色を塗らない場合
    let mut dp2 = vec![vec![0; M * (N + 1) + 1]; N + 1];
    dp1[0][0] = 1;
    dp2[0][0] = 1;

    for k in 0..N {
        let a = A[k];
        for r in 0..M * N + 1 {
            // この整数を赤に塗るケース
            dp1[k + 1][r + a] += dp1[k][r];
            dp1[k + 1][r] %= P;
            dp2[k + 1][r + a] += dp2[k][r];
            dp2[k + 1][r] %= P;

            // 緑か青に塗るケース
            dp1[k + 1][r] += 2 * dp1[k][r];
            dp1[k + 1][r] %= P;
            dp2[k + 1][r] += 1 * dp2[k][r];
            dp2[k + 1][r] %= P;
        }
    }

    let mut u = 0;
    for r in (S + 1) / 2..M * N + 1 {
        u += dp1[N][r];
        u %= P;
    }
    u *= 3;
    u %= P;

    let mut v = if S % 2 == 0 { dp2[N][S / 2] } else { 0 };
    v *= 3;
    v %= P;

    let mut p = 1;
    for _ in 0..N {
        p *= 3;
        p %= P;
    }

    let n = ((p + (P - u)) % P + v) % P;
    debug!(p, u, v);

    println!("{}", n)
}