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 Scala (2.11.7) (judge verdict: Wrong Answer)

import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}

object Main extends App {

  // (x ^ n) % mod
  def repeatSquares(x: Long, n: Int, mod: Int): Long = {
    if(n == 0)
      1
    else if(n % 2 == 0)
      repeatSquares(x*x % mod, n/2, mod)
    else
      (x * repeatSquares(x*x % mod, n/2, mod)) % mod
  }


  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val N = in.next().toInt
  val a = new Array[Int](N+1)
  for (i <- 1 to N)
    a(i) = in.next().toInt


  //
  val MOD = 998244353

  val max = a.sum
  val dp = Array.ofDim[Int](N+1, max+1)
  for (i <- 0 to N; sum <- 0 to max)
    dp(i)(sum) = -1
  dp(0)(0) = 1

  for (i <- 1 to N; sum <- 0 to max) {
    if (sum - a(i) >= 0) {
      if (dp(i-1)(sum) >= 0) {
        dp(i)(sum) = dp(i-1)(sum) * 2 + Math.max(0, dp(i-1)(sum-a(i)))
      }
      else
        dp(i)(sum) = dp(i-1)(sum-a(i))
    }
    else if (dp(i-1)(sum) >= 0)
      dp(i)(sum) = dp(i-1)(sum) * 2

    dp(i)(sum) %= MOD
  }

  var ans = 0L
  for (i <- max / 2 + 1 to max) {
    ans += dp(N)(i)
    ans %= MOD
  }

  ans = repeatSquares(3, N, MOD) - ans * 3
  ans = (ans + MOD) % MOD
  println(ans)
}


class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}