#include <iostream>
#include <set>
#include <vector>
#include <map>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, temp1, temp2, mini = 9999999;
cin >> n >> m;
vector<int> a;
vector<vector<int>> pairs;
vector<int> temp;
temp.push_back(0);
temp.push_back(0);
for (int i = 0; i < n; i++){
cin >> temp1;
a.push_back(temp1);
}
for (int i = 0; i < m; i++){
cin >> temp1 >> temp2;
temp[0] = temp1;
temp[1] = temp2;
pairs.push_back(temp);
}
for (int i = 0; i < m; i++){
map<int, vector<int>> sl;
int summ = a[pairs[i][0] - 1] + a[pairs[i][1] - 1];
for (int j = i + 1; j < m; j++){
if (pairs[j][0] == pairs[i][0]){
sl[pairs[j][1]].push_back(pairs[i][0]);
}
else if (pairs[j][0] == pairs[i][1]){
sl[pairs[j][1]].push_back(pairs[i][1]);
}
else if (pairs[j][1] == pairs[i][0]){
sl[pairs[j][0]].push_back(pairs[i][0]);
}
else if (pairs[j][1] == pairs[i][1]){
sl[pairs[j][0]].push_back(pairs[i][1]);
}
}
for (auto el : sl){
if (el.second.size() == 2){
mini = min(mini, summ + a[el.first - 1]);
}
}
}
if (mini == 9999999){
cout << -1;
} else{
cout << mini;
}
return 0;
}
Cutting a material | Bubble Sort |
Number of triangles | AND path in a binary tree |
Factorial equations | Removal of vertices |
Happy segments | Cyclic shifts |
Zoos | Build a graph |
Almost correct bracket sequence | Count of integers |
Differences of the permutations | Doctor's Secret |
Back to School | I am Easy |
Teddy and Tweety | Partitioning binary strings |
Special sets | Smallest chosen word |
Going to office | Color the boxes |
Missing numbers | Maximum sum |
13 Reasons Why | Friend's Relationship |
Health of a person | Divisibility |
A. Movement | Numbers in a matrix |