using Lib; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using static Lib.OutputLib; public class Solver { const bool MultiTestCase = false; void Solve() { int n = ri; var g = new ProjectSelectionProblem(INF); var x = new int[n]; for (int i = 0; i < n; i++) x[i] = g.AddProject(); for (int i = 0; i < n; i++) { int p = ri; if (p > 0) g.Gain1(x[i], p); if (p < 0) g.Lose1(x[i], -p); } int m = ri; for (int i = 0; i < m; i++) { int u = ri - 1, v = ri - 1; g.Ban01(x[u], x[v]); } int k = ri; for (int i = 0; i < k; i++) { int a = ri - 1, b = ri - 1, s = ri; g.Gain11(x[a], x[b], s); } Write(g.Calc()); } #pragma warning disable CS0162, CS8618 public Solver() { if (!MultiTestCase) Solve(); else for (int t = ri; t > 0; t--) Solve(); } #pragma warning restore CS0162, CS8618 const int IINF = 1 << 30; const long INF = 1L << 60; int ri { [MethodImpl(256)] get => (int)sc.Integer(); } long rl { [MethodImpl(256)] get => sc.Integer(); } uint rui { [MethodImpl(256)] get => (uint)sc.UInteger(); } ulong rul { [MethodImpl(256)] get => sc.UInteger(); } double rd { [MethodImpl(256)] get => sc.Double(); } string rs { [MethodImpl(256)] get => sc.Scan(); } string rline { [MethodImpl(256)] get => sc.Line(); } public StreamScanner sc = new StreamScanner(Console.OpenStandardInput()); void ReadArray(out int[] a, int n) { a = new int[n]; for (int i = 0; i < a.Length; i++) a[i] = ri; } void ReadArray(out long[] a, int n) { a = new long[n]; for (int i = 0; i < a.Length; i++) a[i] = rl; } void ReadArray(out T[] a, int n, Func read) { a = new T[n]; for (int i = 0; i < a.Length; i++) a[i] = read(); } void ReadArray(out T[] a, int n, Func read) { a = new T[n]; for (int i = 0; i < a.Length; i++) a[i] = read(i); } } static class Program { static public void Main(string[] args) { SourceExpander.Expander.Expand(); Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); new Solver(); Console.Out.Flush(); } } #region Expanded by https://github.com/kzrnm/SourceExpander public class MaxFlow{public class Edge{public int to,rev;public long cap;}readonly int n;public List[]g;public MaxFlow(int _n){n=_n;g=new List[n];for(int i=0;i();}public void AddEdge(int from,int to,long cap){g[from].Add(new Edge{to=to,cap=cap,rev=g[to].Count});g[to].Add(new Edge{to=from,cap=0,rev=g[from].Count-1});}private void BFS(int s,int t){Array.Fill(level,-1);level[s]=0;var que=new Queue();que.Enqueue(s);while(que.Count>0){int v=que.Dequeue();for(int i=0;i=0)continue;level[e.to]=level[v]+1;if(e.to==t)return;que.Enqueue(e.to);}}}private long DFS(int s,int v,long up){if(v==s)return up;long res=0;int level_v=level[v];for(;iter[v]();qu.Enqueue(s);while(qu.Count>0){var x=qu.Dequeue();visited[x]=true;for(int i=0;i0&&!visited[e.to]){visited[e.to]=true;qu.Enqueue(e.to);}}}return visited;}} public class ProjectSelectionProblem{int n;List<(int from,int to,long cap)>edges;long sum,INF;public ProjectSelectionProblem(long inf){n=2;edges=new List<(int from,int to,long cap)>();sum=0;INF=inf;}public int AddProject()=>n++;public void Ban0(int x)=>edges.Add((x,1,INF));public void Lose0(int x,long w)=>edges.Add((x,1,w));public void Gain0(int x,long w){sum+=w;edges.Add((0,x,w));}public void Ban1(int x)=>edges.Add((0,x,INF));public void Lose1(int x,long w)=>edges.Add((0,x,w));public void Gain1(int x,long w){sum+=w;edges.Add((x,1,w));}public void Ban01(int x,int y)=>edges.Add((x,y,INF));public void Lose01(int x,int y,long w)=>edges.Add((x,y,w));public void GainSame(int x,int y,long w){sum+=w;edges.Add((x,y,w));edges.Add((y,x,w));}public void LoseDiff(int x,int y,long w){edges.Add((x,y,w));edges.Add((y,x,w));}public void BanDiff(int x,int y){edges.Add((x,y,INF));edges.Add((y,x,INF));}public void Gain00(int x,int y,long w){sum+=w;int z=AddProject();edges.Add((0,z,w));edges.Add((z,x,INF));edges.Add((z,y,INF));}public void Gain11(int x,int y,long w){sum+=w;int z=AddProject();edges.Add((z,1,w));edges.Add((x,z,INF));edges.Add((y,z,INF));}public void GainAll0(ReadOnlySpanv,long w){sum+=w;int y=AddProject();edges.Add((0,y,w));foreach(var x in v)edges.Add((y,x,INF));}public void GainAll1(ReadOnlySpanv,long w){sum+=w;int y=AddProject();edges.Add((y,1,w));foreach(var x in v)edges.Add((x,y,INF));}public long Calc(){var mf=new MaxFlow(n);foreach(var(from,to,cap)in edges)mf.AddEdge(from,to,cap);return sum-mf.Calc(0,1);}} namespace Lib{public partial class StreamScanner{public StreamScanner(Stream stream){str=stream;}private readonly Stream str;private readonly byte[]buf=new byte[1024];private int len,ptr;public bool isEof=false;public bool IsEndOfStream{get{return isEof;}}[MethodImpl(256)]private byte Read(){if(isEof)throw new EndOfStreamException();if(ptr>=len){ptr=0;if((len=str.Read(buf,0,1024))<=0){isEof=true;return 0;}}return buf[ptr++];}[MethodImpl(256)]public char Char(){byte b;do b=Read();while(b<33||126=33&&b<=126;b=(char)Read())sb.Append(b);return sb.ToString();}[MethodImpl(256)]public long Integer(){long ret=0;var ng=false;byte b;do b=Read();while(b!='-'&&(b<'0'||'9'double.Parse(Scan());}} namespace Lib{public static class OutputLib{[MethodImpl(256)]public static void WriteJoin(string s,IEnumerablet)=>Console.WriteLine(string.Join(s,t));[MethodImpl(256)]public static void WriteMat(T[,]a,string sep=" "){int sz1=a.GetLength(0),sz2=a.GetLength(1);var b=new T[sz2];for(int i=0;i(T[][]a,string sep=" "){foreach(var ar in a)WriteJoin(sep,ar);}[MethodImpl(256)]public static void WriteMat(T[][]a,Funcmap,string sep=" "){foreach(var ar in a)WriteJoin(sep,ar.Select(x=>map(x)));}[MethodImpl(256)]public static void Write(object t)=>Console.WriteLine(t.ToString());[MethodImpl(256)]public static void Write(params object[]arg)=>Console.WriteLine(string.Join(" ",arg.Select(x=>x.ToString())));[MethodImpl(256)]public static void Write(string str)=>Console.WriteLine(str);[MethodImpl(256)]public static void WriteFlush(object t){Console.WriteLine(t.ToString());Console.Out.Flush();}[MethodImpl(256)]public static void WriteError(object t)=>Console.Error.WriteLine(t.ToString());[MethodImpl(256)]public static void Flush()=>Console.Out.Flush();[MethodImpl(256)]public static void YN(bool t)=>Console.WriteLine(t?"YES":"NO");[MethodImpl(256)]public static void Yn(bool t)=>Console.WriteLine(t?"Yes":"No");[MethodImpl(256)]public static void yn(bool t)=>Console.WriteLine(t?"yes":"no");[MethodImpl(256)]public static void DeleteLine()=>Console.Write("\x1b[1A\x1b[2K");[MethodImpl(256)]public static void ProgressBar(long now,long total,int blocks=50){int x=(int)((2*now*blocks+1)/(2*total));Console.Write($"\x1b[G[\x1b[42m{string.Concat(Enumerable.Repeat("_",x))}\x1b[0m{string.Concat(Enumerable.Repeat("_",blocks-x))}] : {now} / {total}");}}} namespace SourceExpander{public class Expander{[Conditional("EXP")]public static void Expand(string inputFilePath=null,string outputFilePath=null,bool ignoreAnyError=true){}public static string ExpandString(string inputFilePath=null,bool ignoreAnyError=true){return "";}}} #endregion Expanded by https://github.com/kzrnm/SourceExpander