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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//#define DEBUG
#ifdef DEBUG
#define D(...) __VA_ARGS__
#else
#define D(...) do {} while(0)
#define NDEBUG
#endif

#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <assert.h>

using namespace std;

typedef int Substance;
typedef int Amount;
typedef long long TotalAmount;
typedef map<Substance, Amount> Mixture;

vector<Mixture> vials;
vector<pair<Substance, Substance>> recipes;
struct ReagentPairDescription
{
	Substance first;
	Substance second;
	int index;
};
vector<ReagentPairDescription> reagents;
TotalAmount sediment;

bool compare_only_first(const ReagentPairDescription &a, const ReagentPairDescription &b)
{
	return a.first < b.first;
}

bool compare_first_then_second(const ReagentPairDescription &a, const ReagentPairDescription &b)
{
	return a.first == b.first ? a.second < b.second : a.first < b.first;
}

bool compare_indices(const ReagentPairDescription &a, const ReagentPairDescription &b)
{
	return a.index < b.index;
}

int main(int argc, char **argv)
{
	ios_base::sync_with_stdio(false);
	int n, m, k;

	/* input */
	cin >> n >> m >> k;
	assert(0 <= m);
	assert(m <= 200000);
	assert(0 <= n);
	assert(n <= 200000);
	assert(0 <= k);
	assert(k <= 500000);
	vials.resize(n);

	for(int i=0; i<n; i++)
	{
		Amount g;
		cin >> g;
		assert(1 <= g);
		assert(g <= 1000000000);
		vials[i][i] = g;
	}
	recipes.resize(m);
	for(int i=0; i<m; i++)
	{
		Substance a, b;
		cin >> a >> b;
		assert(1 <= a);
		assert(a <= n);
		assert(1 <= b);
		assert(b <= n);
		assert(a != b);
		recipes[i].first = a-1;
		recipes[i].second = b-1;
	}
	for(int i=0; i<k; i++)
	{
		Substance r1, r2;
		cin >> r1 >> r2;
		r1--;
		r2--;
		reagents.push_back({r1, r2, i});
		reagents.push_back({r2, r1, i});
	}
	sort(reagents.begin(), reagents.end(), compare_first_then_second);

	/* solve (or rather dissolve... ;-)) */
	sediment = 0;
	for(auto &step: recipes)
	{
		Mixture &source_vial = vials[step.first];
		Mixture &dest_vial = vials[step.second];
		vector<ReagentPairDescription> reactions;
		{
			bool source_is_less_diverse = source_vial.size() < source_vial.size();
			Mixture &less_diverse_vial = source_is_less_diverse ? source_vial : dest_vial;
			Mixture &more_diverse_vial = source_is_less_diverse ? dest_vial : source_vial;
			for(pair<Substance, Amount> s: less_diverse_vial)
			{
				auto candidate_reagent_pair_range = equal_range(reagents.begin(), reagents.end(), ReagentPairDescription{s.first, -1, -1}, compare_only_first);
				size_t n_candidates = candidate_reagent_pair_range.second - candidate_reagent_pair_range.first;
				if(n_candidates < more_diverse_vial.size())
				{
					for(auto candidate_reagent_pair = candidate_reagent_pair_range.first; candidate_reagent_pair != candidate_reagent_pair_range.second; candidate_reagent_pair++)
					{
						assert(candidate_reagent_pair->first == s.first);
						auto found = more_diverse_vial.find(candidate_reagent_pair->second);
						if(found != more_diverse_vial.end())
						{
							reactions.push_back(*candidate_reagent_pair);
							if(!source_is_less_diverse)
							{
								swap(reactions.back().first, reactions.back().second);
							}
						}
					}
				}
				else
				{
					for(pair<Substance, Amount> mixture_component: more_diverse_vial)
					{

						auto found = equal_range(candidate_reagent_pair_range.first, candidate_reagent_pair_range.second, ReagentPairDescription{s.first, mixture_component.first, -1}, compare_first_then_second);
						if(found.first != found.second)
						{
							reactions.push_back(*found.first);
							if(!source_is_less_diverse)
							{
								swap(reactions.back().first, reactions.back().second);
							}
						}
					}
				}
			}
		}
		sort(reactions.begin(), reactions.end(), compare_indices);
		for(auto &reaction: reactions)
		{
			auto source_reagent = source_vial.find(reaction.first);
			auto dest_reagent = dest_vial.find(reaction.second);
			if(source_reagent != source_vial.end() && dest_reagent != dest_vial.end())
			{
				if(source_reagent->second > dest_reagent->second)
				{
					sediment += 2 * dest_reagent->second;
					source_reagent->second -= dest_reagent->second;
					dest_vial.erase(dest_reagent);
				}
				else if(source_reagent->second < dest_reagent->second)
				{
					sediment += 2 * source_reagent->second;
					dest_reagent->second -= source_reagent->second;
					source_vial.erase(source_reagent);
				}
				else
				{
					sediment += 2 * source_reagent->second;
					source_vial.erase(source_reagent);
					dest_vial.erase(dest_reagent);
				}
			}
		}
		Mixture::iterator s = source_vial.begin();
		Mixture::iterator d = dest_vial.begin();
		while(s != source_vial.end())
		{
			while(s != source_vial.end() && (d == dest_vial.end() || s->first < d->first))
			{
				dest_vial.insert(d, *s);
				s++;
			}
			while(s != source_vial.end() && d != dest_vial.end() && s->first > d->first)
			{
				d++;
			}
			while(s != source_vial.end() && d != dest_vial.end() && s->first == d->first)
			{
				d->second += s->second;
				s++;
				d++;
			}
		}

		source_vial.clear();
	}

	/* output */
	cout << sediment << endl;
	return 0;
}