Rescue Squad
시간 제한1초메모리 제한2048 MB
신뢰 관계 그래프와 각 기사의 레벨이 주어질 때, 네 기사 각자가 나머지 셋 중 최소 둘과 신뢰 관계를 맺는 네 명의 집합 중 레벨 합이 최대인 값을 구하고, 없으면 -1을 출력한다.
문제
As a town leader, you must form a rescue squad to help a neighboring town under attack by monsters. There are knights in your town, numbered from to , and a knight () has a positive integer level . For efficient monster hunting, the knights who form the rescue squad must be able to trust one another. The trust relationship between two knights and () is defined as follows:
, if and trust each other;
, if they do not.
The rescue squad is a set of four distinct knights, and each knight in the squad must have a trust relationship with at least two of the other three. The ability of , Ability(), is defined as the sum of the levels of the four knights in .
For example, suppose that and the levels of the five knights are , , , , and . If the trust relationship is defined as and , then is the only possible rescue squad, and Ability() is .
Given the levels of knights and their trust relationships, write a program to find a squad for which Ability() is maximized and output its ability value.
입력
Your program is to read from standard input. The input starts with a line containing two integers, and (; ), where is the number of knights and is the number of pairs of knights such that and . In the following lines, the -th line contains a positive integer that represents the level () of the knight . In the following lines, each line contains two integers, and () that represent a trust relationship . There are no duplicate entries among the lines describing the trust relationships, and for any pair of knights and that do not appear in the input, .
출력
Your program is to write to standard output. Print exactly one line. The line should contain the ability Ability() of a squad with the maximum ability among the squads that can be formed. If no squad can be formed, print .