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

#include <bits/stdc++.h>
using namespace std;

   typedef long long ll;
   typedef pair<ll, ll> Pii;
   typedef vector<ll> Vi;
   typedef vector<Vi> VVi;

   const double EPS = (1e-7);
   const ll INF =(1e13);
   const double PI = (acos(-1));
   const ll MOD = 998244353;

   #define REP(i, n) for(ll i = 0; i < (ll)(n); i++)
   #define REPR(i, n) for(ll i = n; i > -1; i--)
   #define FOR(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
   #define ALL(x) (x).begin(),(x).end()
   #define SORT(x) sort((x).begin(), (x).end())
   #define RSORT(x) sort((x).rbegin(), (x).rend())
   #define REVERSE(x) reverse((x).begin(), (x).end())
   #define SZ(x) ((ll)(x).size())
   #define pb push_back
   #define mp make_pair

   //chmax(a, b): a>bならaをbで更新　更新したときにtrueを返す
   //chmin(a, b): a<bならaをbで更新　更新したときにtrueを返す
   template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
   template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

   #define dump(x) cerr<< #x << "= " << (x) << endl;

   ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
   ll pow(ll a, ll b){if (b == 0) return 1; else if (b % 2 == 0) return pow(a * a, b / 2); else return pow(a * a, b / 2) * a;}
   ll pow(ll a, ll b, ll m){if (b == 0) return 1; else if (a == 0) return 0; else if (b % 2 == 0) return (pow((a * a) % m, b / 2, m) % m); else return (pow((a * a) % m, b / 2, m) * a) % m;}
   ll residue(ll a, ll m){return ((a % m) + m) % m;};

   ll dx[4]={1,0,-1,0};
   ll dy[4]={0,1,0,-1};

const ll MAX_N = 300, MAX_S = 300 * 300;
ll dp1[MAX_N + 1][MAX_S + 1] = {};
ll dp2[MAX_N + 1][MAX_S + 1] = {};
int main(){
  ll n; cin >> n;
  Vi a(n); REP(i, n) cin >> a[i];
  Vi s(n + 1, 0);
  REP(i, n){
    s[i + 1] = s[i] + a[i];
  }

  dp1[0][0] = 1;
  dp2[0][0] = 1;
  REP(i, n){
    REP(x, s[i + 1] + 1){
      if(x >= a[i]){
        dp1[i + 1][x] = (dp1[i][x - a[i]] + 2 * dp1[i][x]) % MOD;
        dp2[i + 1][x] = (dp2[i][x - a[i]] + dp2[i][x]) % MOD;
      }
      else{
        dp1[i + 1][x] = (2 * dp1[i][x]) % MOD;
        dp2[i + 1][x] = dp2[i][x];
      }
    }
  }
  ll total = pow(3, n, MOD);
  ll temp = 0;
  if(s[n] % 2 != 0){
    FOR(j, s[n] / 2 + 1, s[n] + 1){
      temp += dp1[n][j] * 3;
    }
  }
  else{
    FOR(j, s[n] / 2, s[n] + 1){
      temp += dp1[n][j] * 3;
    }
    temp -= dp2[n][s[n] / 2] * 3;
  }
  cout << residue(total -temp, MOD) << endl;

}