← Home
```go
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	// Use buffered I/O for fast input reading
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	// Increase buffer capacity to handle large inputs if necessary
	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	scanner.Buffer(buf, maxCapacity)

	// Helper to scan next integer
	var scanInt = func() int {
		scanner.Scan()
		val, _ := strconv.Atoi(scanner.Text())
		return val
	}

	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	// Read number of test cases
	if !scanner.Scan() {
		return
	}
	t, _ := strconv.Atoi(scanner.Text())

	for i := 0; i < t; i++ {
		n := scanInt()
		m := scanInt()

		// Read batteries at each checkpoint. 1-based indexing.
		b := make([]int, n+1)
		for j := 1; j <= n; j++ {
			b[j] = scanInt()
		}

		type Edge struct {
			to, w int
		}
		adj := make([][]Edge, n+1)

		for j := 0; j < m; j++ {
			u := scanInt()
			v := scanInt()
			w := scanInt()
			adj[u] = append(adj[u], Edge{v, w})
		}

		// dp[u] stores the maximum number of batteries we can have at node u
		// given a capacity limit X.
		dp := make([]int, n+1)

		// Check function for Binary Search
		check := func(x int) bool {
			// Initialize DP with -1 (unreachable)
			for k := 1; k <= n; k++ {
				dp[k] = -1
			}

			// Base case: at start node 1
			// We can pick up batteries up to b[1], but limited by x
			if b[1] >= x {
				dp[1] = x
			} else {
				dp[1] = b[1]
			}

			// Iterate through nodes in topological order (1 to n)
			// Since s_i < t_i, the graph is a DAG compatible with index order.
			for u := 1; u < n; u++ {
				if dp[u] == -1 {
					continue
				}
				cur := dp[u]
				for _, e := range adj[u] {
					// Can only traverse if we have enough batteries
					if e.w <= cur {
						// At destination v, we add b[v] batteries, capped at x
						nextBat := cur + b[e.to]
						if nextBat > x {
							nextBat = x
						}
						// Maximize batteries at v
						if nextBat > dp[e.to] {
							dp[e.to] = nextBat
						}
					}
				}
			}
			return dp[n] != -1
		}

		// Binary Search for the minimum X
		// Max possible answer is bounded by max(w_i), which is 10^9.
		// If impossible with 10^9, then it's impossible.
		const maxVal = 1000000000
		if !check(maxVal) {
			fmt.Fprintln(writer, -1)
		} else {
			low, high := 0, maxVal
			ans := -1
			for low <= high {
				mid := (low + high) / 2
				if check(mid) {
					ans = mid
					high = mid - 1
				} else {
					low = mid + 1
				}
			}
			fmt.Fprintln(writer, ans)
		}
	}
}
```