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
#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;
#define LONG long long
//#define LONG __int64

class Reaction
{
public:
   long substract1;
   long substract2;
   
   Reaction(long s1, long s2) : substract1(s1), substract2(s2) { };
};

class Step
{
public:
   long phial1;
   long phial2;

   Step(long p1, long p2) : phial1(p1), phial2(p2) { };
};

vector<Reaction> vecReact;
vector<Step> vecSteps;
vector<long> vecSubstracts;

class Phial
{
public:
   set<long> reactions;

   LONG Mix(Phial& ph2)
   {
      LONG sediment = 0LL;

      set<long> common;
      set<long> others;

      set_intersection(reactions.begin(), reactions.end(), ph2.reactions.begin(), ph2.reactions.end(),
         inserter(common, common.begin()));

      if(common.empty())
      {
         reactions.insert(ph2.reactions.begin(), ph2.reactions.end());
      }
      else
      {
         set_symmetric_difference(reactions.begin(), reactions.end(), ph2.reactions.begin(), ph2.reactions.end(),
                                  inserter(others, others.begin()));
         
         for(set<long>::iterator sit = common.begin(), sentinel = common.end(); sit != sentinel; ++sit)
         {
            Reaction& r = vecReact[*sit];
            long s1mass = vecSubstracts[r.substract1];
            long s2mass = vecSubstracts[r.substract2];
          
            if(s1mass > s2mass)
            {
               sediment += 2LL * s2mass;
               vecSubstracts[r.substract1] -= s2mass;
               vecSubstracts[r.substract2] = 0;
            }
            else // (s1mass <= s2mass)
            {
               sediment += 2LL * s1mass;
               vecSubstracts[r.substract1] = 0;
               vecSubstracts[r.substract2] -= s1mass;
            }
         }

         reactions.swap(others);
      }
      return sediment;
   }
};

vector<Phial> vecPhials;

int main(int argc, char* argv[])
{
   long vialCount = 0L;
   long stepCount = 0L;
   long reactCount = 0L;
   long grams = 0L;
   long subs1 = 0L, subs2 = 0L;

   scanf("%ld %ld %ld\n", &vialCount, &stepCount, &reactCount);
   vecReact.reserve(reactCount);
   Phial ph;
   vecPhials.reserve(vialCount + 1);
   vecPhials.insert(vecPhials.begin(), vialCount + 1, ph);
   vecSubstracts.reserve(vialCount + 1);
   vecSubstracts.push_back(0L);
   vecSteps.reserve(stepCount);

   for(long vidx = 0; vidx < vialCount; ++vidx)
   {
      scanf("%ld", &grams);
      vecSubstracts.push_back(grams);
   }

   for(long sidx = 0; sidx < stepCount; ++sidx)
   {
      scanf("%ld %ld\n", &subs1, &subs2);
      vecSteps.push_back(Step(subs1, subs2));
   }

   for(long ridx = 0; ridx < reactCount; ++ridx)
   {
      scanf("%ld %ld\n", &subs1, &subs2);
      vecReact.push_back(Reaction(subs1, subs2));
      vecPhials[subs1].reactions.insert(ridx);
      vecPhials[subs2].reactions.insert(ridx);
   }

   LONG totalResidue = 0LL;

   for(long sidx = 0; sidx < stepCount; ++sidx)
   {
      Step& st = vecSteps[sidx];
      totalResidue += vecPhials[st.phial2].Mix(vecPhials[st.phial1]);
   }

   cout << totalResidue << endl;

   return 0;
}