luogu#P4941. War2

War2

Background

The XM Battle is arriving as scheduled. The Agents gather together for the final showdown. There are many ways to fight, and some complicated methods can earn higher scores. Unfortunately, the people from ENLIGHTENED are not very smart and only know simple hack, so the ENLIGHTENED operation commander asked you to be their chief strategist.

Problem Description

There are NN Portals on the map. Now an Agent's task is to capture MM Portals on this map. The score gained by capturing the ii-th Portal is A[i]A[i]. Besides capturing directly, there are also KK other bonus-scoring methods. For these NN Portals, after capturing the X[i]X[i]-th Portal, capturing the Y[i]Y[i]-th Portal will grant an additional bonus of B[i]B[i]. Bonus rules may be duplicated. The Agent wants to earn more points for the team, so he asks you, the battle strategist, to help him.

Input Format

The first line contains three integers N,M,KN, M, K. The second line contains NN numbers, where the ii-th number represents A[i]A[i]. The next KK lines each contain three integers X[i],Y[i],C[i]X[i], Y[i], C[i], indicating that after capturing the X[i]X[i]-th Portal, capturing the Y[i]Y[i]-th Portal will grant an additional bonus of B[i]B[i].

Output Format

Output only one line with one integer, the maximum score this Agent can obtain.

3 2 1
1 1 1
1 2 3
5
4 3 2
1 1 1 1
4 3 2
3 2 1
6

Hint

For 20%20\% of the data: $1 \leq M \leq N \leq 4, 0 \leq A[i], B[i] \leq 10^3$.

For 40%40\% of the data: $1 \leq M \leq N \leq 8, 0 \leq A[i], B[i] \leq 10^5$.

For 60%60\% of the data: $1 \leq M \leq N \leq 12, 0 \leq A[i], B[i] \leq 10^7$.

For 100%100\% of the data: $1 \leq M, X[i], Y[i] \leq N \leq 18, 0 \leq K \leq N^2-N, 0 \leq A[i], B[i] \leq 10^9$.

Translated by ChatGPT 5