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
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<numeric>
#include<string>
#include<set>
#include<map>
#include<queue>

struct Reaction
{
  int idx, sub1, sub2; 
  Reaction(const int index, const int first, const int second)
    :idx(index), sub1(first), sub2(second)
  {}
  const bool operator<(const Reaction& other) const
  {
    return this->idx < other.idx;
  }
};

struct ReverseReactionCmp
{
  bool operator()(const Reaction& lhs, const Reaction& rhs)
  {
    return !(lhs < rhs);
  }
};

typedef unsigned long long ull;
typedef std::vector<std::vector<Reaction> > ReactionsDescription;
typedef std::map<int, ull> Flask;
typedef std::vector<Flask > Laboratory;
typedef std::vector<std::pair<int, int> > ExperimentDescription;

const int MAX_FLASKS = 200000;

ull mergeFlasks(Laboratory& lab, ReactionsDescription& desc, std::vector<int>& places, int from, int to)
{
  ull res(0ULL);
  std::priority_queue<Reaction, std::vector<Reaction>, ReverseReactionCmp > q;
  std::map<Reaction, std::pair<Flask::iterator, Flask::iterator> > iterators;
  bool swapped = false;
  if(lab[from].size() > lab[to].size())
  {
    std::swap(from, to);
    swapped = true;
  }
  for(Flask::iterator it = lab[from].begin(); it != lab[from].end(); ++it)
  {
    for(std::vector<Reaction>::iterator r = desc[it->first].begin(); r != desc[it->first].end(); ++r)
    { 
      if(places[r->sub2] == to)
      {
        Flask::iterator tmp = lab[to].find(r->sub2);
        if(tmp != lab[to].end())
        {
          q.push(*r);
          iterators.insert(std::make_pair(*r, std::make_pair(it, tmp)));
        }      
      } 
    }
  }
    
  while(!q.empty())
  {
    Reaction r = q.top();
    q.pop();
    std::pair<Flask::iterator, Flask::iterator> subs = iterators[r];
    ull sediment = std::min(subs.first->second, subs.second->second);
    subs.first->second -= sediment;
    subs.second->second -= sediment;
    res += 2ULL * sediment; 
  }
  if(swapped)
  {
    std::swap(from, to);
  }
  for(Flask::iterator it = lab[from].begin(); it != lab[from].end(); ++it)
  {
    places[it->first] = to;
  }
  
  lab[to].insert(lab[from].begin(), lab[from].end());
  lab[from].clear();
  return res;
}

int main(int argc, char** argv)
{
  std::ios_base::sync_with_stdio(false);
  Laboratory laboratory(MAX_FLASKS);
  ReactionsDescription possibleReactions(MAX_FLASKS);
  ExperimentDescription experimentDescription;
  int flasksCount, experimentSteps, reactingPairsCount;
  std::cin>>flasksCount>>experimentSteps>>reactingPairsCount;
  std::vector<int> places(flasksCount);
  for(int i = 0; i < flasksCount; ++i)
  {
    ull gramature;
    std::cin>>gramature;
    laboratory[i].insert(std::make_pair(i, gramature));
    places[i] = i;
  }

  experimentDescription.reserve(experimentSteps); 
  for(int i = 0; i < experimentSteps; ++i)
  {
    int from, to;
    std::cin>>from>>to;
    experimentDescription.push_back(std::make_pair(from - 1, to - 1));
   }
  
  for(int i = 0; i < reactingPairsCount; ++i)
  {
    int first, second;
    std::cin>>first>>second;
    first--;
    second--;
    possibleReactions[first].push_back(Reaction(i, first, second));
    possibleReactions[second].push_back(Reaction(i, second, first));
  }
  

  ull result(0ULL);

  for(ExperimentDescription::iterator it = experimentDescription.begin(); it != experimentDescription.end(); ++it)
  {
    result +=  mergeFlasks(laboratory, possibleReactions, places, it->first, it->second);
  }
  
  std::cout<<result<<std::endl;
   
  return 0;
}