Rescue Squad

시간 제한1초메모리 제한2048 MB

요약
신뢰 관계 그래프와 각 기사의 레벨이 주어질 때, 네 기사 각자가 나머지 셋 중 최소 둘과 신뢰 관계를 맺는 네 명의 집합 중 레벨 합이 최대인 값을 구하고, 없으면 -1을 출력한다.
난이도

어려움10점 중 8점

유형
그래프, 조합론, 완전 탐색, 구현
정답자
아직 제출이 없습니다

문제

As a town leader, you must form a rescue squad to help a neighboring town under attack by monsters. There are nn knights in your town, numbered from 11 to nn, and a knight ii (1≤i≤n1 ≤ i ≤ n) has a positive integer level L_iL\_i. For efficient monster hunting, the knights who form the rescue squad must be able to trust one another. The trust relationship TT between two knights ii and jj (1≤i<j≤n1 ≤ i < j ≤ n) is defined as follows:

T(i,j)=1T(i,j) = 1, if ii and jj trust each other;

T(i,j)=0T(i,j) = 0, if they do not.

The rescue squad SS is a set of four distinct knights, and each knight in the squad must have a trust relationship T=1T = 1 with at least two of the other three. The ability of SS, Ability(SS), is defined as the sum of the levels of the four knights in SS.

For example, suppose that n=5n = 5 and the levels of the five knights are L_1=3L\_1 = 3, L_2=2L\_2 = 2, L_3=3L\_3 = 3, L_4=7L\_4 = 7, and L_5=1L\_5 = 1. If the trust relationship is defined as T(1,2)=T(2,3)=T(3,4)=T(1,3)=T(1,4)=T(1,5)=1T(1, 2) = T(2, 3) = T(3, 4) = T(1, 3) = T(1, 4) = T(1, 5) = 1 and T(2,4)=T(2,5)=T(3,5)=T(4,5)=0T(2, 4) = T(2, 5) = T(3, 5) = T(4, 5) = 0, then S=1,2,3,4S = \\{1, 2, 3, 4\\} is the only possible rescue squad, and Ability(SS) is 1515.

Given the levels of nn knights and their trust relationships, write a program to find a squad SS for which Ability(SS) is maximized and output its ability value.

입력

Your program is to read from standard input. The input starts with a line containing two integers, nn and mm (4≤n≤1,0004 ≤ n ≤ 1\\,000; 1≤m≤min⁡(n(n−1)2,25,000)1 ≤ m ≤ \min (\frac{n(n-1)}{2}, 25\\,000)), where nn is the number of knights and mm is the number of pairs of knights (i,j)(i,j) such that T(i,j)=1T(i,j) = 1 and i<ji < j. In the following nn lines, the ii-th line contains a positive integer that represents the level L_iL\_i (1≤L_i≤10,0001 ≤ L\_i ≤ 10\\,000) of the knight ii. In the following mm lines, each line contains two integers, ii and jj (1≤i<j≤n1 ≤ i < j ≤ n) that represent a trust relationship T(i,j)=1T(i,j) = 1. There are no duplicate entries among the mm lines describing the trust relationships, and for any pair of knights i′i' and j′j' that do not appear in the input, T(i′,j′)=0T(i',j') = 0.

출력

Your program is to write to standard output. Print exactly one line. The line should contain the ability Ability(SS) of a squad SS with the maximum ability among the squads that can be formed. If no squad can be formed, print −1-1.

예제2

  1. 예제 1

    입력
    5 6
    3
    2
    3
    7
    1
    1 2
    2 3
    3 4
    1 3
    1 4
    1 5
    
    예상 출력
    15
    
  2. 예제 2

    입력
    5 5
    1
    2
    3
    4
    5
    1 2
    2 3
    3 4
    4 5
    1 5
    
    예상 출력
    -1