CF1641D.Two Arrays

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Sam changed his school and on the first biology lesson he got a very interesting task about genes.

You are given nn arrays, the ii -th of them contains mm different integers — ai,1,ai,2,,ai,ma_{i,1}, a_{i,2},\ldots,a_{i,m} . Also you are given an array of integers ww of length nn .

Find the minimum value of wi+wjw_i + w_j among all pairs of integers (i,j)(i, j) ( 1i,jn1 \le i, j \le n ), such that the numbers ai,1,ai,2,,ai,m,aj,1,aj,2,,aj,ma_{i,1}, a_{i,2},\ldots,a_{i,m}, a_{j,1}, a_{j,2},\ldots,a_{j,m} are distinct.

输入格式

The first line contains two integers nn , mm ( 2n1052 \leq n \leq 10^5 , 1m51 \le m \le 5 ).

The ii -th of the next nn lines starts with mm distinct integers ai,1,ai,2,,ai,ma_{i,1}, a_{i,2}, \ldots, a_{i,m} and then wiw_i follows ( 1ai,j1091\leq a_{i,j} \leq 10^9 , 1wi1091 \leq w_{i} \leq 10^9 ).

输出格式

Print a single number — the answer to the problem.

If there are no suitable pairs (i,j)(i, j) , print 1-1 .

输入输出样例

  • 输入#1

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

    输出#1

    5
  • 输入#2

    4 3
    1 2 3 5
    2 3 4 2
    3 4 5 3
    1 3 10 10

    输出#2

    -1

说明/提示

In the first test the minimum value is 5=w3+w45 = w_3 + w_4 , because numbers {2,3,4,5}\{2, 3, 4, 5\} are distinct.

In the second test case, there are no suitable pair (i,j)(i, j) .

首页