File size: 1,541 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
****************************************************************
****************************************************************
-> Coded by Stavros Chryselis				   
-> Visit my github for more solved problems over multiple sites 
-> https://github.com/StavrosChryselis			  
-> Feel free to email me at [email protected]	
****************************************************************
****************************************************************
*/

#include <stdio.h>
#include <vector>
#include <algorithm>
 
using namespace std;
 
const int N = 8;
const unsigned int MAXN = 1 << 8;
vector< pair<int, int> > E;
int A[N];
int M;
 
inline void init()
{
	int i;
	pair<int, int> t;
 
	for (i = 0; i < N; i++)
		scanf("%d", &A[i]);
 
	scanf("%d", &M);
 
	for (i = 0; i < M; i++)
	{
		scanf("%d %d", &t.first, &t.second);
		t.first--;
		t.second--;
		E.push_back(t);
	}
}
 
inline bool test(unsigned int &bset, int pos)
{
	return bset & (1 << pos);
}
 
inline bool check(unsigned int &bset)
{
	int i;
 
	for (i = 0; i < M; i++)
		if (test(bset, E[i].first) && test(bset, E[i].second))
			return 0;
 
	return 1;
}
 
inline int sum(unsigned int &bset)
{
	int i;
	int rval = 0;
 
	for (i = 0; i < N; i++)
		if (test(bset, i))
			rval += A[i];
 
	return rval;
}
 
inline int solve()
{
	int rval = 0;
	unsigned int bset = 0;
 
	for (bset = 0; bset < MAXN; bset++)
		if (check(bset))
			rval = max(rval, sum(bset));
 
	return rval;
}
 
int main()
{
//	freopen("input.txt", "r", stdin);
 
	init();
	printf("%d\n", solve());
 
	return 0;
}