A - Clothes

A little boy Gerald entered a clothes shop and found out something very unpleasant: not all clothes turns out to match. For example, Gerald noticed that he looks rather ridiculous in a smoking suit and a baseball cap.

Overall the shop sells n clothing items, and exactly m pairs of clothing items match. Each item has its price, represented by an integer number of rubles. Gerald wants to buy three clothing items so that they matched each other. Besides, he wants to spend as little money as possible. Find the least possible sum he can spend.

Input

The first input file line contains integers n and m — the total number of clothing items in the shop and the total number of matching pairs of clothing items ().

Next line contains n integers ai (1 ≤ ai ≤ 106) — the prices of the clothing items in rubles.

Next m lines each contain a pair of space-separated integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi). Each such pair of numbers means that the ui-th and the vi-th clothing items match each other. It is guaranteed that in each pair ui and vi are distinct and all the unordered pairs (ui, vi) are different.

Output

Print the only number — the least possible sum in rubles that Gerald will have to pay in the shop. If the shop has no three clothing items that would match each other, print "-1" (without the quotes).

Examples

Input

3 3
1 2 3
1 2
2 3
3 1

Output

6

Input

3 2
2 3 4
2 3
2 1

Output

-1

Input

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

Output

-1

Note

In the first test there only are three pieces of clothing and they all match each other. Thus, there is only one way — to buy the 3 pieces of clothing; in this case he spends 6 roubles.

The second test only has three pieces of clothing as well, yet Gerald can't buy them because the first piece of clothing does not match the third one. Thus, there are no three matching pieces of clothing. The answer is -1.

In the third example there are 4 pieces of clothing, but Gerald can't buy any 3 of them simultaneously. The answer is -1.

代码

#include<stdio.h>
#include<string.h>
#define inf 0x3f3f3f3f
#include<algorithm>
using namespace std;
 
int main()
{
    int m, n, a[105], minn=9999999, map[105][105], x, y, ans;
    scanf("%d%d", &n, &m);
    for(int i=1;i<=n;i++)
        scanf("%d", &a[i]);
    memset(map, 0, sizeof(map));
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d", &x, &y);
        map[x][y]=map[y][x]=1;
    }
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(map[i][k] && map[k][j] && map[i][j])
                {
                    ans=a[i]+a[j]+a[k];
                    if(ans<minn)
                        minn=ans;
                }
    if(minn==9999999)
        printf("-1\n");
    else
        printf("%d\n", minn);
    return 0;
}

 

全部评论

相关推荐

约会议室面试被+1发现了
积极的悲伤蛙愿off...:这怎么了,我都直接跟带教说投了哪些公司让他帮我看看简历,本来就是阶段性的实习生,别真把自己当正式工了
点赞 评论 收藏
分享
已注销:再接着投吧项目经历太流水账,且没有实习经历,我之前也是这样,后来跟着大厂导师修改了项目和简历之后成功上岸,有需要可以问我
点赞 评论 收藏
分享
是觉得求职者都很闲吗?我请问呢
没有offer别哭好...:oppo暑期的时候就这样,做完笔面就开始筛简历,然后挂,没投了现在
投递OPPO等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务