Jimbo

Time limit: 2s Memory limit: 256MB Input: Output:

Gimi has recently discovered a new game called Talatro. In this game, Gimi has a deck of NN cards, and his goal is to maximize the score obtained by playing exactly 55 cards.

At the beginning, the score is determined by two components:

  • Chips, denoted by CC, initialized with 00.
  • Multiplier, denoted by MM, initialized with 11.

Each card played by Gimi can modify the values of CC and MM. There are 44 types of cards:

  • 1  x ⁣:1\,\,x\colon Adds xx to CC. More precisely, C←C+xC \leftarrow C + x.
  • 2  x  y ⁣:2\,\,x\,\,y\colon Adds xx to CC and adds yy to MM. More precisely, C←C+x,M←M+yC \leftarrow C + x, M \leftarrow M + y.
  • 3  x  z ⁣:3\,\,x\,\,z\colon Adds xx to CC and multiplies MM by zz. More precisely, C←C+x,M←Mβ‹…zC \leftarrow C + x, M \leftarrow M \cdot z.
  • 4  x  y  z ⁣:4\,\,x\,\,y\,\,z\colon Adds xx to CC, then adds yy to MM, and finally multiplies MM by zz. More precisely, C←C+x,M←(M+y)β‹…zC \leftarrow C + x, M \leftarrow (M + y) \cdot z.

The final score is calculated as the product of CC and MM. Gimi is free to choose the order in which he plays the cards. The question is: what is the maximum score he can achieve?

Task

Write a program that, given as input NN (the total number of cards in the deck) and the description of the NN cards, determines the maximum score Gimi can obtain.

Input Data

The input file contains on the first line the natural number NN, representing the total number of cards in the deck. The following NN lines each describe one card, with each line starting with the value tit_i, indicating the type of card ii. Depending on the type of the card, the corresponding values follow:

  • For ti=1t_i = 1, the value xix_i is given.
  • For ti=2t_i = 2, the values xix_i and yiy_i are given.
  • For ti=3t_i = 3, the values xix_i and ziz_i are given.
  • For ti=4t_i = 4, the values xi,yix_i, y_i, and ziz_i are given.

Output data

The output file will contain on the first line a single natural number, representing the maximum achievable score.

Restrictions

  • 5≀N≀10005 \leq N \leq 1000.
  • 1≀xi,yi≀10001 \leq x_i, y_i \leq 1000, for any 1≀i≀N1 \leq i \leq N.
  • 2≀zi≀1002 \leq z_i \leq 100, for any 1≀i≀N1 \leq i \leq N.
# Points Restrictions
1 8 5≀N≀105 \leq N \leq 10
2 10 5≀N≀155 \leq N \leq 15
3 14 x1=x2=β‹―=xNx_1 = x_2 = \cdots = x_N
4 18 All cards of types 22 and 44 will have the same yy, and all cards of types 33 and 44 will have the same zz.
5 23 There will be no cards of type t=4t = 4.
6 27 No additional restrictions.

Example

stdin

6
1 3
1 5
2 1 1
3 1 2
4 1 1 2
3 1 3

stdout

324

Explanation

The optimal playing order is: the cards with indices 2,3,5,4,2, 3, 5, 4, and 66.
Initially: C=0C = 0, M=1M = 1.
After card 2: C=5,M=1C = 5, M = 1
After card 3: C=6,M=2C = 6, M = 2
After card 5: C=7,M=6C = 7, M = 6
After card 4: C=8,M=12C = 8, M = 12
After card 6: C=9,M=36C = 9, M = 36
Final score: Cβ‹…M=9β‹…36=324C \cdot M = 9 \cdot 36 = 324.
A different order of playing these cards may lead to lower scores.

Log in or sign up to be able to send submissions!