using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;

namespace Codeforces.R318.E
{
    public static class Solver
    {
        public static void Main()
        {
            using (var sw = new StreamWriter(Console.OpenStandardOutput()) {
                AutoFlush = false
            })
            {
                Solve(Console.In, sw);
            }
        }

        public static void Solve(TextReader tr, TextWriter tw)
        {
            WriteAnswer(tw, Parse(tr));
        }

        private static void WriteAnswer(TextWriter tw, long[] res)
        {
            if (res == null)
            {
                tw.WriteLine("NO");
                return;
            }

            tw.WriteLine(res.Length);

            foreach (var t in res)
            {
                var prev = t & ((1 << 16) - 1);
                var to = (t >> 16) & ((1 << 16) - 1);
                var d = t >> 32;
                tw.WriteLine("{0} {1} {2}", prev + 1, to + 1, d);
            }
        }

        private static long[] Parse(TextReader tr)
        {
            var w = tr.ReadLine().Split();
            var e = int.Parse(w[2]);

            var a = from x in tr.ReadLine().Split() select int.Parse(x);
            var b = from x in tr.ReadLine().Split() select int.Parse(x);

            var edges = new (int, int)[e];
            for (int i = 0; i < e; ++i)
            {
                var l = tr.ReadLine().Split();
                edges[i] = (int.Parse(l[0]) - 1, int.Parse(l[1]) - 1);
            }

            return Calc(a.ToArray(), b.ToArray(), edges);
        }

        public static long[] Calc(int[] a, int[] b, (int, int)[] edges)
        {
            var n = a.Length;
            var mat = new (int, int)[n, n];

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    mat[i, j] = i == j ? (0, i) : (9999, i);
                }
            }

            foreach (var edge in edges)
            {
                mat[edge.Item1, edge.Item2] = (1, edge.Item1);
                mat[edge.Item2, edge.Item1] = (1, edge.Item2);
            }

            for (int k = 0; k < n; ++k)
            {
                for (int i = 0; i < n; ++i)
                {
                    for (int j = 0; j < n; ++j)
                    {
                        if (mat[i, k].Item1 + mat[k, j].Item1 < mat[i, j].Item1)
                        {
                            mat[i, j] = (mat[i, k].Item1 + mat[k, j].Item1, mat[k, j].Item2);
                        }
                    }
                }
            }

            var res = Execute(a, b, mat);

            if (!a.SequenceEqual(b))
            {
                return null;
            }

            return res.ToArray();
        }

        public static List<long> Execute(int[] a, int[] b, (int, int)[,] mat)
        {
            var calculator = new Calculator(a, b, mat);
            return calculator.Solve();
        }
    }

    internal class Calculator
    {
        List<long> res = new List<long>();
        int[] a;
        int[] b;
        (int, int)[,] mat;

        public Calculator(int[] a, int[] b, (int, int)[,] mat)
        {
            this.a = a;
            this.b = b;
            this.mat = mat;
        }

        public List<long> Solve()
        {
            var n = a.Length;
            for (; ; )
            {
                var prev = res.Count;
                for (int i = 0; i < n; ++i)
                {
                    for (int j = 0; j < n; ++j)
                    {
                        if (a[i] > b[i] && a[j] < b[j] && mat[i, j].Item1 <= n)
                        {
                            Pour(i, j, Math.Min(a[i] - b[i], b[j] - a[j]));
                        }
                    }
                }
                if (prev == res.Count)
                {
                    break;
                }
            }

            return res;
        }

        private void Pour(int from, int to, int d)
        {
            if (from == to)
            {
                return;
            }

            var prev = mat[from, to].Item2;
            var p = Math.Min(a[prev], d);
            if (p > 0)
            {
                a[prev] -= p;
                a[to] += p;
                res.Add(CreateKey(prev, to, p));
            }

            Pour(from, prev, d);

            if (d - p > 0)
            {
                a[prev] -= d - p;
                a[to] += d - p;
                res.Add(CreateKey(prev, to, d - p));
            }
        }

        private long CreateKey(long prev, long to, long d)
        {
            return (d << 32) + (to << 16) + prev;
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Codeforces.R318.E
{
    public static class Solver
    {
        public static void Main()
        {
            using (var sw = new StreamWriter(Console.OpenStandardOutput()) {
                AutoFlush = false
            })
            {
                Solve(Console.In, sw);
            }
        }

        public static void Solve(TextReader tr, TextWriter tw)
        {
            WriteAnswer(tw, Parse(tr));
        }

        private static void WriteAnswer(TextWriter tw, (long, long, long)[] res)
        {
            if (res == null)
            {
                tw.WriteLine("NO");
                return;
            }

            tw.WriteLine(res.Length);

            foreach (var t in res)
            {
                var prev = t.Item1;
                var to = t.Item2;
                var d = t.Item3;
                tw.WriteLine("{0} {1} {2}", prev + 1, to + 1, d);
            }
        }

        public static int ReadInt(this TextReader tr)
        {
            var v = 0;
            var negative = false;

            for (var c = (char)tr.Read(); !char.IsWhiteSpace(c); c = (char)tr.Read())
            {
                if (c == '-')
                {
                    negative = true;
                }
                else
                {
                    v = v * 10 + c - '0';
                }
            }

            for (; char.IsWhiteSpace((char)tr.Peek()); tr.Read()) ;

            return negative ? -v : v;
        }
        private static (long, long, long)[] Parse(TextReader tr)
        {
            var n = tr.ReadInt();
            var v = tr.ReadInt();
            var e = tr.ReadInt();

            var a = from x in tr.ReadLine().Split() select int.Parse(x);
            var b = from x in tr.ReadLine().Split() select int.Parse(x);

            var edges = new (int, int)[e];
            for (int i = 0; i < e; ++i)
            {
                var l = tr.ReadLine().Split();
                edges[i] = (int.Parse(l[0]) - 1, int.Parse(l[1]) - 1);
            }

            return Calc(a.ToArray(), b.ToArray(), edges);
        }

        public static (long, long, long)[] Calc(int[] a, int[] b, (int, int)[] edges)
        {
            var n = a.Length;
            var mat = new WarshallFloydGraph(n);

            foreach (var edge in edges)
            {
                mat.AddEdge(edge.Item1, edge.Item2);
            }

            mat.Solve();

            var res = Execute(a, b, mat);

            if (!a.SequenceEqual(b))
            {
                return null;
            }

            return res.ToArray();
        }

        public static List<(long, long, long)> Execute(int[] a, int[] b, WarshallFloydGraph mat)
        {
            var calculator = new Calculator(a, b, mat);
            return calculator.Solve();
        }
    }

    public class WarshallFloydGraph
    {
        private int[,] mat;
        private int[,] prev;

        public WarshallFloydGraph(int n)
        {
            mat = new int[n, n];
            prev = new int[n, n];

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    mat[i, j] = i == j ? 0 : n + 1;
                    prev[i, j] = i;
                }
            }
        }

        internal void AddEdge(int s, int t)
        {
            mat[s, t] = 1;
            prev[s, t] = s;
            mat[t, s] = 1;
            prev[t, s] = t;
        }

        internal void Solve()
        {
            var n = mat.GetLength(0);

            for (int k = 0; k < n; ++k)
            {
                for (int i = 0; i < n; ++i)
                {
                    for (int j = 0; j < n; ++j)
                    {
                        if (mat[i, k] + mat[k, j] < mat[i, j])
                        {
                            mat[i, j] = mat[i, k] + mat[k, j];
                            prev[i, j] = prev[k, j];
                        }
                    }
                }
            }
        }

        internal bool IsConnected(int i, int j)
        {
            return mat[i, j] <= mat.GetLength(0);
        }

        internal int PreviousNode(int from, int to)
        {
            return prev[from, to];
        }

    }

    internal class Calculator
    {
        readonly List<(long, long, long)> res = new List<(long, long, long)>();
        readonly int[] a;
        readonly int[] b;
        readonly WarshallFloydGraph mat;

        public Calculator(int[] a, int[] b, WarshallFloydGraph mat)
        {
            this.a = a;
            this.b = b;
            this.mat = mat;
        }

        public List<(long, long, long)> Solve()
        {
            var n = a.Length;

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    if (a[i] > b[i] && a[j] < b[j] && mat.IsConnected(i, j))
                    {
                        Pour(i, j, Math.Min(a[i] - b[i], b[j] - a[j]));
                    }
                }
            }

            return res;
        }

        private void Pour(int from, int to, int d)
        {
            if (from == to)
            {
                return;
            }

            var prev = mat.PreviousNode(from, to);
            var p = Math.Min(a[prev], d);
            if (p > 0)
            {
                a[prev] -= p;
                a[to] += p;
                res.Add((prev, to, p));
            }

            Pour(from, prev, d);

            int remainder = d - p;

            if (remainder > 0)
            {
                a[prev] -= remainder;
                a[to] += remainder;
                res.Add((prev, to, remainder));
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Codeforces.R318.E
{
    public static class Solver
    {
        public static void Main()
        {
            using (var sw = new StreamWriter(Console.OpenStandardOutput()) {
                AutoFlush = false
            })
            {
                Solve(Console.In, sw);
            }
        }

        public static void Solve(TextReader tr, TextWriter tw)
        {
            WriteAnswer(tw, Parse(tr));
        }

        private static void WriteAnswer(TextWriter tw, long[] res)
        {
            if (res == null)
            {
                tw.WriteLine("NO");
                return;
            }

            tw.WriteLine(res.Length);

            foreach (var t in res)
            {
                var prev = t & ((1 << 16) - 1);
                var to = (t >> 16) & ((1 << 16) - 1);
                var d = t >> 32;
                tw.WriteLine("{0} {1} {2}", prev + 1, to + 1, d);
            }
        }

        private static long[] Parse(TextReader tr)
        {
            var w = tr.ReadLine().Split();
            var e = int.Parse(w[2]);

            var a = from x in tr.ReadLine().Split() select int.Parse(x);
            var b = from x in tr.ReadLine().Split() select int.Parse(x);

            var edges = new (int, int)[e];
            for (int i = 0; i < e; ++i)
            {
                var l = tr.ReadLine().Split();
                edges[i] = (int.Parse(l[0]) - 1, int.Parse(l[1]) - 1);
            }

            return Calc(a.ToArray(), b.ToArray(), edges);
        }

        public static long[] Calc(int[] a, int[] b, (int, int)[] edges)
        {
            var n = a.Length;
            var mat = new (int, int)[n, n];

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    mat[i, j] = i == j ? (0, i) : (9999, i);
                }
            }

            foreach (var edge in edges)
            {
                mat[edge.Item1, edge.Item2] = (1, edge.Item1);
                mat[edge.Item2, edge.Item1] = (1, edge.Item2);
            }

            for (int k = 0; k < n; ++k)
            {
                for (int i = 0; i < n; ++i)
                {
                    for (int j = 0; j < n; ++j)
                    {
                        if (mat[i, k].Item1 + mat[k, j].Item1 < mat[i, j].Item1)
                        {
                            mat[i, j] = (mat[i, k].Item1 + mat[k, j].Item1, mat[k, j].Item2);
                        }
                    }
                }
            }

            var res = Execute(a, b, mat);

            if (!a.SequenceEqual(b))
            {
                return null;
            }

            return res.ToArray();
        }

        public static List<long> Execute(int[] a, int[] b, (int, int)[,] mat)
        {
            var calculator = new Calculator(a, b, mat);
            return calculator.Solve();
        }
    }

    internal class Calculator
    {
        List<long> res = new List<long>();
        int[] a;
        int[] b;
        (int, int)[,] mat;

        public Calculator(int[] a, int[] b, (int, int)[,] mat)
        {
            this.a = a;
            this.b = b;
            this.mat = mat;
        }

        public List<long> Solve()
        {
            var n = a.Length;
            //for (; ; )
            {
                var prev = res.Count;
                for (int i = 0; i < n; ++i)
                {
                    for (int j = 0; j < n; ++j)
                    {
                        if (a[i] > b[i] && a[j] < b[j] && mat[i, j].Item1 <= n)
                        {
                            Pour(i, j, Math.Min(a[i] - b[i], b[j] - a[j]));
                        }
                    }
                }
                //if (prev == res.Count)
                //{
                //    break;
                //}
            }

            return res;
        }

        private void Pour(int from, int to, int d)
        {
            if (from == to)
            {
                return;
            }

            var prev = mat[from, to].Item2;
            var p = Math.Min(a[prev], d);
            if (p > 0)
            {
                a[prev] -= p;
                a[to] += p;
                res.Add(CreateKey(prev, to, p));
            }

            Pour(from, prev, d);

            if (d - p > 0)
            {
                a[prev] -= d - p;
                a[to] += d - p;
                res.Add(CreateKey(prev, to, d - p));
            }
        }

        private long CreateKey(long prev, long to, long d)
        {
            return (d << 32) + (to << 16) + prev;
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;

namespace Codeforces.R318.E
{
    public static class Solver
    {
        public static void Main()
        {
            using (var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false })
            {
                Solve(Console.In, sw);
            }
        }

        public static void Solve(TextReader tr, TextWriter tw)
        {
            WriteAnswer(tw, Parse(tr));
        }

        private static void WriteAnswer(TextWriter tw, long[] res)
        {
            if (res == null)
            {
                tw.WriteLine("NO");
                return;
            }

            tw.WriteLine(res.Length);

            foreach (var t in res)
            {
                var prev = t & ((1 << 16) - 1);
                var to = (t >> 16) & ((1 << 16) - 1);
                var d = t >> 32;
                tw.WriteLine("{0} {1} {2}", prev + 1, to + 1, d);
            }
        }

        private static long[] Parse(TextReader tr)
        {
            var w = tr.ReadLine().Split();
            var e = int.Parse(w[2]);

            var a = from x in tr.ReadLine().Split() select int.Parse(x);
            var b = from x in tr.ReadLine().Split() select int.Parse(x);

            var edges = new (int, int)[e];
            for (int i = 0; i < e; ++i)
            {
                var l = tr.ReadLine().Split();
                edges[i] = (int.Parse(l[0]) - 1, int.Parse(l[1]) - 1);
            }

            return Calc(a.ToArray(), b.ToArray(), edges);
        }

        public static long[] Calc(int[] a, int[] b, (int, int)[] edges)
        {
            var n = a.Length;
            var mat = new (int, int)[n, n];
            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    mat[i, j] = i == j ? (0, j) : (9999, j);
                }
            }

            foreach (var edge in edges)
            {
                mat[edge.Item1, edge.Item2] = (1, edge.Item1);
                mat[edge.Item2, edge.Item1] = (1, edge.Item2);
            }

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    for (int k = 0; k < n; ++k)
                    {
                        if (mat[i, k].Item1 + mat[k, j].Item1 < mat[i, j].Item1)
                        {
                            mat[i, j] = (mat[i, k].Item1 + mat[k, j].Item1, mat[k, j].Item2);
                        }
                    }
                }
            }

            var res = Execute(a, b, mat);

            if (!a.SequenceEqual(b))
            {
                return null;
            }

            return res.ToArray();
        }

        public static List<long> Execute(int[] a, int[] b, (int, int)[,] mat)
        {
            var calculator = new Calculator(a, b, mat);
            return calculator.Solve();
        }
    }

    internal class Calculator
    {
        List<long> res = new List<long>();
        int[] a;
        int[] b;
        (int, int)[,] mat;

        public Calculator(int[] a, int[] b, (int, int)[,] mat)
        {
            this.a = a;
            this.b = b;
            this.mat = mat;
        }

        public List<long> Solve()
        {
            var n = a.Length;

            for (; ; )
            {
                var prev = res.Count;

                var maxValue = 0;
                var maxIndex = 0;

                for (int k = 0; k < n; ++k)
                {
                    if (a[k] - b[k] > maxValue)
                    {
                        maxValue = a[k] - b[k];
                        maxIndex = k;
                    }
                }

                if (maxValue == 0)
                {
                    break;
                }

                var minValue = 0;
                var minIndex = 0;

                for (int j = 0; j < n; ++j)
                {
                    if (a[j] - b[j] < minValue)
                    {
                        if (mat[maxIndex, j].Item1 > n)
                        {
                            continue;
                        }
                        minValue = a[j] - b[j];
                        minIndex = j;
                    }
                }

                if (minValue == 0)
                {
                    break;
                }

                Pour(maxIndex, minIndex, Math.Min(maxValue, -minValue));

                if (prev == res.Count)
                {
                    break;
                }
            }

            return res;
        }

        private void Pour(int from, int to, int d)
        {
            for (; ; )
            {
                var prev = mat[from, to].Item2;

                var p = Math.Min(a[prev], d);
                if (p > 0)
                {
                    a[prev] -= p;
                    a[to] += p;
                    res.Add(CreateKey(prev, to, d));
                }
                if (prev == from)
                {
                    break;
                }
                to = prev;
            }
        }

        private long CreateKey(long prev, long to, long d)
        {
            return (d << 32) + (to << 16) + prev;
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Codeforces.R318.E
{
    public static class Solver
    {
        public static void Main()
        {
            using (var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false })
            {
                Solve(Console.In, sw);
            }
        }

        public static void Solve(TextReader tr, TextWriter tw)
        {
            WriteAnswer(tw, Parse(tr));
        }

        private static void WriteAnswer(TextWriter tw, (int, int, int)[] res)
        {
            if (res == null)
            {
                tw.WriteLine("NO");
                return;
            }

            tw.WriteLine(res.Length);

            foreach (var t in res)
            {
                tw.WriteLine("{0} {1} {2}", t.Item1 + 1, t.Item2 + 1, t.Item3);
            }
        }

        private static (int, int, int)[] Parse(TextReader tr)
        {
            var w = tr.ReadLine().Split();
            var v = int.Parse(w[1]);
            var e = int.Parse(w[2]);

            var a = from x in tr.ReadLine().Split() select int.Parse(x);
            var b = from x in tr.ReadLine().Split() select int.Parse(x);

            var edges = new (int, int)[e];
            for (int i = 0; i < e; ++i)
            {
                var l = tr.ReadLine().Split();
                edges[i] = (int.Parse(l[0]) - 1, int.Parse(l[1]) - 1);
            }

            return Calc(a.ToArray(), b.ToArray(), edges, v);
        }

        public static (int, int, int)[] Calc(int[] a, int[] b, (int, int)[] edges, int v)
        {
            var n = a.Length;
            var mat = new (int, int)[n, n];
            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    mat[i, j] = i == j ? (0, j) : (9999, j);
                }
            }

            foreach (var edge in edges)
            {
                mat[edge.Item1, edge.Item2] = (1, edge.Item2);
                mat[edge.Item2, edge.Item1] = (1, edge.Item1);
            }

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    for (int k = 0; k < n; ++k)
                    {
                        if (mat[i, k].Item1 + mat[k, j].Item1 < mat[i, j].Item1)
                        {
                            mat[i, j] = (mat[i, k].Item1 + mat[k, j].Item1, mat[i, k].Item2);
                            //mat[i, j] = (mat[i, k].Item1 + mat[k, j].Item1, k);
                        }
                    }
                }
            }

            List<(int, int, int)> res = Execute(a, b, n, mat);

            if (!a.SequenceEqual(b))
            {
                return null;
            }

            return res.ToArray();
        }

        public static (int, int, int)[] Calc0(int[] a, int[] b, (int, int)[] edges, int v)
        {
            var n = a.Length;
            var mat = new (int, int)[n, n];
            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    mat[i, j] = i == j ? (0, j) : (9999, j);
                }
            }

            foreach (var edge in edges)
            {
                mat[edge.Item1, edge.Item2] = (1, edge.Item2);
                mat[edge.Item2, edge.Item1] = (1, edge.Item1);
            }

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    for (int k = 0; k < n; ++k)
                    {
                        if (mat[i, k].Item1 + mat[k, j].Item1 < mat[i, j].Item1)
                        {
                            mat[i, j].Item1 = mat[i, k].Item1 + mat[k, j].Item1;
                            mat[i, j].Item2 = k;
                        }
                    }
                }
            }

            List<(int, int, int)> res = Execute(a, b, n, mat, v);

            if (!a.SequenceEqual(b))
            {
                return null;
            }

            return res.ToArray();
        }

        public static List<(int, int, int)> Execute(int[] a, int[] b, int n, (int, int)[,] mat)
        {
            var res = new List<(int, int, int)>();

            for (; ; )
            {
                var prev = res.Count;
                for (int i = 0; i < n; ++i)
                {
                    if (a[i] > b[i])
                    {
                        for (int j = 0; j < n; ++j)
                        {
                            if (a[j] < b[j])
                            {
                                if (mat[i, j].Item1 > n)
                                {
                                    continue;
                                }
                                res.AddRange(Pour(mat, a, b, i, j, Math.Min(a[i] - b[i], b[j] - a[j])));
                            }
                        }
                    }
                }
                if (prev == res.Count)
                {
                    break;
                }
            }

            return res;
        }

        public static List<(int, int, int)> Execute(int[] a, int[] b, int n, (int, int)[,] mat, int v)
        {
            var R = new List<(int, int, int)>();
            for (int x = 0; x < n; ++x)
            {
                for (int y = 0; y < n; ++y)
                {
                    if (mat[x, y].Item1 > n)
                        continue;
                    var j = Math.Min(a[x] - b[x], b[y] - a[y]);
                    if (j <= 0) continue;
                    Flow(x, y, j, mat, a, v, R);
                }
            }
            return R;
        }

        private static void Flow(int s, int t, int d, (int,int)[,] mat, int[] A, int V, List<(int,int,int)>R)
        {
            var N = mat.GetLength(0);

            var P = new List<int>();
            P.Add(s);

            for (var i = s; i != t;)
            {
                int y = 0;
                for (;y< N; ++y)
                    if (mat[i,y].Item1 == 1 && mat[y,t].Item1 == mat[i,t].Item1 - 1)
                        break;
                P.Add(i = y);
            }

            Flow2(P, A, 0, P.Count - 1, d, V, R);
        }

        private static void Flow2(List<int> P, int[] A, int si, int ti, int d, int V, List<(int, int, int)> R)
        {
            int i;
            if (si == ti) return;
            if (A[P[si + 1]] + d <= V)
            {
                A[P[si]] -= d;
                A[P[si + 1]] += d;
                R.Add((P[si], P[si + 1], d));
                Flow2(P, A, si + 1, ti, d, V, R);
            }
            else
            {
                int d2 = V - A[P[si + 1]];
                A[P[si]] -= d2;
                A[P[si + 1]] += d2;
                if (d2 > 0) R.Add((P[si], P[si + 1], d2));
                Flow2(P, A, si + 1, ti, d, V, R);
                A[P[si]] -= d - d2;
                A[P[si + 1]] += d - d2;
                R.Add((P[si], P[si + 1], d - d2));
            }
        }

        private static List<(int, int, int)> Pour((int, int)[,] g, int[] a, int[] b, int from, int to, int d)
        {
            var res = new List<(int, int, int)>();

            d = Math.Min(a[from], d);

            if (d == 0)
            {
                return res;
            }

            var next = g[from, to].Item2;

            if (next != to)
            {
                res.AddRange(Pour(g, a, b, next, to, d));
            }

            a[next] += d;
            a[from] -= d;
            res.Add((from, next, d));

            return res;
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.ComTypes;

namespace Codeforces.R318.E
{
    public static class Solver
    {
        public static void Main()
        {
            using (var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false })
            {
                Solve(Console.In, sw);
            }
        }

        public static void Solve(TextReader tr, TextWriter tw)
        {
            WriteAnswer(tw, Parse(tr));
        }

        private static void WriteAnswer(TextWriter tw, (int, int, int)[] res)
        {
            if (res == null)
            {
                tw.WriteLine("NO");
                return;
            }

            tw.WriteLine(res.Length);

            foreach (var t in res)
            {
                tw.WriteLine("{0} {1} {2}", t.Item1, t.Item2, t.Item3);
            }
        }

        private static (int, int, int)[] Parse(TextReader tr)
        {
            var w = tr.ReadLine().Split();
            var v = int.Parse(w[1]);
            var e = int.Parse(w[2]);

            var a = from x in tr.ReadLine().Split() select int.Parse(x);
            var b = from x in tr.ReadLine().Split() select int.Parse(x);

            var edges = new (int, int)[e];
            for (int i = 0; i < e; ++i)
            {
                var l = tr.ReadLine().Split();
                edges[i] = (int.Parse(l[0]) - 1, int.Parse(l[1]) - 1);
            }

            return Calc(a.ToArray(), b.ToArray(), edges);
        }

        private static (int, int, int)[] Calc(int[] a, int[] b, (int, int)[] edges)
        {
            var n = a.Length;
            var g = new (int, int)[n, n];
            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    g[i, j] = i == j ? (0, j) : (n + 1, 0);
                }
            }

            foreach (var edge in edges)
            {
                g[edge.Item1, edge.Item2] = (1, edge.Item2);
                g[edge.Item2, edge.Item1] = (1, edge.Item1);
            }

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    for (int k = 0; k < n; ++k)
                    {
                        if (g[i, k].Item1 + g[k, j].Item1 < g[i, j].Item1)
                        {
                            g[i, j].Item1 = g[i, k].Item1 + g[k, j].Item1;
                            g[i, j].Item2 = k;
                        }
                    }
                }
            }

            var res = new List<(int, int, int)>();

            for (var updated = true; updated;)
            {
                updated = false;
                for (int i = 0; i < n; ++i)
                {
                    if (a[i] > b[i])
                    {
                        for (int j = 0; j < n; ++j)
                        {
                            if (a[j] < b[j])
                            {
                                if (g[i, j].Item1 > n)
                                {
                                    continue;
                                }
                                var list = Pour(g, a, b, i, j, Math.Min(a[i] - b[i], b[j] - a[j]));
                                if (list != null)
                                {
                                    res.AddRange(list);
                                    updated = true;
                                }
                            }
                        }
                    }
                }
            }

            if (!a.SequenceEqual(b))
            {
                return null;
            }

            return res.ToArray();
        }

        private static List<(int, int, int)> Pour((int, int)[,] g, int[] a, int[] b, int from, int to, int d)
        {
            d = Math.Min(a[from], d);

            if (d == 0)
            {
                return null;
            }

            var res = new List<(int, int, int)>();

            var next = g[from, to].Item2;

            if (next != to)
            {
                var list = Pour(g, a, b, next, to, d);
                if (list != null)
                {
                    res.AddRange(list);
                }
            }

            a[to] += d;
            a[from] -= d;
            res.Add((from + 1, to + 1, d));

            return res;
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Codeforces.R318.E
{
    public static class Solver
    {
        public static void Main()
        {
            using (var sw = new StreamWriter(Console.OpenStandardOutput()) { /*AutoFlush = false*/ })
            {
                Solve(Console.In, sw);
            }
        }

        public static void Solve(TextReader tr, TextWriter tw)
        {
            WriteAnswer(tw, Parse(tr));
        }

        private static void WriteAnswer(TextWriter tw, long[] res)
        {
            if (res == null)
            {
                Console.Out.WriteLine("NO");
                return;
            }

            Console.Out.WriteLine(res.Length);

            foreach (var t in res)
            {
                var prev = t & ((1 << 16) - 1);
                var to = (t >> 16) & ((1 << 16) - 1);
                var d = t >> 32;
                Console.Out.WriteLine("{0} {1} {2}", prev + 1, to + 1, d);
            }
            Environment.Exit(0);
        }

        private static long[] Parse(TextReader tr)
        {
            var w = tr.ReadLine().Split();
            var e = int.Parse(w[2]);

            var a = from x in tr.ReadLine().Split() select int.Parse(x);
            var b = from x in tr.ReadLine().Split() select int.Parse(x);

            var edges = new (int, int)[e];
            for (int i = 0; i < e; ++i)
            {
                var l = tr.ReadLine().Split();
                edges[i] = (int.Parse(l[0]) - 1, int.Parse(l[1]) - 1);
            }

            return Calc(a.ToArray(), b.ToArray(), edges);
        }

        public static long[] Calc(int[] a, int[] b, (int, int)[] edges)
        {
            var n = a.Length;
            var mat = new int[n, n];
            var prev = new int[n, n];

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    mat[i, j] = i == j ? 0 : 9999;
                    prev[i, j] = i;
                }
            }

            foreach (var edge in edges)
            {
                mat[edge.Item1, edge.Item2] = 1;
                prev[edge.Item1, edge.Item2] = edge.Item1;
                mat[edge.Item2, edge.Item1] = 1;
                prev[edge.Item2, edge.Item1] = edge.Item2;
            }

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    for (int k = 0; k < n; ++k)
                    {
                        if (mat[i, k] + mat[k, j] < mat[i, j])
                        {
                            mat[i, j] = mat[i, k] + mat[k, j];
                            prev[i, j] = prev[k, j];
                        }
                    }
                }
            }

            var res = Execute(a, b, mat, prev);

            if (!a.SequenceEqual(b))
            {
                return null;
            }

            return res.ToArray();
        }

        public static List<long> Execute(int[] a, int[] b, int[,] mat, int[,] prev)
        {
            var calculator = new Calculator(a, b, mat, prev);
            return calculator.Solve();
        }
    }

    internal class Calculator
    {
        List<long> res = new List<long>();
        int[] a;
        int[] b;
        int[,] mat;
        int[,] prev;

        public Calculator(int[] a, int[] b, int[,] mat, int[,] prev)
        {
            this.a = a;
            this.b = b;
            this.mat = mat;
            this.prev = prev;
        }

        public List<long> Solve()
        {
            var n = a.Length;
            for (; ; )
            {
                var prev = res.Count;
                for (int i = 0; i < n; ++i)
                {
                    if (a[i] > b[i])
                    {
                        for (int j = 0; j < n; ++j)
                        {
                            if (a[j] < b[j])
                            {
                                if (mat[i, j] > n)
                                {
                                    continue;
                                }
                                Pour(i, j, Math.Min(a[i] - b[i], b[j] - a[j]));
                            }
                        }
                    }
                }
                if (prev == res.Count)
                {
                    break;
                }
            }

            return res;
        }

        private void Pour(int from, int to, int d)
        {
            for (; ; )
            {
                var prev = this.prev[from, to];

                var p = Math.Min(a[prev], d);
                if (p > 0)
                {
                    a[prev] -= p;
                    a[to] += p;
                    res.Add(CreateKey(prev, to, p));
                }
                if (prev == from)
                {
                    break;
                }
                to = prev;
            }
        }

        private long CreateKey(long prev, long to, long d)
        {
            return (d << 32) + (to << 16) + prev;
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Codeforces.R318.E
{
    public static class Solver
    {
        public static void Main()
        {
            using (var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false })
            {
                Solve(Console.In, sw);
            }
        }

        public static void Solve(TextReader tr, TextWriter tw)
        {
            WriteAnswer(tw, Parse(tr));
        }

        private static void WriteAnswer(TextWriter tw, (int, int, int)[] res)
        {
            if (res == null)
            {
                tw.WriteLine("NO");
                return;
            }

            tw.WriteLine(res.Length);

            foreach (var t in res)
            {
                tw.WriteLine("{0} {1} {2}", t.Item1 + 1, t.Item2 + 1, t.Item3);
            }
        }

        private static (int, int, int)[] Parse(TextReader tr)
        {
            var w = tr.ReadLine().Split();
            var v = int.Parse(w[1]);
            var e = int.Parse(w[2]);

            var a = from x in tr.ReadLine().Split() select int.Parse(x);
            var b = from x in tr.ReadLine().Split() select int.Parse(x);

            var edges = new (int, int)[e];
            for (int i = 0; i < e; ++i)
            {
                var l = tr.ReadLine().Split();
                edges[i] = (int.Parse(l[0]) - 1, int.Parse(l[1]) - 1);
            }

            return Calc(a.ToArray(), b.ToArray(), edges, v);
        }

        public static (int, int, int)[] Calc(int[] a, int[] b, (int, int)[] edges, int v)
        {
            var n = a.Length;
            var mat = new (int, int)[n, n];
            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    mat[i, j] = i == j ? (0, j) : (9999, j);
                }
            }

            foreach (var edge in edges)
            {
                mat[edge.Item1, edge.Item2] = (1, edge.Item2);
                mat[edge.Item2, edge.Item1] = (1, edge.Item1);
            }

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    for (int k = 0; k < n; ++k)
                    {
                        if (mat[i, k].Item1 + mat[k, j].Item1 < mat[i, j].Item1)
                        {
                            mat[i, j] = (mat[i, k].Item1 + mat[k, j].Item1, k);
                        }
                    }
                }
            }

            List<(int, int, int)> res = Execute(a, b, n, mat);

            if (!a.SequenceEqual(b))
            {
                return null;
            }

            return res.ToArray();
        }

        public static (int, int, int)[] Calc0(int[] a, int[] b, (int, int)[] edges, int v)
        {
            var n = a.Length;
            var mat = new (int, int)[n, n];
            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    mat[i, j] = i == j ? (0, j) : (9999, j);
                }
            }

            foreach (var edge in edges)
            {
                mat[edge.Item1, edge.Item2] = (1, edge.Item2);
                mat[edge.Item2, edge.Item1] = (1, edge.Item1);
            }

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    for (int k = 0; k < n; ++k)
                    {
                        if (mat[i, k].Item1 + mat[k, j].Item1 < mat[i, j].Item1)
                        {
                            mat[i, j].Item1 = mat[i, k].Item1 + mat[k, j].Item1;
                            mat[i, j].Item2 = k;
                        }
                    }
                }
            }

            List<(int, int, int)> res = Execute(a, b, n, mat, v);

            if (!a.SequenceEqual(b))
            {
                return null;
            }

            return res.ToArray();
        }

        public static List<(int, int, int)> Execute(int[] a, int[] b, int n, (int, int)[,] mat)
        {
            var res = new List<(int, int, int)>();

            for (; ; )
            {
                var prev = res.Count;
                for (int i = 0; i < n; ++i)
                {
                    if (a[i] > b[i])
                    {
                        for (int j = 0; j < n; ++j)
                        {
                            if (a[j] < b[j])
                            {
                                if (mat[i, j].Item1 > n)
                                {
                                    continue;
                                }
                                res.AddRange(Pour(mat, a, b, i, j, Math.Min(a[i] - b[i], b[j] - a[j])));
                            }
                        }
                    }
                }
                if (prev == res.Count)
                {
                    break;
                }
            }

            return res;
        }

        public static List<(int, int, int)> Execute(int[] a, int[] b, int n, (int, int)[,] mat, int v)
        {
            var R = new List<(int, int, int)>();
            for (int x = 0; x < n; ++x)
            {
                for (int y = 0; y < n; ++y)
                {
                    if (mat[x, y].Item1 > n)
                        continue;
                    var j = Math.Min(a[x] - b[x], b[y] - a[y]);
                    if (j <= 0) continue;
                    Flow(x, y, j, mat, a, v, R);
                }
            }
            return R;
        }

        private static void Flow(int s, int t, int d, (int,int)[,] mat, int[] A, int V, List<(int,int,int)>R)
        {
            var N = mat.GetLength(0);

            var P = new List<int>();
            P.Add(s);

            for (var i = s; i != t;)
            {
                int y = 0;
                for (;y< N; ++y)
                    if (mat[i,y].Item1 == 1 && mat[y,t].Item1 == mat[i,t].Item1 - 1)
                        break;
                P.Add(i = y);
            }

            Flow2(P, A, 0, P.Count - 1, d, V, R);
        }

        private static void Flow2(List<int> P, int[] A, int si, int ti, int d, int V, List<(int, int, int)> R)
        {
            int i;
            if (si == ti) return;
            if (A[P[si + 1]] + d <= V)
            {
                A[P[si]] -= d;
                A[P[si + 1]] += d;
                R.Add((P[si], P[si + 1], d));
                Flow2(P, A, si + 1, ti, d, V, R);
            }
            else
            {
                int d2 = V - A[P[si + 1]];
                A[P[si]] -= d2;
                A[P[si + 1]] += d2;
                if (d2 > 0) R.Add((P[si], P[si + 1], d2));
                Flow2(P, A, si + 1, ti, d, V, R);
                A[P[si]] -= d - d2;
                A[P[si + 1]] += d - d2;
                R.Add((P[si], P[si + 1], d - d2));
            }
        }

        private static List<(int, int, int)> Pour((int, int)[,] g, int[] a, int[] b, int from, int to, int d)
        {
            var res = new List<(int, int, int)>();

            d = Math.Min(a[from], d);

            if (d == 0)
            {
                return res;
            }

            var next = g[from, to].Item2;

            if (next != to)
            {
                res.AddRange(Pour(g, a, b, next, to, d));
            }

            a[next] += d;
            a[from] -= d;
            res.Add((from, next, d));

            return res;
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Codeforces.R318.E
{
    public static class Solver
    {
        public static void Main()
        {
            using (var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false })
            {
                Solve(Console.In, sw);
            }
        }

        public static void Solve(TextReader tr, TextWriter tw)
        {
            WriteAnswer(tw, Parse(tr));
        }

        private static void WriteAnswer(TextWriter tw, (int, int, int)[] res)
        {
            if (res == null)
            {
                tw.WriteLine("NO");
                return;
            }

            tw.WriteLine(res.Length);

            foreach (var t in res)
            {
                tw.WriteLine("{0} {1} {2}", t.Item1, t.Item2, t.Item3);
            }
        }

        private static (int, int, int)[] Parse(TextReader tr)
        {
            var w = tr.ReadLine().Split();
            var e = int.Parse(w[2]);

            var a = from x in tr.ReadLine().Split() select long.Parse(x);
            var b = from x in tr.ReadLine().Split() select long.Parse(x);

            var edges = new (int, int)[e];
            for (int i = 0; i < e; ++i)
            {
                var l = tr.ReadLine().Split();
                edges[i] = (int.Parse(l[0]) - 1, int.Parse(l[1]) - 1);
            }

            return Calc(a.ToArray(), b.ToArray(), edges);
        }

        public static (int, int, int)[] Calc(long[] a, long[] b, (int, int)[] edges)
        {
            var n = a.Length;
            var g = new (int, int)[n, n];
            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    g[i, j] = i == j ? (0, j) : (9999, j);
                }
            }

            foreach (var edge in edges)
            {
                g[edge.Item1, edge.Item2] = (1, edge.Item2);
                g[edge.Item2, edge.Item1] = (1, edge.Item1);
            }

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    for (int k = 0; k < n; ++k)
                    {
                        if (g[i, k].Item1 + g[k, j].Item1 < g[i, j].Item1)
                        {
                            g[i, j].Item1 = g[i, k].Item1 + g[k, j].Item1;
                            g[i, j].Item2 = k;
                        }
                    }
                }
            }

            var res = new List<(int, int, int)>();

            for (; ; )
            {
                var prev = res.Count;
                for (int i = 0; i < n; ++i)
                {
                    if (a[i] > b[i])
                    {
                        for (int j = 0; j < n; ++j)
                        {
                            if (a[j] < b[j])
                            {
                                if (g[i, j].Item1 > n)
                                {
                                    continue;
                                }
                                res.AddRange(Pour(g, a, b, i, j, Math.Min(a[i] - b[i], b[j] - a[j])));
                            }
                        }
                    }
                }
                if (prev == res.Count)
                {
                    break;
                }
            }

            if (!a.SequenceEqual(b))
            {
                return null;
            }

            return res.ToArray();
        }

        private static List<(int, int, int)> Pour((int, int)[,] g, long[] a, long[] b, int from, int to, long d)
        {
            var res = new List<(int, int, int)>();

            d = Math.Min(a[from], d);

            if (d == 0)
            {
                return res;
            }

            var next = g[from, to].Item2;

            if (next != to)
            {
                res.AddRange(Pour(g, a, b, next, to, d));
            }

            a[next] += d;
            a[from] -= d;
            res.Add((from + 1, next + 1, (int)d));

            return res;
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.ComTypes;

namespace Codeforces.R318.E
{
    public static class Solver
    {
        public static void Main()
        {
            using (var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false })
            {
                Solve(Console.In, sw);
            }
        }

        public static void Solve(TextReader tr, TextWriter tw)
        {
            WriteAnswer(tw, Parse(tr));
        }

        private static void WriteAnswer(TextWriter tw, (int, int, int)[] res)
        {
            if (res == null)
            {
                tw.WriteLine("NO");
                return;
            }

            tw.WriteLine(res.Length);

            foreach (var t in res)
            {
                tw.WriteLine("{0} {1} {2}", t.Item1, t.Item2, t.Item3);
            }
        }

        private static (int, int, int)[] Parse(TextReader tr)
        {
            var w = tr.ReadLine().Split();
            var v = int.Parse(w[1]);
            var e = int.Parse(w[2]);

            var a = from x in tr.ReadLine().Split() select int.Parse(x);
            var b = from x in tr.ReadLine().Split() select int.Parse(x);

            var edges = new (int, int)[e];
            for (int i = 0; i < e; ++i)
            {
                var l = tr.ReadLine().Split();
                edges[i] = (int.Parse(l[0]) - 1, int.Parse(l[1]) - 1);
            }

            return Calc(a.ToArray(), b.ToArray(), edges);
        }

        private static (int, int, int)[] Calc(int[] a, int[] b, (int, int)[] edges)
        {
            var n = a.Length;
            var g = new (int, int)[n, n];
            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    g[i, j] = i == j ? (0, j) : (n + 1, 0);
                }
            }

            foreach (var edge in edges)
            {
                g[edge.Item1, edge.Item2] = (1, edge.Item2);
                g[edge.Item2, edge.Item1] = (1, edge.Item1);
            }

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    for (int k = 0; k < n; ++k)
                    {
                        if (g[i, k].Item1 + g[k, j].Item1 < g[i, j].Item1)
                        {
                            g[i, j].Item1 = g[i, k].Item1 + g[k, j].Item1;
                            g[i, j].Item2 = k;
                        }
                    }
                }
            }

            var res = new List<(int, int, int)>();

            for (var updated = true; updated;)
            {
                updated = false;
                for (int i = 0; i < n; ++i)
                {
                    if (a[i] > b[i])
                    {
                        for (int j = 0; j < n; ++j)
                        {
                            if (a[j] < b[j])
                            {
                                if (g[i, j].Item1 > n)
                                {
                                    continue;
                                }
                                var list = Pour(g, a, b, i, j, Math.Min(a[i] - b[i], b[j] - a[j]));
                                if (list != null)
                                {
                                    res.AddRange(list);
                                    updated = true;
                                }
                            }
                        }
                    }
                }
            }

            if (!a.SequenceEqual(b))
            {
                return null;
            }

            return res.ToArray();
        }

        private static List<(int, int, int)> Pour((int, int)[,] g, int[] a, int[] b, int from, int to, int d)
        {
            d = Math.Min(a[from], d);

            if (d == 0)
            {
                return null;
            }

            var res = new List<(int, int, int)>();

            var next = g[from, to].Item2;

            if (next != to)
            {
                var list = Pour(g, a, b, next, to, d);
                if (list != null)
                {
                    res.AddRange(list);
                }
            }

            a[next] += d;
            a[from] -= d;
            res.Add((from + 1, next + 1, d));

            return res;
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Codeforces.R318.E
{
    public static class Solver
    {
        public static void Main()
        {
            using (var sw = new StreamWriter(Console.OpenStandardOutput()) {
                AutoFlush = false
            })
            {
                Solve(Console.In, sw);
            }
        }

        public static void Solve(TextReader tr, TextWriter tw)
        {
            WriteAnswer(tw, Parse(tr));
        }

        private static void WriteAnswer(TextWriter tw, long[] res)
        {
            if (res == null)
            {
                tw.WriteLine("NO");
                return;
            }

            tw.WriteLine(res.Length);

            foreach (var t in res)
            {
                var prev = t & ((1 << 16) - 1);
                var to = (t >> 16) & ((1 << 16) - 1);
                var d = t >> 32;
                tw.WriteLine("{0} {1} {2}", prev + 1, to + 1, d);
            }
        }

        private static long[] Parse(TextReader tr)
        {
            var w = tr.ReadLine().Split();
            var e = int.Parse(w[2]);

            var a = from x in tr.ReadLine().Split() select int.Parse(x);
            var b = from x in tr.ReadLine().Split() select int.Parse(x);

            var edges = new (int, int)[e];
            for (int i = 0; i < e; ++i)
            {
                var l = tr.ReadLine().Split();
                edges[i] = (int.Parse(l[0]) - 1, int.Parse(l[1]) - 1);
            }

            return Calc(a.ToArray(), b.ToArray(), edges);
        }

        public static long[] Calc(int[] a, int[] b, (int, int)[] edges)
        {
            var n = a.Length;
            var mat = new (int, int)[n, n];

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    mat[i, j] = i == j ? (0, i) : (9999, i);
                }
            }

            foreach (var edge in edges)
            {
                mat[edge.Item1, edge.Item2] = (1, edge.Item1);
                mat[edge.Item2, edge.Item1] = (1, edge.Item2);
            }

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    for (int k = 0; k < n; ++k)
                    {
                        if (mat[i, k].Item1 + mat[k, j].Item1 < mat[i, j].Item1)
                        {
                            mat[i, j] = (mat[i, k].Item1 + mat[k, j].Item1, mat[k, j].Item2);
                        }
                    }
                }
            }

            var res = Execute(a, b, mat);

            if (!a.SequenceEqual(b))
            {
                return null;
            }

            return res.ToArray();
        }

        public static List<long> Execute(int[] a, int[] b, (int, int)[,] mat)
        {
            var calculator = new Calculator(a, b, mat);
            return calculator.Solve();
        }
    }

    internal class Calculator
    {
        List<long> res = new List<long>();
        int[] a;
        int[] b;
        (int, int)[,] mat;

        public Calculator(int[] a, int[] b, (int, int)[,] mat)
        {
            this.a = a;
            this.b = b;
            this.mat = mat;
        }

        public List<long> Solve()
        {
            var n = a.Length;
            for (; ; )
            {
                var prev = res.Count;
                for (int i = 0; i < n; ++i)
                {
                    for (int j = 0; j < n; ++j)
                    {
                        if (a[i] > b[i] && a[j] < b[j] && mat[i, j].Item1 <= n)
                        {
                            Pour(i, j, Math.Min(a[i] - b[i], b[j] - a[j]));
                        }
                    }
                }
                if (prev == res.Count)
                {
                    break;
                }
            }

            return res;
        }

        private void Pour(int from, int to, int d)
        {
            for (; ; )
            {
                var prev = mat[from, to].Item2;

                var p = Math.Min(a[prev], d);
                if (p > 0)
                {
                    a[prev] -= p;
                    a[to] += p;
                    res.Add(CreateKey(prev, to, p));
                }
                if (prev == from)
                {
                    break;
                }
                to = prev;
            }
        }

        private long CreateKey(long prev, long to, long d)
        {
            return (d << 32) + (to << 16) + prev;
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Codeforces.R318.E
{
    public static class Solver
    {
        public static void Main()
        {
            using (var sw = new StreamWriter(Console.OpenStandardOutput()) {
                AutoFlush = false
            })
            {
                Solve(Console.In, sw);
            }
        }

        public static void Solve(TextReader tr, TextWriter tw)
        {
            WriteAnswer(tw, Parse(tr));
        }

        private static void WriteAnswer(TextWriter tw, long[] res)
        {
            if (res == null)
            {
                tw.WriteLine("NO");
                return;
            }

            tw.WriteLine(res.Length);

            foreach (var t in res)
            {
                var prev = t & ((1 << 16) - 1);
                var to = (t >> 16) & ((1 << 16) - 1);
                var d = t >> 32;
                tw.WriteLine("{0} {1} {2}", prev + 1, to + 1, d);
            }
        }

        private static long[] Parse(TextReader tr)
        {
            var w = tr.ReadLine().Split();
            var e = int.Parse(w[2]);

            var a = from x in tr.ReadLine().Split() select int.Parse(x);
            var b = from x in tr.ReadLine().Split() select int.Parse(x);

            var edges = new (int, int)[e];
            for (int i = 0; i < e; ++i)
            {
                var l = tr.ReadLine().Split();
                edges[i] = (int.Parse(l[0]) - 1, int.Parse(l[1]) - 1);
            }

            return Calc(a.ToArray(), b.ToArray(), edges);
        }

        public static long[] Calc(int[] a, int[] b, (int, int)[] edges)
        {
            var n = a.Length;
            var mat = new (int, int)[n, n];

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    mat[i, j] = i == j ? (0, i) : (9999, i);
                }
            }

            foreach (var edge in edges)
            {
                mat[edge.Item1, edge.Item2] = (1, edge.Item1);
                mat[edge.Item2, edge.Item1] = (1, edge.Item2);
            }

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    for (int k = 0; k < n; ++k)
                    {
                        if (mat[i, k].Item1 + mat[k, j].Item1 < mat[i, j].Item1)
                        {
                            mat[i, j] = (mat[i, k].Item1 + mat[k, j].Item1, mat[k, j].Item2);
                        }
                    }
                }
            }

            var res = Execute(a, b, mat);

            if (!a.SequenceEqual(b))
            {
                return null;
            }

            return res.ToArray();
        }

        public static List<long> Execute(int[] a, int[] b, (int, int)[,] mat)
        {
            var calculator = new Calculator(a, b, mat);
            return calculator.Solve();
        }
    }

    internal class Calculator
    {
        List<long> res = new List<long>();
        int[] a;
        int[] b;
        (int, int)[,] mat;

        public Calculator(int[] a, int[] b, (int, int)[,] mat)
        {
            this.a = a;
            this.b = b;
            this.mat = mat;
        }

        public List<long> Solve()
        {
            var n = a.Length;
            for (; ; )
            {
                var prev = res.Count;
                for (int i = 0; i < n; ++i)
                {
                    for (int j = 0; j < n; ++j)
                    {
                        if (a[i] > b[i] && a[j] < b[j] && mat[i, j].Item1 <= n)
                        {
                            Pour(i, j, Math.Min(a[i] - b[i], b[j] - a[j]));
                        }
                    }
                }
                if (prev == res.Count)
                {
                    break;
                }
            }

            return res;
        }

        private void Pour(int from, int to, int d)
        {
            var n = a.Length;
            var prevList = new List<int>();
            for (var cur = from; cur != to;)
            {
                prevList.Add(cur);
                for (int i = 0; i < n; ++i)
                {
                    if (mat[cur, i].Item1 == 1 && mat[i, to].Item1 == mat[cur, to].Item1 - 1)
                    {
                        cur = i;
                        break;
                    }
                }
            }

            for (var i = prevList.Count - 1; i >= 0; --i)
            {
                var prev = prevList[i];// mat[from, to].Item2;

                var p = Math.Min(a[prev], d);
                if (p > 0)
                {
                    a[prev] -= p;
                    a[to] += p;
                    res.Add(CreateKey(prev, to, p));
                }
                if (prev == from)
                {
                    break;
                }
                to = prev;
            }
        }

        private long CreateKey(long prev, long to, long d)
        {
            return (d << 32) + (to << 16) + prev;
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Codeforces.R318.E
{
    public static class Solver
    {
        public static void Main()
        {
            using (var sw = new StreamWriter(Console.OpenStandardOutput()) {
                AutoFlush = false
            })
            {
                Solve(Console.In, sw);
            }
        }

        public static void Solve(TextReader tr, TextWriter tw)
        {
            WriteAnswer(tw, Parse(tr));
        }

        private static void WriteAnswer(TextWriter tw, long[] res)
        {
            if (res == null)
            {
                tw.WriteLine("NO");
                return;
            }

            tw.WriteLine(res.Length);

            foreach (var t in res)
            {
                var prev = t & ((1 << 16) - 1);
                var to = (t >> 16) & ((1 << 16) - 1);
                var d = t >> 32;
                tw.WriteLine("{0} {1} {2}", prev + 1, to + 1, d);
            }
        }

        private static long[] Parse(TextReader tr)
        {
            var w = tr.ReadLine().Split();
            var e = int.Parse(w[2]);

            var a = from x in tr.ReadLine().Split() select int.Parse(x);
            var b = from x in tr.ReadLine().Split() select int.Parse(x);

            var edges = new (int, int)[e];
            for (int i = 0; i < e; ++i)
            {
                var l = tr.ReadLine().Split();
                edges[i] = (int.Parse(l[0]) - 1, int.Parse(l[1]) - 1);
            }

            return Calc(a.ToArray(), b.ToArray(), edges);
        }

        public static long[] Calc(int[] a, int[] b, (int, int)[] edges)
        {
            var n = a.Length;
            var mat = new (int, int)[n, n];

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    mat[i, j] = i == j ? (0, i) : (9999, i);
                }
            }

            foreach (var edge in edges)
            {
                mat[edge.Item1, edge.Item2] = (1, edge.Item1);
                mat[edge.Item2, edge.Item1] = (1, edge.Item2);
            }

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    for (int k = 0; k < n; ++k)
                    {
                        if (mat[i, k].Item1 + mat[k, j].Item1 < mat[i, j].Item1)
                        {
                            mat[i, j] = (mat[i, k].Item1 + mat[k, j].Item1, mat[k, j].Item2);
                        }
                    }
                }
            }

            var res = Execute(a, b, mat);

            if (!a.SequenceEqual(b))
            {
                return null;
            }

            return res.ToArray();
        }

        public static List<long> Execute(int[] a, int[] b, (int, int)[,] mat)
        {
            var calculator = new Calculator(a, b, mat);
            return calculator.Solve();
        }
    }

    internal class Calculator
    {
        List<long> res = new List<long>();
        int[] a;
        int[] b;
        (int, int)[,] mat;

        public Calculator(int[] a, int[] b, (int, int)[,] mat)
        {
            this.a = a;
            this.b = b;
            this.mat = mat;
        }

        public List<long> Solve()
        {
            var n = a.Length;
            for (; ; )
            {
                var prev = res.Count;
                for (int i = 0; i < n; ++i)
                {
                    if (a[i] > b[i])
                    {
                        for (int j = 0; j < n; ++j)
                        {
                            if (a[j] < b[j])
                            {
                                if (mat[i, j].Item1 > n)
                                {
                                    continue;
                                }
                                Pour(i, j, Math.Min(a[i] - b[i], b[j] - a[j]));
                            }
                        }
                    }
                }
                if (prev == res.Count)
                {
                    break;
                }
            }

            return res;
        }

        private void Pour(int from, int to, int d)
        {
            for (; ; )
            {
                var prev = mat[from, to].Item2;

                var p = Math.Min(a[prev], d);
                if (p > 0)
                {
                    a[prev] -= p;
                    a[to] += p;
                    res.Add(CreateKey(prev, to, p));
                }
                if (prev == from)
                {
                    break;
                }
                to = prev;
            }
        }

        private long CreateKey(long prev, long to, long d)
        {
            return (d << 32) + (to << 16) + prev;
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.ComTypes;

namespace Codeforces.R318.E
{
    public static class Solver
    {
        public static void Main()
        {
            using (var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false })
            {
                Solve(Console.In, sw);
            }
        }

        public static void Solve(TextReader tr, TextWriter tw)
        {
            WriteAnswer(tw, Parse(tr));
        }

        private static void WriteAnswer(TextWriter tw, (int, int, int)[] res)
        {
            if (res == null)
            {
                tw.WriteLine("NO");
                return;
            }

            tw.WriteLine(res.Length);

            foreach (var t in res)
            {
                tw.WriteLine("{0} {1} {2}", t.Item1, t.Item2, t.Item3);
            }
        }

        private static (int, int, int)[] Parse(TextReader tr)
        {
            var w = tr.ReadLine().Split();
            var v = int.Parse(w[1]);
            var e = int.Parse(w[2]);

            var a = from x in tr.ReadLine().Split() select long.Parse(x);
            var b = from x in tr.ReadLine().Split() select long.Parse(x);

            var edges = new (int, int)[e];
            for (int i = 0; i < e; ++i)
            {
                var l = tr.ReadLine().Split();
                edges[i] = (int.Parse(l[0]) - 1, int.Parse(l[1]) - 1);
            }

            return Calc(a.ToArray(), b.ToArray(), edges);
        }

        private static (int, int, int)[] Calc(long[] a, long[] b, (int, int)[] edges)
        {
            var n = a.Length;
            var g = new (int, int)[n, n];
            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    g[i, j] = i == j ? (0, j) : (n + 1, 0);
                }
            }

            foreach (var edge in edges)
            {
                g[edge.Item1, edge.Item2] = (1, edge.Item2);
                g[edge.Item2, edge.Item1] = (1, edge.Item1);
            }

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    for (int k = 0; k < n; ++k)
                    {
                        if (g[i, k].Item1 + g[k, j].Item1 < g[i, j].Item1)
                        {
                            g[i, j].Item1 = g[i, k].Item1 + g[k, j].Item1;
                            g[i, j].Item2 = k;
                        }
                    }
                }
            }

            var res = new List<(int, int, int)>();

            for (var updated = true; updated;)
            {
                updated = false;
                for (int i = 0; i < n; ++i)
                {
                    if (a[i] > b[i])
                    {
                        for (int j = 0; j < n; ++j)
                        {
                            if (a[j] < b[j])
                            {
                                if (g[i, j].Item1 > n)
                                {
                                    continue;
                                }
                                var list = Pour(g, a, b, i, j, Math.Min(a[i] - b[i], b[j] - a[j]));
                                if (list != null)
                                {
                                    res.AddRange(list);
                                    updated = true;
                                }
                            }
                        }
                    }
                }
            }

            if (!a.SequenceEqual(b))
            {
                return null;
            }

            return res.ToArray();
        }

        private static List<(int, int, int)> Pour((int, int)[,] g, long[] a, long[] b, int from, int to, long d)
        {
            d = Math.Min(a[from], d);

            if (d == 0)
            {
                return null;
            }

            var res = new List<(int, int, int)>();

            var next = g[from, to].Item2;

            if (next != to)
            {
                var list = Pour(g, a, b, next, to, d);
                if (list != null)
                {
                    res.AddRange(list);
                }
            }

            a[next] += d;
            a[from] -= d;
            res.Add((from + 1, next + 1, (int)d));

            return res;
        }
    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.ComTypes;

namespace Codeforces.R318.E
{
    public static class Solver
    {
        public static void Main()
        {
            using (var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false })
            {
                Solve(Console.In, sw);
            }
        }

        public static void Solve(TextReader tr, TextWriter tw)
        {
            WriteAnswer(tw, Parse(tr));
        }

        private static void WriteAnswer(TextWriter tw, (int, int, int)[] res)
        {
            if (res == null)
            {
                tw.WriteLine("NO");
                return;
            }

            tw.WriteLine(res.Length);

            foreach (var t in res)
            {
                tw.WriteLine("{0} {1} {2}", t.Item1, t.Item2, t.Item3);
            }
        }

        private static (int, int, int)[] Parse(TextReader tr)
        {
            var w = tr.ReadLine().Split();
            var v = int.Parse(w[1]);
            var e = int.Parse(w[2]);

            var a = from x in tr.ReadLine().Split() select long.Parse(x);
            var b = from x in tr.ReadLine().Split() select long.Parse(x);

            var edges = new (int, int)[e];
            for (int i = 0; i < e; ++i)
            {
                var l = tr.ReadLine().Split();
                edges[i] = (int.Parse(l[0]) - 1, int.Parse(l[1]) - 1);
            }

            return Calc(a.ToArray(), b.ToArray(), edges);
        }

        private static (int, int, int)[] Calc(long[] a, long[] b, (int, int)[] edges)
        {
            var n = a.Length;
            var g = new (int, int)[n, n];
            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    g[i, j] = i == j ? (0, j) : (n + 1, j);
                }
            }

            foreach (var edge in edges)
            {
                g[edge.Item1, edge.Item2] = (1, edge.Item2);
                g[edge.Item2, edge.Item1] = (1, edge.Item1);
            }

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    for (int k = 0; k < n; ++k)
                    {
                        if (g[i, k].Item1 + g[k, j].Item1 < g[i, j].Item1)
                        {
                            g[i, j].Item1 = g[i, k].Item1 + g[k, j].Item1;
                            g[i, j].Item2 = k;
                        }
                    }
                }
            }

            var res = new List<(int, int, int)>();

            for (; ; )
            {
                var prev = res.Count;
                for (int i = 0; i < n; ++i)
                {
                    if (a[i] > b[i])
                    {
                        for (int j = 0; j < n; ++j)
                        {
                            if (a[j] < b[j])
                            {
                                if (g[i, j].Item1 > n)
                                {
                                    continue;
                                }
                                res.AddRange(Pour(g, a, b, i, j, Math.Min(a[i] - b[i], b[j] - a[j])));
                            }
                        }
                    }
                }
                if (prev == res.Count)
                {
                    break;
                }
            }

            if (!a.SequenceEqual(b))
            {
                return null;
            }

            return res.ToArray();
        }

        private static List<(int, int, int)> Pour((int, int)[,] g, long[] a, long[] b, int from, int to, long d)
        {
            var res = new List<(int, int, int)>();

            d = Math.Min(a[from], d);

            if (d == 0)
            {
                return res;
            }

            var next = g[from, to].Item2;

            if (next != to)
            {
                res.AddRange(Pour(g, a, b, next, to, d));
            }

            a[next] += d;
            a[from] -= d;
            res.Add((from + 1, next + 1, (int)d));

            return res;
        }
    }
}