#ARC142F. [ARC142F] Paired Wizards

[ARC142F] Paired Wizards

Score : 10001000 points

Problem Statement

Two wizards, XX and YY, are fighting with a monster.

Initially, each of the wizards has a magic power of 00. They know the following two spells.

  • Spell 11: Increases the caster's magic power by 11.
  • Spell 22: Deals damage equal to the caster's magic power to the monster.

After each wizard uses a spell NN times, they will withdraw from the battle. For each i=1,,Ni=1, \ldots, N, they must use one of the following combinations of spells for their ii-th spells.

  • XX casts Spell aia_i, and YY casts Spell bib_i.
  • XX casts Spell cic_i, and YY casts Spell did_i.

Find the maximum total damage that can be dealt to the monster before the withdrawal.

Constraints

  • 1N80001 \leq N \leq 8000
  • ai,bi,ci,di{1,2}a_i,b_i,c_i,d_i \in \{1,2\}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

a1a_1 b1b_1 c1c_1 d1d_1

\vdots

aNa_N bNb_N cNc_N dNd_N

Output

Print the answer.

3
1 1 2 2
2 1 2 2
2 1 1 1
3

The maximum total damage can be achieved as follows.

  • As the 11-st spells, use a1=1,b1=1a_1=1,\, b_1=1, increasing both XX's and YY's magic powers to 11.
  • As the 22-nd spells, use c2=2,d2=2c_2=2,\, d_2=2, dealing 22 points of damage in total.
  • As the 33-rd spells, use a3=2,b3=1a_3=2,\, b_3=1, dealing 11 point of damage by XX's spell and increasing YY's magic power to 22.
5
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
0

Casting Spell 22 with a magic power of 00 deals no damage.

8
1 1 2 2
2 2 2 1
1 1 2 1
1 1 2 2
2 1 1 1
1 2 1 2
2 1 1 2
2 1 2 1
20
20
2 1 2 1
2 1 1 1
1 2 1 1
2 2 1 2
2 2 2 1
1 1 2 1
1 2 2 2
2 2 2 1
1 1 1 2
1 2 1 2
1 2 2 2
2 1 1 2
2 1 1 1
1 2 1 2
1 2 1 2
1 1 1 2
1 1 2 1
2 2 1 1
1 2 2 2
2 1 1 2
138