CF1038E.Maximum Matching

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given nn blocks, each of them is of the form [color 1_1 |value|color 2_2 ], where the block can also be flipped to get [color 2_2 |value|color 1_1 ].

A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C.

The value of the sequence is defined as the sum of the values of the blocks in this sequence.

Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence.

输入格式

The first line of input contains a single integer nn ( 1n1001 \le n \le 100 ) — the number of given blocks.

Each of the following nn lines describes corresponding block and consists of color1,i\mathrm{color}_{1,i} , valuei\mathrm{value}_i and color2,i\mathrm{color}_{2,i} ( 1color1,i,color2,i41 \le \mathrm{color}_{1,i}, \mathrm{color}_{2,i} \le 4 , 1valuei1000001 \le \mathrm{value}_i \le 100\,000 ).

输出格式

Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence.

输入输出样例

  • 输入#1

    6
    2 1 4
    1 2 4
    3 4 4
    2 8 3
    3 16 3
    1 32 2
    

    输出#1

    63
  • 输入#2

    7
    1 100000 1
    1 100000 2
    1 100000 2
    4 50000 3
    3 50000 4
    4 50000 4
    3 50000 3
    

    输出#2

    300000
  • 输入#3

    4
    1 1000 1
    2 500 2
    3 250 3
    4 125 4
    

    输出#3

    1000

说明/提示

In the first example, it is possible to form a valid sequence from all blocks.

One of the valid sequences is the following:

[4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2]

The first block from the input ([2|1|4] \to [4|1|2]) and second ([1|2|4] \to [4|2|1]) are flipped.

In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped):

[2|100000|1] [1|100000|1] [1|100000|2]

In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value.

首页