Yesterday, TMD made no network flow .. This is because I cannot understand a sentence .. Tragedy .. The question should be well understood .. That is, the parentheses should be understood .. That is to say, if engineering team A wants to build 1 to 2, and engineering team B wants to build 2 to 3, it means that engineering team A is related to engineering team B .. If you understand this, the minimal cut model can be constructed .. Set the tax revenue of each engineering team to the Authority and the source point. The total construction of each engineering team is C [I], the connection point, and the value is C [I];
The associated engineering teams have a directed edge, and the edge permission is inf .. The key to the topic of the max weight closure is how to create a graph and use the max stream to find it ..
The answer is the total tax revenue-Maximum Flow
*/
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Define N 5005
# Define M 40005 // This should be large enough ..
# Define inf 999999999
Using namespace std;
Int n, m, s, t, num, adj [N], dis [N], q [N], tmp;
Struct edge
{
Int v, w, pre;
} E [M];
Int min (int x, int y)
{
If (x> y)
Return y;
Else
Return x;
}
Void insert (int u, int v, int w)
{
E [num]. v = v;
E [num]. w = w;
E [num]. pre = adj [u];
Adj [u] = num ++;
E [num]. v = u;
E [num]. w = 0;
E [num]. pre = adj [v];
Adj [v] = num ++;
}
Int bfs ()
{
Int I, x, v, tail = 0, head = 0;
Memset (dis, 0, sizeof (dis ));
Dis [s] = 1;
Q [tail ++] = s;
While (head <tail)
{
X = q [head ++];
For (I = adj [x]; I! =-1; I = e [I]. pre)
If (e [I]. w & dis [v = e [I]. v] = 0)
{
Dis [v] = dis [x] + 1;
If (v = t)
Return 1;
Q [tail ++] = v;
}
}
Return 0;
}
Int dfs (int s, int limit)
{
If (s = t)
Return limit;
Int I, v, tmp, cost = 0;
For (I = adj [s]; I! =-1; I = e [I]. pre)
If (e [I]. w & dis [s] = dis [v = e [I]. v]-1)
{
Tmp = dfs (v, min (limit-cost, e [I]. w ));
If (tmp> 0)
{
E [I]. w-= tmp;
E [I ^ 1]. w + = tmp;
Cost + = tmp;
If (limit = cost)
Break;
}
Else dis [v] =-1;
}
Return cost;
}
Int Dinic ()
{
Int ans = 0;
While (bfs ())
Ans + = dfs (s, inf );
Return ans;
}
Int C [5001];
Struct E
{
Int a, B, c;
} Team [2, 3003];
Int main ()
{
Int I, j, Q;
While (scanf ("% d", & n, & m), n + m)
{
Memset (adj,-1, sizeof (adj ));
Num = 0;
Int sum = 0, sum1 = 0;
For (I = 1; I <= m; I ++)
{
Scanf ("% d", & tmp );
Insert (0, I, tmp); // 0 is the sink point
Sum1 + = tmp;
C [I] = 0;
}
Scanf ("% d", & Q );
For (I = 1; I <= Q; I ++)
{
Scanf ("% d", & Team [I]. a, & Team [I]. b, & Team [I]. c, & tmp );
C [Team [I]. c] + = tmp; // remember the taxes of each engineering Team ..
}
For (I = 1; I <= Q; I ++)
{
For (j = 1; j <= Q; j ++) if (I! = J)
{
If (Team [I]. c! = Team [j]. c & Team [I]. B = Team [j]. a) // you can draw a picture on a piece of paper if you do not understand it .. Determine whether there is a relationship between the two engineering teams ..
Insert (Team [I]. c, Team [j]. c, inf );
}
}
For (I = 1; I <= m; I ++)
{
Insert (I, m + 1, C [I]);
}
S = 0;
T = m + 1;
Int ans = sum1-Dinic ();
If (ans <0)
Ans = 0;
Printf ("% d \ n", ans );
}
Return 0;
}