CF1641D.Two Arrays

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:512MB

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) (1≤i,j≤n1 \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.

萨姆转学了,在第一节生物课上,他得到了一个关于基因的非常有趣的任务。

给你 nn 个数组,其中第 ii 个数组包含 mm 个互不相同的整数:ai,1,ai,2,…,ai,ma_{i,1}, a_{i,2},\ldots,a_{i,m}。此外,还给你一个长度为 nn 的整数数组 ww。

在所有满足以下条件的整数对 (i,j)(i, j)(其中 1≤i,j≤n1 \le i, j \le n)中,求 wi+wjw_i + w_j 的最小值:
数组 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} 中的所有数互不相同。

输入格式

The first line contains two integers nn, mm (2≤n≤1052 \leq n \leq 10^5, 1≤m≤51 \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 (1≤ai,j≤1091\leq a_{i,j} \leq 10^9, 1≤wi≤1091 \leq w_{i} \leq 10^9).

第一行包含两个整数 nn 和 mm(2≤n≤1052 \leq n \leq 10^5,1≤m≤51 \le m \le 5)。

接下来的 nn 行中,第 ii 行以 mm 个互不相同的整数 ai,1,ai,2,…,ai,ma_{i,1}, a_{i,2}, \ldots, a_{i,m} 开头,随后是 wiw_i(1≤ai,j≤1091\leq a_{i,j} \leq 10^9,1≤wi≤1091 \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.

输出一个数字——即该问题的答案。

如果没有满足条件的数对 (i,j)(i, j),则输出 −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).

在第一个测试用例中,最小值为 5=w3+w45 = w_3 + w_4,因为数字 {2,3,4,5}\{2, 3, 4, 5\} 互不相同。

在第二个测试用例中,不存在满足条件的数对 (i,j)(i, j)。

输入解题思路,AI测评打分。不知道怎么写?

首页