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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#include <vector>

using namespace std;

struct substance
{
	long int id;
	long int quantity;
};

struct sequence
{
	long int a;
	long int b;
};

int main()
{	
	long int vials_amount = 0;
	long int sequences_amount = 0;
	long int reactions_amount = 0;
	long int residue = 0;

	scanf ("%ld",&vials_amount); 
	scanf ("%ld",&sequences_amount); 
	scanf ("%ld",&reactions_amount); 
	
	
	vector <vector<substance>> vials;
	vector <sequence>sequences;

	for(long int vials_iterator = 0; vials_iterator < vials_amount; vials_iterator++)
	{
		vector <substance> x;		
		
		long int substance_quantity = 0;
		substance s;
		s.id = vials_iterator;		
		scanf ("%ld",&substance_quantity);
		s.quantity = substance_quantity;
		x.push_back(s);
		vials.push_back(x);
	}

	for(long int sequence_iterator = 0; sequence_iterator < sequences_amount; sequence_iterator++ )
	{
		long int a = 0;
		long int b = 0;
		
		scanf ("%ld",&a);
		scanf ("%ld",&b);
		
		a--;
		b--;
		for(int i = 0; i < vials.at(a).size(); i++)
		{
			vials.at(b).push_back(vials.at(a).at(i));
		}
		vials.at(a).clear();
	}

	for(long int possible_reactions_iterator = 0; possible_reactions_iterator < reactions_amount; possible_reactions_iterator++)
	{
		long int a;
		long int b;		
		
		scanf("%ld",&a);
		scanf("%ld",&b);
				
		a--;
		b--;

		for(long int vials_iterator = 0; vials_iterator < vials_amount; vials_iterator++)
		{
			if(vials.at(vials_iterator).size() > 1)
			{			
				int correct = 0;
				long int temp = 0;
				for(int i = 0; i < vials.at(vials_iterator).size(); i++)
				{				
					if(vials.at(vials_iterator).at(i).id == a || vials.at(vials_iterator).at(i).id == b)
					{
						correct++;
						if(correct == 2)
						{
							if(vials.at(vials_iterator).at(i).quantity > vials.at(vials_iterator).at(temp).quantity)
							{
								residue += vials.at(vials_iterator).at(temp).quantity * 2;
								vials.at(vials_iterator).at(i).quantity -= vials.at(vials_iterator).at(temp).quantity;
								vials.at(vials_iterator).erase(vials.at(vials_iterator).begin() + temp);
							}
							else
							{
								residue += vials.at(vials_iterator).at(i).quantity * 2;
								vials.at(vials_iterator).at(temp).quantity -= vials.at(vials_iterator).at(i).quantity;
								vials.at(vials_iterator).erase(vials.at(vials_iterator).begin() + i);
							}
							break;
						}
						else
						{
							temp = i;
						}
					}
				}			
			}
		}
	}
	printf("%d", residue);
	return 0;
}